Managing images using Docker CLI
The first step in running and using a container on your server or laptop is to search and pull a Docker image from the Docker registry using the docker search
command.
Let's search for the web server container. The command to do so is:
$ docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd ... 1569 [OK]
hypriot/rpi-busybox-httpd ... 40
centos/httpd 15 [OK]
centos/httpd-24-centos7 ... 9
Alternatively, we can go to https://hub.docker.com/ and type httpd
in the search window. It will give us something similar to the docker search httpd
results:
Docker Hub search results
Once the container image is found, we can pull this image from the Docker registry in order to start working with it. To pull a container image to your host, you need to use the docker pull
command:
$ docker pull httpd
The output of the preceding command is as follows:
Note that Docker uses concepts from union filesystem layers to build Docker images. This is why you can see seven layers being pulled from Docker Hub. One stacks up onto another, building a final image.
By default, Docker will try to pull the image with the latest tag, but we can also download an older, more specific version of an image we are interested in using different tags. The best way to quickly find available tags is to go to https://hub.docker.com/, search for the specific image, and click on the image details:
Docker Hub image details
There we are able to see all the image tags available for us to pull from Docker Hub. There are ways to achieve the same goal using the docker search
CLI command, which we are going to cover later in this book.
$ docker pull httpd:2.2.29
The output of the preceding code should look something like the following:
You may notice that the download time for the second image was significantly lower than for the first image. It happens because the first image we pulled (docker:latest
) has most layers in common with the second image (httpd:2.2.29
). So there is no need to download all the layers again. This is very useful and saves a lot of time in large environments.
Now we want to check the images available on our local server. To do this, we can use the docker images
command:
$ docker images
The output of the preceding command will be as shown in the following screenshot:
If we downloaded a wrong image, we can always delete it from the local server by using the docker rmi
command: ReMove Image (RMI). In our case, we have two versions of the same image, so we can specify a tag for the image we want to delete:
$ docker rmi httpd:2.2.29
The output of the preceding command will be as shown in the following screenshot:
At this point, we have only one image left, which is httpd:latest
:
$ docker images
The output of the preceding command will be as shown in the following screenshot:
Saving and loading images
The Docker CLI allows us to export and import Docker images and container layers using export/import or save/load Docker commands. The difference between save/load and export/import is that the first one works with images including metadata, but the export/import combination uses only container layers and doesn't include any image metadata information such as name, tags, and so on. In most cases, the save/load combination is more relevant and works properly for images without special needs. The docker save
command packs the layers and metadata of all the chains required to build the image. You can then load this saved images chain into another Docker instance and create containers from these images.
The docker export
will fetch the whole container, like a snapshot of a regular VM. It saves the OS, of course, but also any change a you made and any data file written during the container life. This one is more like a traditional backup:
$ docker save httpd -o httpd.tar
$ ls -l httpd.tar
To load the image back from the file, we can use the docker load
command. Before we do that, though, let's remove the httpd image from the local repository first:
$ docker rmi httpd:latest
The output of the preceding command will be as shown in the following screenshot:
We verify that we do not have any images in the local repository:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
Load the image file we previously saved with the docker save
command. Like docker export
and docker import, this command forms a pair with Docker save and thus is used for loading a saved container archive with all intermediate layers and metadata to the Docker cache:
$ docker load -i httpd.tar
The output of the preceding command will be as shown in the following screenshot:
Check the local docker images with docker image
command:
$ docker images
The output of the preceding command will be as shown in the following screenshot:
Uploading images to the Docker registry
Now we know how to search, pull, remove, save, load, and list available images. The last piece we are missing is how to push images back to Docker Hub or a private registry.
To upload an image to Docker Hub, we need to do a few tricks and follow these steps:
$ docker login
Username: #Enter your username here
Password: #Enter your password here
Login Succeeded
- Copy the Docker image you want to push to a different path in the Docker repository on your server:
$ docker tag httpd:latest flashdumper/httpd:latest
Note
Note that flashdumper
is your Docker Hub username.
- Finally, push the copied image back to Docker Hub:
$ docker push flashdumper/httpd:latest
The output of the preceding command will be as shown in the following screenshot:
Now the image is pushed to your Docker Hub and available for anyone to download.
$ docker search flashdumper/*
The output of the preceding command will be as shown in the following screenshot:
You can check the same result using a web browser. If you go to https://hub.docker.com/ you should be able to see this httpd
image available under your account:
Docker Hub account images