Shipping an image

We usually ship Docker images via a Docker registry. The public registry hosted by Docker Inc. is called Docker Hub. To ship a Docker image to a registry, we use the docker push command. When we start a container, its image will be automatically checked and downloaded to the host before running. The process of downloading can be explicitly done using the docker pull command. The following diagram illustrates the push/pull behavior among different environments and registries:

Figure 2.4: Push and pull image workflow

In the previous diagram, developers pull images from the Docker public registry (Docker Hub) then push and pull images from their own Docker private registry. In the development environment, each environment will be triggered by a mechanism to pull images there and run them.

To check that our Docker daemon is allowed to interact with a Docker registry insecurely over the non-encrypted HTTP, we do docker info then grep for the Registries keyword.

Please note that the insecure Docker registry is not recommended for a production environment. You have been warned!
$ docker info | grep -A3 Registries
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false

OK, seeing 127.0.0.0/8 means that we are allowed to do so. We will have a local Docker registry running at 127.0.0.1:5000. Let's set it up. 

To have a local Docker registry running, just run it from the Docker registry V2 image:

$ docker container run --name=registry -d -p 5000:5000 registry:2
6f7dc5ef89f070397b93895527ec2571f77e86b8d2beea2d8513fb30294e3d10

We should check if it is now up and running:

$ docker container ls --filter name=registry
CONTAINER ID IMAGE COMMAND CREATED STATUS
6f7dc5ef89f0 registry:2 "/entrypoint.sh /e.." 8 seconds ago Up

The details of container run and other commands will be discussed again in the Running a container section.

Recall that we have built an image named my-nginx. We can check if it is still there; this time we use --filter reference to select only an image name ending with nginx

$ docker image ls --filter reference=*nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
my-nginx latest a773a4303694 1 days ago 216MB
nginx latest b8efb18f159b 2 months ago 107MB

We can also shorten the command to docker image ls *nginx. It yields the same result.

Let's tag the image. We will tag my-nginx to 127.0.0.1:5000/my-nginx so it can be pushed into our private Docker registry. We can do this using the docker image tag command (docker tag for the old-style, top-level command):

$ docker image tag my-nginx 127.0.0.1:5000/my-nginx

We can check using image ls again to see that the tag command is done successfully:

$ docker image ls 127.0.0.1:5000/my-nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
127.0.0.1:5000/my-nginx latest a773a4303694 1 days ago 216MB

OK, that looks great! We can now push the my-nginx image to the local repository, of course with docker image push, and the process will be very quick because the Docker repository is locally here on our machine.

Again, you will find that the hash number is not the same as in the following listing when you try the commands. It is harmless; please just ignore it.

Now, execute the following command to push the my-nginx image onto the local private repository:

$ docker image push 127.0.0.1:5000/my-nginx
The push refers to a repository [127.0.0.1:5000/my-nginx]
b3c96f2520ad: Pushed
a09947e71dc0: Pushed
9c42c2077cde: Pushed
625c7a2a783b: Pushed
25e0901a71b8: Pushed
8aa4fcad5eeb: Pushed
latest: digest: sha256:c69c400a56b43db695 ... size: 1569

The hard part has already been done beautifully. We now go back to the simple part: pushing an image to Docker Hub. Before we continue, please sign up for your Docker ID at https://hub.docker.com/ if you don't have one yet.

To store an image there, we have to tag the image with the <docker id>/<image name> format. For pushing my-nginx to the Docker Hub, we will image tag it to <docker id>/my-nginx. I'll use my Docker ID there. Replace <docker id> with your registered Docker ID:

$ docker image tag my-nginx chanwit/my-nginx

Before pushing, we need to log in to the Docker Hub first using the docker login command. Please use -u and your Docker ID to specify the account. We will be asked for a password; if everything is OK, the command will say Login Succeeded:

$ docker login -u chanwit
Password:
Login Succeeded

Please note that our username and password are insecurely stored in ~/.docker/config.json, so please do not forget to type docker logout whenever possible.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset