Introduction

When you start a container from the Base image using Docker, 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 the 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, create, delete and restart a container with it.

Docker permits you to create the image in the following ways:

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

In this tutorial we will create Ubuntu instance and host Website running under Nginx Web Server using an interactive shell on Ubuntu 18.04.

Prerequisites

To follow this guide, you'll need a fresh installed Ubuntu 18.04 server, a regular, non-root user with sudo permission, enable a basic firewall to block non-essential ports, 2GB of memory and a processor with 2 cores.

When you have an account available, log in as your non-root user to begin.

Getting Started

First, you will need to update the server with the latest version. You can it them by running the following command:

$$

sudo apt-get update -y sudo apt-get upgrade -y

Once the server is updated, restart it to update all the changes.

Next, you will also need to install some required packages to your server. You can install all of them with the following command:

$

sudo apt-get install -y apt-transport-https software-properties-common ca-certificates curl openssl wget

Install Docker

Next, you will need to install Docker on your server. By default, the latest version of Docker is not available in the Ubuntu 18.04 server default repository. So, you will need to add the repository for that.

First, download and add Docker CE GPG key with the following command:

$$

wget https://download.docker.com/linux/ubuntu/gpg sudo apt-key add gpg

Next, add the Docker CE repository to APT with the following command:

$

sudo nano /etc/apt/sources.list.d/docker.list

Add the following line:

/etc/apt/sources.list.d/docker.list

deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable

Save and close the file, when you are finished. Then, update the repository with the following command:

$

sudo apt-get update -y

Once the repository is updated, install Docker CE with the following command:

$

sudo apt-get install docker-ce -y

After installing Docker CE, check the Docker service with the following command:

$

sudo systemctl status docker

You should see the following output:

        

● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2019-07-5 03:12:11 UTC; 1min 2s ago Docs: https://docs.docker.com Main PID: 3477 (dockerd) Tasks: 8 CGroup: /system.slice/docker.service └─3477 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

July 17 05:12:12 ubuntu1804 dockerd[3477]: time="2019-07-5T03:12:24.075302742Z" level=warning msg="Your kernel does not support swap memory lim July 17 05:12:12 ubuntu1804 dockerd[3477]: time="2019-07-5T03:12:24.075970607Z" level=warning msg="Your kernel does not support cgroup rt perio July 17 05:12:12 ubuntu1804 dockerd[3477]: time="2019-07-5T03:12:24.076338523Z" level=warning msg="Your kernel does not support cgroup rt runti July 17 05:12:12 ubuntu1804 dockerd[3477]: time="2019-07-5T03:12:24.085407732Z" level=info msg="Loading containers: start." July 17 05:12:12 ubuntu1804 dockerd[3477]: time="2019-07-5T03:12:24.882504663Z" level=info msg="Default bridge (docker0) is assigned with an IP July 17 05:12:12 ubuntu1804 dockerd[3477]: time="2019-07-5T03:12:25.195655181Z" level=info msg="Loading containers: done." July 17 05:12:12 ubuntu1804 dockerd[3477]: time="2019-07-5T03:12:25.625414313Z" level=info msg="Docker daemon" commit=481bc77 graphdriver(s)=ov July 17 05:12:12 ubuntu1804 dockerd[3477]: time="2019-07-5T03:12:25.628379636Z" level=info msg="Daemon has completed initialization" July 17 05:12:12 ubuntu1804 systemd[1]: Started Docker Application Container Engine. July 17 05:12:12 ubuntu1804 dockerd[3477]: time="2019-07-5T03:12:25.770575369Z" level=info msg="API listen on /var/run/docker.sock"

Create a Docker Instance

First, you will need to pull Ubuntu image from the Docker Hub before starting anything.

You can easily pull Ubuntu image from the Docker public registry with the following command.

$

sudo docker pull ubuntu

Once the Ubuntu image has been downloaded, you should see the following output:

       

Using default tag: latest latest: Pulling from library/ubuntu 6adf03819f3e: Pull complete 0573fgd463211: Pull complete 0du67c50as3e: Pull complete Digest: sha256:f08sjajsd75ldj90065187e7eabdfac3c96e5ff0f6b2fs2k4ddi84hjjd Status: Downloaded newer image for ubuntu:latest

