How to make a mirror image

We know that mirroring can be done in two ways:Copy the code
  1. In the Dockerfile formatCopy the code
  2. In the commit modeCopy the code

    We usually use Dockerfile to create our own service image, in Dockerfile we will use FROM to specify the base image, and then on the base image or install some required software, or make some other changes, and then their packaged projects added to the image.

    The commit mode is used to customize your own image. For example, if we need to make some changes to the base image and install some software, we can use the commit method to create a custom base image for ourselves. The nice thing about this is that we only make these changes once when we make the base image. When we use Dockfile to make the service image, we can use our custom base image FROM later. This eliminates the need to download software over the network and greatly reduces the time needed to construct a service image. Our Dockerfile will also look simpler and have fewer rows.

    The following docker commands (not all of them, depending on your needs) are used during the commit process:

  • docker images
    Copy the code
  • docker pull
    Copy the code
  • docker run
    Copy the code
  • docker ps
    Copy the code
  • docker start
    Copy the code
  • docker attach
    Copy the code
  • docker commit
    Copy the code
  • docker start
    Copy the code
  • docker push
    Copy the code

Viewing a Local Mirror

First, we use Docker Images to view our local image. For example, if we want to create an image based on OpenJDK: 8-jre-Alpine, let's see if there is any local JDK image.Copy the code
$ docker images
REPOSITORY                                                       TAG                 IMAGE ID            CREATED             SIZE
testwx                                                           latest              49729fc355e3        6 days ago          106MB
Copy the code

Pull the image from the warehouse

If we don’t have a mirror we need locally, we need to pull it from the mirror repository

Docker pull repository address/OpenJDK: 8-JDK-alpineCopy the code

Run the image

The next step is to run the JDK image. Once an image is run, it becomes a container with a lifecycle.

docker run -ti openjdk:8-jdk-alpine
Copy the code

When we use exit to exit the container, the container stops.

Viewing the Container Status

We can view the status of the container by using the ps command.

$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 10c9e8af6359 testwx "/bin/sh" 6 days ago Exited (0)  23 seconds ago nostalgic_euclid f4c501780d5a testwx "/bin/sh" 6 days ago Exited (0) 20 hours ago sleepy_fermatCopy the code

Start the container

We can start a container by using the start command. Note that the start command is followed by the name or ID of the container, not the image. To start a container with an image, use the RUN command.

$ docker start 10c9
10c9
Copy the code

Plug in the container and make some changes

We can attach the container with the attach command. Note that this command also attaches the name or ID of the container.

# $docker attach 10 c/apk update the fetch https://mirrors.aliyun.com/alpine/v3.9/main/x86_64/APKINDEX.tar.gz fetch https://mirrors.aliyun.com/alpine/v3.9/community/x86_64/APKINDEX.tar.gz v3.9.6-44 - g3992359a2b [https://mirrors.aliyun.com/alpine/v3.9/main] v3.9.6-42 - g88b6599af0 [https://mirrors.aliyun.com/alpine/v3.9/community] OK: 9789 distinct packages available / # apk add tini 0% K: 103 MiB in 55 packages / #Copy the code

The above is to install a Tini software, but this container has already installed Tini before, so there is no repeat installation.

Save the changes to the container

The next step is to mirror the container changes with the commit command. The commit command creates a new image from the container, so note that the commit command is also followed by the container name or ID.

$ docker commit -a "wenxue" -m "test" 10c testimage
sha256:1b8a4c6421a38da7a73837deb819735ed329472a7fcd64760b67898717032ce1
Copy the code

The custom image is now complete.

Finally, we can use the images command to see the image we just made.

We can also use the push command to push our images to the warehouse.

In addition, it is recommended that you only use the commit method to learn about the image. In practice, always use Dockerfile to create the image, because the image produced by the COMMIT is a black box, the user does not know what commands are executed in the image, and what operations are performed at each layer. The image produced by Dockerfile can be searched through the docker history command for each command in the Dockerfile.