Docker images are the read-only templates from which a container is created. In previous sections, we have seen how to pull and list the docker images. Let us learn how to build them.
A lot of Docker's command line sub-commands are inspired by Git, including this one. Docker provides a very simple way to create images. The workflow goes like this:
$ sudo docker commit <container_id> <optional_tag>
While this presents a very simple way to create images, this is not a good way to do so. Creating images this way is not very reproducible and hence not recommended.
Docker provides an easy way to build images from a description file. This file is known as Dockerfile. A simple Dockerfile will look like this:
FROM centos MAINTAINER Aditya Patawari <firstname.lastname@example.org> RUN yum -y update && yum clean all RUN yum -y install httpd EXPOSE 80 CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
Dockerfile supports various commands. I have used a few of them and I am going to describe them below:
A comprehensive list and description of all the supported commands can be found in the documentation. Let us create a file with the above lines and build an image from this.
$ sudo docker build -f <path_of_dockerfile> .
Images created this way document the steps involved clearly and hence using Dockerfile is a good way to build reproducible images. It is also worth nothing that each line in Dockerfile create a new layer in Docker image. So often, we will club statements using and operator (&&) like this:
RUN yum -y update && yum clean all