16th May, 2016 | Tutorials |

How to Create a Docker Container using Dockerfile

Need Hosting? Try ours, it's fast, reliable and feature loaded with support you can depend on.

View Plans

Docker allows you to build containers using a Dockerfile. Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to build an image.

A Dockerfile consists of various commands and arguments listed successively to automatically perform actions on a base image in order to create a new one. It helps us to avoid issuing the command everytime while running container.

You can build the Docker image using one of the following two options:

  1. Interactively launch a BASH shell under the Ubuntu Base image, install Apache and its dependencies, and then save the image.
  2. Build the Docker image using Dockerfile with the web site included.

In this tutorial, I will explain how to create a Dockerfile, install the required Apache packages, add the necessary content and then build image.


Ubuntu Server 14.04 with Docker installed on your system.

Creating a Dockerfile

A Dockerfile is a text file that has a series of instructions to build an image. It supports a set of commands that we need to use in our Dockerfile.

There are several commands supported like FROM, CMD, ENTRYPOINT, VOLUME, ENV and many more. Each and every instruction set in the Dockerfile adds an additional layer to the image and then performs a commit.

Here, we will create a Dockerfile to create an image to install the Apache Web Server container.

To do this, we will need to create a file named Dockerfile using any text editor:

sudo nano Dockerfile

Add the following content which includes the commands and arguments for the Apache Web Server Container.

    #Pull base image
    FROM ubuntu
    #Install Apache
    RUN apt-get update -y && apt-get install apache2 apache2-utils -y

#Define default port EXPOSE 80 ENTRYPOINT [ "/usr/sbin/apache2ctl" ]

#Define default command CMD [ "-D", "FOREGROUND" ]

Now, save and close the file.

In the above Dockerfile, the first parameter FROM tells Docker what the source of our image is, in this example we're using Ubuntu image.

A RUN parameter executes a series of commands inside the image to install package. Here we are updating the Ubuntu repository and installing Apache and other dependencies.

The EXPOSE parameter set's the default Apache port 80 so that the website will be available normally. Then, the ENTRYPOINT is set to /usr/sbin/apache2ctl so that the Apache Server will execute.

Building an Image using Dockerfile

Now, after we finish creating our Dockerfile for the Apache container, we are ready to create our first Apache Web Server images with docker.

We'll need to run the following command in our current working base directory to build an image.

sudo docker build -t ubuntu:Apache_Server .

You should see the following output:

    Sending build context to Docker daemon 8.704 kB
    Step 1 : FROM ubuntu
    ---> 3876b81b5a81
    Step 2 : RUN apt-get update -y && apt-get install apache2 apache2-utils -y
    ---> Running in a7173526f87f
    ---> ea72a4d99ae3
    Removing intermediate container a7173526f87f
    Step 3 : EXPOSE 80
    ---> Running in dc180eaaf5d2
    ---> a4f48bbe6995
    Removing intermediate container dc180eaaf5d2
    Step 4 : ENTRYPOINT /usr/sbin/apache2ctl
    ---> Running in e0a28430e4b2
    ---> 77a8fc26d2f9
    Removing intermediate container e0a28430e4b2
    Step 5 : CMD -D FOREGROUND
    ---> Running in 292651b9259b
    ---> 6466197ee4df
    Removing intermediate container 292651b9259b
    Successfully built 6466197ee4df


-> The -t parameter used to tag the Docker image. The . parameter is used to specify the location of the Dockerfile that we created.

After building Apache_Server image, run docker images command:

sudo docker images | grep -i Apache_Server

You should see the Apache_Server image listed in the output as shown below:

    ubuntu              Apache_Server       6466197ee4df        2 minutes ago       224.1 MB

Creating a Docker Container

Using the image we have built, we will now proceed to create a container running an Apache instance inside, using a name of our choice. Here we will use Apache_Instance.

Run following command to create a container:

sudo docker run --name Apache_Instance -p 80:80 -d ubuntu:Apache_Server

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

sudo docker ps

You should see the following output:

    f49877210e7d        ubuntu:Apache_Server   "/usr/sbin/apache2ctl"   2 minutes ago       Up 2 minutes>80/tcp   Apache_Instance

After setting everything up, you can verify the Apache Web Server by typing the url http://your.container.ipaddress:80 or http://localhost:80 in your web browser.


Need Hosting? Try ours, it's fast, reliable and feature loaded with support you can depend on.

View Plans