The Complete Magazine on Open Source

Running a Web Server Inside a Docker Container

, / 513 0

cargo ship full of containers

There are problems with setting up Docker when it is operated behind a proxy. This article explains how these can be overcome.

Wikipedia defines Docker as an open source project that automates the deployment of applications inside software containers, which are nothing but isolated user space instances running on an operating system. Docker makes use of the resource isolation features provided by the underlying Linux kernel, such as cgroups and kernel namespaces, so that multiple isolated instances can run on a Linux kernel without having a hypervisor layer installed.
This article offers a few tips to overcome CentOS7 based Docker set-up problems

when it’s operated behind a proxy. It tells you how to run a Web server inside a CentOS7
based container.


Figure 1: Docker engine service status


Figure 2: Docker engine service status with dns resolution set

Setting up Docker in CentOS7, which runs behind a proxy
When you run Docker behind a proxy, you need to provide the proxy details to download the images. So let’s get on with installing and setting up Docker on a CentOS7 machine (CentOS Linux release 7.0.1406 (Core) and kernel version is 3.10.0-123.20.1.el7.x86_64) when it runs behind a proxy. The detailed steps are as follows.
1. The very first step is setting up Yum with the proxy details. Add the following lines to /etc/yum.conf:

proxy=http://<proxy IP>:<port>
proxy_username=<User Name>

2. Install Docker using yum –y install docker.
3. Start the Docker service using systemctl start docker.service.

Note: In this specific version of CentOS7, the Docker engine kept showing an error code “Failed to start Docker Application Container Engine. /usr/bin/docker: relocation error: /usr/bin/docker: symbol dm_task_get_info_with_deferred_remove, version Base not defined in file with link time reference.” The reason for the failure is that Docker has a dependency on device-mapper and the Docker version used in this specific version of CentOS7 expects the device–mapper version to be >= 1.02.90. The solution is to upgrade the device-mapper RPM using yum –y install device-mapper-event-libs.

Once device-mapper is installed, start the Docker service as suggested in Step 3. Perform chkconfig docker on to make the service start across reboots.

Note: You need to carefully check the service status. Refer to Figure 1.


Figure 3: Docker images sample output


Figure 4: Docker running processes


Figure 5: Docker commit sample output


Figure 6: Docker custom image

From the figure you can see that the lookup failure is shown, though the service is up and running. This means that your system DNS has not been set up correctly. Have a proper nameserver entered in /etc/resolv.conf and restart the services. Refer to Figure 2 which shows a proper nameserver set, so that lookup failure errors are not shown and the service status is active (running).
The next step is to pull the Centos Docker container image from the hub using the docker pull centos command. For this comand to work, the proxy details need to be set in the /etc/sysconfig/docker configuration file:

HTTP_PROXY=http://<<proxy user name>>:<<Password>>@<<Proxy IP>>:<<Proxy port>>
export HTTP_PROXY HTTPS_PROXY http_proxy https_proxy

A sample list of images downloaded to my machine is given in Figure 3.
Docker is now successfully configured and set up in your system, where the machine runs behind a proxy.

Configuring a Web server in the CentOS7 Docker container
This section details how to set up a simple Web server in the Docker container based on CentOS7.
Step 1: To begin, run the command:

~# docker run –it /bin/bash

Step 2: In the Centos Docker container terminal, set up yum.conf with the proxy details as we did for the host in Step 1 in the section Setting up Docker in CentOS7, which runs behind a proxy.
Step 3: Once /etc/yum.conf is saved with the proxy details, make sure that the container can talk to the outside world via proxy by performing a yum list command.
Step 4: To come out of the container without killing it, perform CTRL-q followed by CTRL-P. If you perform an exit command from the container, it comes out of the container and kills the container process. The intention of not killing the instance is to save the Yum setting changes that we performed inside the container.
Step 5: Save the container by executing the following command:

a. ~# docker ps

b. ~# docker commit << ID listed in docker ps command>> <<Custom Name>>

c. ~# docker images

Step 6: Create a Docker config file to set up a Web server as follows. Create a custom Docker file named Dockerfile. The content of the Docker file is shown in Figure 7.
Step 7: Build the Docker container to run the Web server by using the command docker build –t <<Custom Name>>
Step 8: Create a test page in the Web server to test it. The default DocumentRoot is /var/www/html/; hence create a test page index.html inside this folder.
Step 9: Once Step 7 is successful, run the command docker run-privileged-ti-v/sys/fs/cgroup:/sys/fs/cgroup:ro-p 80:80 mycentoswebserver.


Figure 7: Sample Dockerfile


Figure 8: Build using Dockerfile


Figure 9: Testing docker with a sample index page residing in web server

This command is to run the httpd daemon inside the container in privileged mode so that systemd is started for httpd. Without this step, the privilege is not assigned for the container and the systemd command will not run on an unprivileged

The command run in Step 9 will not be terminated and should not be stopped.
Now it’s time to test the docker environment that we created. Invoke the index page that we created in the Web server created inside the docker container as shown in Figure 9.
This article has been written with the purpose of clarifying various doubts about setting up Docker when it is operated behind a proxy. We do hope it has served its purpose. Do let us know!