Next, 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

You should see the following output:

 

root@13246cfc6417:/#

Install Nginx on running Container

Now, after Ubuntu base image with instance is ready, you can easily install Nginx Server interactively for it. To do so, you will need to run following command in a terminal.

$$

sudo apt-get update -y sudo apt-get install nginx -y

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

$

exit

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

$

sudo docker ps -a

You should see the following output:

   

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 13246cfc6417 ubuntu "bash" 3 minutes ago Exited (0) 8 seconds ago stoic_williamson ff2deb4f97b1 ubuntu "/bin/bash" 30 hours ago Exited (0) 30 hours ago gifted_wiles

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

$

sudo docker commit 13246cfc6417 ubuntu-nginx

The output looks something like this:

 

sha256:3d880d3d819eaf383f454da974602878a4354f99cd5eeb30a4c89b5cfas3485ds

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

$

sudo docker images

You can see the ubuntu-nginx image listed below:

   

REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu-nginx latest 7b280d3d819e 18 seconds ago 191MB ubuntu latest 7698f282e524 2 weeks ago 69.9MB

Add the Website Contents to the ubuntu-nginx Image

Now, you have a new image that contains a Nginx Web server. Next, you will need to create 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 a index.html file on host system and add it to a tarball called testsite.tar in current directory:

$$

mkdir -p docker/testsite cd docker

$

nano testsite/index.html

add the following content:

testsite/index.html

<html> This is my first Nginx Web Server </htm>

Save and close the file

Now, compress the website directory using tar:

$

tar -cvf testsite.tar testsite

Now, create Dockerfile to add the web site content to the ubuntu-nginx image and launch Nginx on port 80:

$

nano dockerfile

Add the following content

dockerfile

FROM ubuntu-nginx ADD testsite.tar /tmp/ RUN mv /tmp/testsite/* /var/www/html/ EXPOSE 80 CMD ["/usr/sbin/nginx" "-g" "daemon off;"]

Save and close the file.

In above dockerfile, the website content in testsite.tar will get automatically extracted to /tmp/ folder. Then, the entire site will move to the Nginx root directory /var/www/html/ and the expose 80 will open port 80 so that the website will be available normally. Then, the entrypoint is set to /usr/sbin/nginx so that the Nginx Server will execute.

Create and Run Container using Dockerfile

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

To do this, Run the following command:

$$

cd docker sudo docker build -t testsite .

You should see the following output:

                     

Sending build context to Docker daemon 14.34kB Step 1/6 : FROM ubuntu-nginx ---> 7b280d3d819e Step 2/6 : ADD testsite.tar /tmp/ ---> 697007c7e514 Step 3/6 : RUN mv /tmp/testsite/* /var/www/html/ ---> Running in 68097dd4b53b Removing intermediate container 68097dd4b53b ---> 0630c6e738a8 Step 4/6 : EXPOSE 80 ---> Running in 00cc1a93ae16 Removing intermediate container 00cc1a93ae16 ---> 24efe8462fba Step 5/6 : ENTRYPOINT [ "/usr/sbin/nginx" ] ---> Running in 19f560394a01 Removing intermediate container 19f560394a01 ---> Running in 564d4b993f07 Removing intermediate container 564d4b993f07 ---> e964ce72b333 Successfully built e964ce72b333 Successfully tagged testsite:latest

After image has been build, you can now proceed by creating a container running Nginx instance on it.

$

sudo docker run -d -P testsite

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 216063fc335d testsite "/usr/sbin/nginx…" 19 seconds ago Up 14 seconds 0.0.0.0:32768->80/tcp pensive_cori

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

$

curl localhost:32768

or

$

curl "Container IP Address":80

You should see the Nginx Web page you have created earlier:

 

This is my first Nginx Web Server

Conclusion

Congratulations! you have successfully host web site using Nginx web server on Docker container using an interactive shell. I hope you can now easily install anything inside Docker container with an interactive shell.