• Get In Touch
May 16, 2016

How to Create a Docker Container Using an Interactive Shell

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

When you start a container in Docker from the Base 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. This read-write layer, information of its Parent Image, networking configuration, resource limits and unique id is called a Container.

Containers has two states running state and exited state. In running state container includes a tree of processes running on the CPU, isolated from the other processes running on the host. 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 allows you to build the image in one of the following two ways:

  1. Interactively launch BASH shell under Ubuntu Base image, install Apache and its dependencies, and then save the image.
  2. Build the image using Dockerfile.

In this tutorial we will build an Ubuntu instance and host a Website running under Apache Web Server using an interactive shell.

Requirements

Ubuntu Server 14.04 with Docker installed on your system.

Running a Docker Instance

First of all, you will need to pull the Ubuntu image before starting anything.

You can easily pull the Ubuntu image from the Docker public registry hub.

To download the Ubuntu image, Run:

sudo docker pull ubuntu

Once the Ubuntu image has been download, You can create an Ubuntu instance in a Docker container and attach a bash shell by running the following command:

sudo docker run -i -t ubuntu bash

Installing Apache

Now, after the Ubuntu base image with instance is ready, you can easily install the Apache Server interactively for it.

To do so, you will need to run following command in a terminal.

apt-get update -y
apt-get install apache2 apache2-utils -y

After installation the installation is finished, you can exit from the current shell by running the following command:

exit

Saving the Image

Now, you can save the changes you made into the Ubuntu instance. To do that, first you will need the Container ID of running Ubuntu instance.
To get that, Run:

sudo docker ps -a

Now, save the changes as a new image with name ubuntu-apache by running the following command:

sudo docker commit df888c72ed5a ubuntu-apache

The output looks something like this:

    sha256:00c9fc0494a2953ab8b01ff9ae6471eaaccfa8860d669c1f0da13cca3a5cd886

You can see that the changes are saved using Container ID and image name ubuntu-apache.
To verify the new image is running or not, Run:

sudo docker images

You can see the ubuntu-apache image listed below:

    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    ubuntu-apache       latest              00c9fc0494a2        43 seconds ago      202.6 MB
    ubuntu              latest              3876b81b5a81        3 weeks ago         187.9 MB
    chef/ubuntu-14.04   latest              f1f7972ced25        17 months ago       386.6 MB


Adding the Website Contents to the ubuntu-apache image

Now, you have a new image that contains a Apache Web server. You can build a Dockerfile based on that image and add the necessary files. Given the relative path to a tarball of the site content, Docker automatically untars or unzips the files in a source tar or zip file into the target directory.

To do this, you need to create an index.html file on the host system and add it to a tarball called website.tar in current directory:

mkdir website
nano website/index.html

add the following content:


Sample Apache Web Page

Save and close the file.

Now, compress the website directory using tar:

sudo tar -cvf website.tar website

Now, create a Dockerfile to add the web site content to the ubuntu-apache image and launch Apache on port 80:

nano Dockerfile

Add the following content –

    FROM ubuntu-apache
    ADD website.tar /tmp/
    RUN mv /tmp/website/* /var/www/html/
    EXPOSE 80
    ENTRYPOINT [ "/usr/sbin/apache2ctl" ]
    CMD [ "-D", "FOREGROUND" ]

Save and close the file.

In the above Dockerfile, the website content in website.tar will be automatically extracted to /tmp/ folder. Then, the entire site will move to the Apache root directory /var/www/html/ and the expose 80 will open port 80 so that the website will be available as normal. Then, the entrypoint is set to /usr/sbin/apache2 so that the Apache Server will execute.

Building and running a Container

Now, you will build a Container using the Dockerfile you just created in order to add website on it.

To do this, run the following command:

sudo docker build -t website .

You should see the following output:

    Sending build context to Docker daemon 17.92 kB
    Step 1 : FROM ubuntu-apache
     ---> 00c9fc0494a2
    Step 2 : ADD website.tar /tmp/
     ---> Using cache
     ---> be3aeba8dc4e
    Step 3 : RUN mv /tmp/website/* /var/www/html/
     ---> Using cache
     ---> 761bc3d9ba23
    Step 4 : EXPOSE 80
     ---> Using cache
     ---> 51d11f0a2b38
    Step 5 : ENTRYPOINT /usr/sbin/apache2ctl
     ---> Using cache
     ---> dfb777849e98
    Step 6 : CMD -D FOREGROUND
     ---> Using cache
     ---> 68215963e648
    Successfully built 68215963e648

After the image has been built, you can now proceed by creating a container running the Apache instance on it.

sudo docker run -d -P website

Now, use the “docker ps” command to determine the port activated and then use curl to inspect the sample content.

sudo docker ps

You should see the following output:

    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
    45dfb7cf3ef1        website             "/usr/sbin/apache2ctl"   12 seconds ago      Up 10 seconds       0.0.0.0:32775->80/tcp   nauseous_euclid

Now, verify your Apache Web server by running the following command:

curl localhost:32775

or

curl "Container IP Address":80

You should see the Apache Web page you have created earlier:
“` language-bash

Sample Apache Web Page
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 […]