Usually, those who use Docker containers expect to have a high-level of automation, and the docker commit command is difficult to automate. Luckily, Docker can build images automatically by reading instructions from a special file usually called a Dockerfile. A Dockerfile is a text document that contains all the commands a user can call on the command line to assemble an image. Using docker build, users can create an automated build that executes several command-line instructions in succession. On CentOS 7, you can learn a lot more using the Dockerfile built-in documentation page man Dockerfile.
A Dockerfile has a number of instructions that help Docker to build an image according to your requirements. Here is a Dockerfile example, which allows us to achieve the same result as in the previous section:
$ cat Dockerfile FROM httpd RUN echo "This is a custom image" > /usr/local/apache2/htdocs/index.html
Once this Dockerfile is created, we can build a custom image using the docker build command:
$ docker build -t custom_image2 . Sending build context to Docker daemon 2.048 kB Step 1 : FROM httpd ---> 01154c38b473 Step 2 : RUN echo "This is a custom image" > /usr/local/apache2/htdocs/index.html ---> Using cache ---> 6b9be8efcb3a Successfully built 6b9be8efcb3a
Please note that the . at the end of the first line is important as it specifies the working directory. Alternatively, you can use ./ or even $(pwd). So the full commands are going to be:
docker build -t custom_image2 .
or
docker build -t custom_image2 ./ or
docker build -t custom_image2 $(pwd)
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE custom_image2 latest 6b9be8efcb3a 2 minutes ago 177.4 MB custom_image latest ffd3a523f984 19 minutes ago 177.4 MB docker.io/httpd latest 01154c38b473 2 weeks ago 177.4 MB