In the previous article, we learned how to create docker image from a container using commit sub-command. There is one more method to achieve this task using a file called as dockerfile. It contains all tasks in a simple language which we will be discussing later in this article below. After writing a docker file, docker build sub-command is executed to create a image from the container. Lets understand the basic syntax of docker file.
“INSTRUCTION arguments”
It follows a simple patter which comprises of an instruction followed by an argument. It is not case-sensitive, however to make it appear simple, we use the instructions in capital letters. For example, a basic dockerfile appears like:
#comment
FROM ubuntu:latest
CMD echo Welcome to Ubuntu…
In the above file, FROM is the instruction and ununtu:latest is the argument. Which is clearly understandable, it means download image “ubuntu, latest version” and the run a command on that. By default, the docker build looks in the Docker host for the images, but in case its not there, it will going to pull one from the Docker Hub Registry. Multiple FROM can be used in a single docker file, but that is not a good practice to follow. Some of the other common instructions are as follows:
- MAINTAINER: Used to mention the author’s details, such as name and email address.
- COPY: Used to copy the files from docker host to the file system of the new image. COPY <src>…<dst>
- ADD: Similar as COPY, but additionally can be used to handle TAR files and remote URLs. In this way, we can copy multiple files by creating a tar and then using the ADD instruction.
- ENV: Used to set an environment variable. ENV <key> <value>
- USER: Used to set the startup user in the new image. USER <UID|UNAME>
- WORKDIR: Used to set the Working directory in the new image. WORKDIR <dirpath>.
- VOLUME: Used to create a mount point in the new image.
- EXPOSE: Used to open the container network port for communication with the Internet. EXPOSE 8080
- RUN: Used to run multiple commands.
- CMD: Similar to RUN, the major difference is that it will execute the command after the container will be launched, whereas with RUN it executes the commands during the build time.
- ENTRYPOINT: Similar to CMD, the only difference is that after running the command, the container will exit.
Creating Docker File: Lets take a scenario and learn building a image. The below example will install apache package on a base image of Ubuntu.
1. Create a dockerfile and build the image using docker build:

Note: We need to run the docker build command from the directory where we have stored the dockerfile.
2. Run the container using command:
#sudo docker -it -name apache apache.demo bash
3. check the version and apache to confirm. Remeber, this image was created using dockerfile.

Conclusion: In the above article, we learned creating a image using dockerfile. If you want to explore more on writing docker file, please refer below link https://docs.docker.com/articles/dockerfile_best-practices/.
Pingback: Managing Containers in Docker. | Bag Of Tricks!