How to Create Docker Container using Dockerfile

This article is all about how we can use a Dockerfile to create a Docker Container. So, here is a brief introduction on what really is a Container and a Dockerfile.

Once we start a process in Docker from an Image, Docker fetches the image and its Parent Image, and repeats the process until it reaches the Base Image. Then the Union File System adds a read-write layer on top. That read-write layer, the information about its Parent Image and some other information like its unique id, networking configuration, and resource limits is called a container. Containers has states as they can change from running to exited state. A container with state as running includes a tree of processes running on the CPU, isolated from the other processes running on the host where as exited is the state of the file system and its exit value is preserved. You can start, stop, and restart a container with it.

Docker filesystems Busyboxrw

Dockerfile is a script/text file, composed of various commands and arguments listed successively to automatically perform actions on a base image in order to create  a new one. It help us to avoid issuing the command everytime while running container. They are used for organizing things and greatly help with deployments by simplifying the process from start-to-finish. It begins with defining an image FROM which the build process starts and provides a new image with required commands and arguments which is to be used for creating docker containers.

So, here's a quick and easy steps on how we can use a Dockerfile to Create a Docker Container.

1. Docker Commands

So, before we start creating our Dockerfile, we need to learn the necessary commands to create a working Dockerfile. There are dozens of commands that can be used to create it but we'll quick learn about those commands.


Add command is used to copy the files from a source directory to destination, the source can also be an url. If its an url, the command will simply download the target to the destination directory.


CMD command is used to execute specific commands that gets executed with the creation of containers based on the image. It is very similar to RUN command whereas CMD is not executed during build.


ENTRYPOINT argument sets the concrete default application that is used every time a container is created using the image.


ENV is used to set the environment variables. These variables can be accessed with the docker container which  offers an enormous amount of flexibility for running programs..


FROM argument defines the base image to use to start the build process which can be any docker image whether its on the host or in the repository.


The WORKDIR directive is used to set where the command defined with CMD is to be executed.


RUN is the command which is used to build the docker images which is a centrally executing directive for the Dockerfiles.


MAINTAINER is the command which doesn't get executed but declares the author, hence setting the author field of the images.


The USER directive is used to set the UID (or username) which is to run the container based on the image being built.


The VOLUME command is used to enable access from your container to a directory on the host machine.


The EXPOSE command is used to associate a specified port to enable networking between the running process inside the container and the host.

 2. Creating a Dockerfile

After we learned about the necessary commands to build our required container, we'll finally get our hands dirty with creating a Dockerfile to do our job done. Here, we'll create a Dockerfile to create an image to install Nginx Web Server container. For that, we'll need to create a file named Dockerfile with our favorite text editor.

$ sudo nano Dockerfile

Then, we'll want to add our Docker configuration which includes the commands and arguments for the Nginx Web Server Container.

# Pull base image.
FROM dockerfile/ubuntu

# Install Nginx.
add-apt-repository -y ppa:nginx/stable && \
apt-get update && \
apt-get install -y nginx && \
rm -rf /var/lib/apt/lists/* && \
echo "\ndaemon off;" >> /etc/nginx/nginx.conf && \
chown -R www-data:www-data /var/lib/nginx

# Define mountable directories.
VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/etc/nginx/conf.d", "/var/log/nginx", "/var/www/html"]

# Define working directory.
WORKDIR /etc/nginx

# Define default command.
CMD ["nginx"]

# Expose ports.
Nginx Image Dockerfile

After getting the files with the above commands, we'll want to save the file and exit towards running that Dockerfile.

3. Building an Image using Dockerfile

Now, after we finish creating our Dockerfile for the Nginx Web Server Container, we'll now finally build a image using that Dockerfile. To build an image, we'll need to run the following command in our working base directory.

# sudo docker build -t my_nginx .
Building Nginx Image

The above command will build an image named my_nginx using the Dockerfile created.

4. Creating a Docker Container

Using the image we just built, we'll now proceed to the final step of creating a container running a Nginx Web Server instance inside, using a name of our choice. Here its my_nginx_instance.

# docker run --name my_nginx_instance -p 80:80 -d my_nginx
Running Docker Container

Finally, we have created our Nginx Container and is forwarded to port 80. Now to check if its running properly or not we can run docker ps .

# docker ps
Listing Docker Containers


In this tutorial, we learned how we can create and use a Dockerfile to create a Docker Container. Docker Container is easy to build after we figure out what should go inside the Dockerfile. Dockerfile is the most important part in building the container because it is responsible for the configuration in the container. So, if you have any questions, suggestions, feedback please write them in the comment box below so that we can improve or update our contents. Thank you ! Enjoy :-)

Leave a Comment