• Get In Touch
May 16, 2016

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.

Requirements

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

Note:

-> 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        0.0.0.0:80->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.

Enjoy……

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

Share this Article!

Related Posts

Node.js Authentication – A Complete Guide with Passport and JWT

Node.js Authentication – A Complete Guide with Passport and JWT

Truth be told, it’s difficult for a web application that doesn’t have some kind of identification, even if you don’t see it as a security measure in and of itself. The Internet is a kind of lawless land, and even on free services like Google’s, authentication ensures that abuses will be avoided or at least […]

Node.js and MongoDB: How to Connect MongoDB With Node

Node.js and MongoDB: How to Connect MongoDB With Node

MongoDB is a document-oriented NoSQL database, which was born in 2007 in California as a service to be used within a larger project, but which soon became an independent and open-source product. It stores documents in JSON, a format based on JavaScript and simpler than XML, but still with good expressiveness. It is the dominant […]

Using MySQL with Node.js: A Complete Tutorial

Using MySQL with Node.js: A Complete Tutorial

Although data persistence is almost always a fundamental element of applications, Node.js has no native integration with databases. Everything is delegated to third-party libraries to be included manually, in addition to the standard APIs. Although MongoDB and other non-relational databases are the most common choice with Node because if you need to scale an application, […]

Node.Js Vs Django: Which Is the Best for Your Project

Node.Js Vs Django: Which Is the Best for Your Project

Django and NodeJs are two powerful technologies for web development, both have great functionality, versatile applications, and a great user interface. Both are open source and can be used for free. But which one fits your project best? NodeJs is based on JavaScript, while Django is written in Python. These are two equally popular technologies […]

Nodejs Vs PHP:  Which Works Best?

Nodejs Vs PHP: Which Works Best?

Before getting into the “battle” between Node.js and PHP we need to understand why the issue is still ongoing. It all started with the increased demand for smartphone applications, their success forcing developers to adapt to new back-end technologies that could handle a multitude of simultaneous requests. JavaScript has always been identified as a client-side […]