Docker Image Management - 5. Create your own Docker image (3/3 based on existing images, create new images)

Keywords: Operation & Maintenance Docker JSON ssh

1. Description of existing mirror problems

1.1. Download existing images

See Existing Docker Mirrors . The command for Pull mirror canhui/open-mpi-base:v0.0.1 is as follows.

$ docker pull canhui/open-mpi-base:v0.0.1

1.2. Problem Description

There are two problems with existing mirrors. First, after executing the command docker run canhui/open-mpi-base:v0.0.1, the container task exits immediately after execution. Second, after executing the command docker run canhui/open-mpi-base:v0.0.1, port 22 of container is closed.

There are two solutions. Firstly, a dead-cycle task will be given to the container through the Dockerfile startup parameter to ensure that the container never dies. Similarly, open the port for container through the Dockerfile boot parameter. Secondly, through the docker run start parameter, a dead-cycle task is given to the container to ensure that the container never dies. Similarly, the container is opened by docker run start parameters. This article explains the first solution.



2. Creating new images based on existing images

2.1. Add Function 1: Open Port

Create a new folder named "Image" and add a file named "Docker file" to it.

$ mkdir Image
$ cd Image
$ touch Dockerfile

Dockerfile adds two functions to the existing mirror canhui/open-mpi-base:v0.0.1: adds a dead-cycle task and opens port 22. The "Dockerfile" file reads as follows.

FROM canhui/open-mpi-base:v0.0.1
COPY . /Image
EXPOSE 22
CMD [ "/bin/sh", "-c", "while true; do echo hi; sleep 86400;done"]

2.2. Adding Function 2: Password of root User in Mirror System

Add a root user's password "hello" to the mirror file.

FROM canhui/open-mpi-base:v0.0.1
COPY . /Image
EXPOSE 22
RUN echo "root:hello" | chpasswd
CMD [ "/bin/sh", "-c", "while true; do echo hi; sleep 86400;done"]

2.3. Generating a new image based on Dockerfile

Docker file is compiled and executed through Docker build-t to generate the latest version of open-mpi-base image. Note: The "dot" at the end of docker built cannot be dropped.

$ docker build -t open-mpi-base .
Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM canhui/open-mpi-base:v0.0.1
 ---> 9092b2afc543
Step 2/5 : COPY . /Image
 ---> 229c534679ab
Step 3/5 : EXPOSE 22
 ---> Running in 6ba4bc88fb39
Removing intermediate container 6ba4bc88fb39
 ---> b63da86548ba
Step 4/5 : RUN echo "root:hello" | chpasswd
 ---> Running in d564e23dcec9
Removing intermediate container d564e23dcec9
 ---> a56ca17c18f3
Step 5/5 : CMD [ "/bin/sh", "-c", "while true; do echo hi; sleep 86400;done"]
 ---> Running in bbbec218c52a
Removing intermediate container bbbec218c52a
 ---> 8ff4bf699689
Successfully built 8ff4bf699689
Successfully tagged open-mpi-base:latest

2.4. Log on to Docker Hub

Log in to Docker Hub through Docker login as follows.

$ docker login
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /home/joe/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

2.5. Label the latest local image (Docker Hub cannot be uploaded without a label)

View the image id of the latest local image and label it with the image id.

$ docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
open-mpi-base                latest              4050bd63f493        4 minutes ago       221MB
canhui/open-mpi-base         v0.0.1              9092b2afc543        13 days ago         221MB
hyperledger/fabric-ca        x86_64-1.1.0        72617b4fa9b4        14 months ago       299MB
hyperledger/fabric-orderer   x86_64-1.1.0        ce0c810df36a        14 months ago       180MB
hyperledger/fabric-peer      x86_64-1.1.0        b023f9be0771        14 months ago       187MB
hyperledger/fabric-ccenv     x86_64-1.1.0        c8b4909d8d46        14 months ago       1.39GB
hyperledger/fabric-baseos    x86_64-0.4.6        220e5cf3fb7f        15 months ago       151MB
$ docker tag 4050bd63f493 canhui/open-mpi-base:v0.0.5

2.6. Upload Mirror Image

Upload the latest image through docker push.

$ docker push canhui/open-mpi-base
The push refers to repository [docker.io/canhui/open-mpi-base]
ffd76d68dee2: Layer already exists 
8d267010480f: Layer already exists 
270f934787ed: Layer already exists 
02571d034293: Layer already exists 
v0.0.1: digest: sha256:207eca20a77bbed1b8783abe509f1204620923ede3bb4af449ca3b47124be739 size: 1155
8ed776e740ee: Layer already exists 
ffd76d68dee2: Layer already exists 
8d267010480f: Layer already exists 
270f934787ed: Layer already exists 
02571d034293: Layer already exists 
v0.0.5: digest: sha256:019ddd48c89d6a1fb23b71f36df20a3865d7818cdd193ede2895c8f4375af41d size: 1362
8ed776e740ee: Layer already exists 
ffd76d68dee2: Layer already exists 
8d267010480f: Layer already exists 
270f934787ed: Layer already exists 
02571d034293: Layer already exists 
v0.0.6: digest: sha256:019ddd48c89d6a1fb23b71f36df20a3865d7818cdd193ede2895c8f4375af41d size: 1362


3. test

Drop down the image of the specified version through docker pull.

docker pull canhui/open-mpi-base:v0.0.6

Run the mirror to generate the container.

$ docker run open-mpi-base
hi

Close the old terminal. Then, open a new terminal and view the container status.

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
5cdfc92987d2        open-mpi-base       "/bin/sh -c 'while t..."   13 seconds ago      Up 11 seconds       22/tcp              stupefied_bell

It was found that the container was still running in the background and port 22 was open. Complete.

Finally, stop and delete the container.

$ docker stop 5cdfc92987d2
$ docker rm 5cdfc92987d2
$ docker ps -a


Reference

[1. Dockerfile generates mirror files] https://blog.csdn.net/Canhui_WANG/article/details/90261063
[1. docker image uploads docker hub] https://blog.csdn.net/Canhui_WANG/article/details/90264180



Remarks

[Version 1] Preliminary draft, May 18, 2019.
[Version 2] Second draft, Question: ssh is open but has bugs, May 29, 2019.

Posted by padma on Wed, 29 May 2019 01:56:02 -0700