This article is intended to cover the Docker DCA certification objectives around Docker Images. We will start by taking a look at what a Docker image is, and then look at some of the docker commands that are used to work with images. Let’s get started!
What are Docker Images?
Docker images are the basis of the Docker containers you create. When you run a command to create a new docker container, you always specify the image from which the container will be created. A Docker image includes everything you need to be able to run an application or OS as a container. It includes:
- code
- runtime
- libraries
- environment variables
- configuration files
Docker images are built from a DockerFile. An image is typically comprised of multiple layers – for example, the base layer would contain those files containing the OS components – Ubuntu for example. Then further layers may contain files/applications – for example, it may contain the files needed to run a web server. Each layer depends on the layer below it.
Often an image is based on another existing image in this way. For example, you may want to build an image to run an application you have written on Centos – in which case you could use an existing Centos image as your base image to build upon.
Images are read-only. When you launch a container from an image you are essentially creating a read/write layer on top of that image, which you can interact with.
Where to Find Images
So, where do you get Docker images from? You can create your own, as discussed above, by creating a dockerfile. A dockerfile is a script of instructions that define how to build a specific Docker image. A sample dockerfile to create an Apache web server might look like this:
FROM ubuntu:12.04 RUN apt-get update && apt-get install -y apache2 && apt-get clean && rm -rf /var/lib/apt/lists/* ENV APACHE_RUN_USER www-data ENV APACHE_RUN_GROUP www-data ENV APACHE_LOG_DIR /var/log/apache2 EXPOSE 80 CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]
Or, you can download a pre-existing docker image from the Docker Hub, or from a private registry that you manage. By default Docker will pull images from Docker Hub. You can find docker images by searching the docker hub site, or you can use the docker search command, for example:
$ docker search apache
This command will list docker images on the hub related to the search.
You can find out where docker images are stored on your filesystem here.
Working with Docker Images
As shown above, you can search for docker images using the docker search command. When you have found the image you need, you can download it using the docker pull command. For example:
# docker pull httpd:latest
This command will pull down the latest version of the httpd image. To list the docker images on your docker host you can use the docker image ls command:
# docker image list REPOSITORY TAG IMAGE ID CREATED SIZE httpd latest 8326be82abe6 2 days ago 166MB python latest 9038c75f5336 2 months ago 933MB nginx latest 5ad3bd0e67a9 2 months ago 127MB
As you can see from the output, I have the httpd image I have just downloaded, along with python and nginx images. You can use the docker inspect command to find out more information about the image:
# docker image inspect httpd:latest [ { "Id": "sha256:8326be82abe637e922bde6490fb056b004549e3fda8a0740c8b36dbe7618d8c0", "RepoTags": [ "httpd:latest" ], "RepoDigests": [ "httpd@sha256:13aa010584cb3d79d66adf52444494ae5db67faa28d65a1a25e6ddc57f7c0e2a" ], "Parent": "", "Comment": "", "Created": "2020-03-31T03:48:29.628379976Z", "Container": "2cd27bbade883fb6762f5ce7ef96cf34cedd7a368f5e82cb6bcdd2091333efdd", "ContainerConfig": { "Hostname": "2cd27bbade88", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "80/tcp": {} },
You can filter the output to get specific information if you already know what to look for. For example, you can list just the architecture attribute by running:
# docker image inspect httpd:latest --format "{{.Architecture}}"
You can list multiple attributes by running:
docker image inspect httpd:latest --format "{{.Architecture}} {{.Os}}"
You can also look into the history of an image by using the docker image history command:
# docker image history httpd:latest IMAGE CREATED CREATED BY SIZE COMMENT 8326be82abe6 2 days ago /bin/sh -c #(nop) CMD ["httpd-foreground"] 0B 2 days ago /bin/sh -c #(nop) EXPOSE 80 0B 2 days ago /bin/sh -c #(nop) COPY file:c432ff61c4993ecd… 138B 2 days ago /bin/sh -c #(nop) STOPSIGNAL SIGWINCH 0B 2 days ago /bin/sh -c set -eux; savedAptMark="$(apt-m… 60.9MB 2 days ago /bin/sh -c #(nop) ENV HTTPD_PATCHES= 0B 2 days ago /bin/sh -c #(nop) ENV HTTPD_SHA256=a497652a… 0B 2 days ago /bin/sh -c #(nop) ENV HTTPD_VERSION=2.4.43 0B 2 days ago /bin/sh -c set -eux; apt-get update; apt-g… 35.4MB 2 days ago /bin/sh -c #(nop) WORKDIR /usr/local/apache2 0B 2 days ago /bin/sh -c mkdir -p "$HTTPD_PREFIX" && chow… 0B 2 days ago /bin/sh -c #(nop) ENV PATH=/usr/local/apach… 0B 2 days ago /bin/sh -c #(nop) ENV HTTPD_PREFIX=/usr/loc… 0B 2 days ago /bin/sh -c #(nop) CMD ["bash"] 0B 2 days ago /bin/sh -c #(nop) ADD file:d1f1b387a158136fb… 69.2MB
This shows the history of when and how the image was created.
To delete an existing image, use the docker image rm command. For example, if I wanted to delete the httpd image I would use:
# docker image rm httpd:latest
Alternatively, to delete all images you have that you haven’t launched a currently running container from you can use the prune command:
# docker image prune
Hopefully this has given a good overview of what a Docker image is and some of the commands used to manage images. I haven’t covered building docker images here – look out for a dedicated article on that soon. Also, check out this one on how to copy a docker image, which looks at how to use the docker image load and save commands.
Learning Docker?
If you are starting out, then I highly recommend this book.
Thirsty for more?
Then it’s time to take your Docker skills to the next level with this book (It’s my favorite). Also, check out my page on Docker Certification.