Configure Web Server and Python Interpreter using Container — Docker

Rahulbhatia1998
5 min readNov 1, 2020

What is Docker?

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

Basic Docker Workflow

In this task, I am going to explain you the basic functionality of docker container and setting up a Web server on Docker.

Tasks:

  • Configure WebServer on the top of Docker Container
  • Setting up Python Interpreter and running Python Code on Docker Container

My Docker Host is set up on Ubuntu 20.04

Task 1:

For this task, I am setting up an Apache HTTP Web Server on top of the docker Container.

For this, I am using a Centos Image.

  1. Starting the Docker Container :

docker run -it — name WebServer centos:latest

The above command will start the docker container named WebServer for you

Note: The docker run command would internally check with the docker daemon service if the centos image is present , if not the docker pull command is run, and the image is downloaded.

Starting the Docker Container

breaking down the options:

[ — name ] — This option is used for assigning a name to the container.

[ -it ] — This option gives an interactive terminal to our container to run any command.

You can confirm if the container is running using the following command.

docker ps

docker ps gives information about all running containers

Note: If you want to see all the containers (whether running / stopped), you can view it through the following command.

docker ps -a

Now the basic steps to install and setup any web server are as follows:

  1. Install the webserver : We can use yum to install the web server
  2. Configure the WebServer: After you have installed the webserver, now you can configure it by going to this directory.
    /var/www/html
    Here you can add your respective HTML files that required to set up based on business requirement.
  3. Run the Webserver

Installing the Webserver :

yum install httpd

2. Configuring the Web Server:

3. Running the Web Server

Normally we use systemctl / service command to start any service.

But docker does not support this,

Hence we directly have to execute the file here.

/usr/sbin/httpd

This will start the service and creates a respective pid file in the var/run/httpd directory.

You can confirm if the server is running using the following command.

netstat -tnlp

Here we can see the httpd server is running on port 80

The result is

Note: But there is a small disadvantage to this , if we stop the container the process stops, and after you restart the container, the server does not start on its own.

The following error comes in

port 80: Connection refused

This is because the server is stopped since docker container was stopped,

We can make use of a file like .bashrc to make the server start whenever the container starts.

The use of .bashrc file is due to the fact that it gets executed when you open the bash shell/terminal

Due to this we can place the commands in this file, so that they get executed when the container starts

rm -rf /var/run/httpd/*

This is done to remove/clear cache data present in the directory from the previous session and as well as remove the .pid file which lets the OS assume that the process (httpd server) is running.

Now you can restart your docker container.

As you can see the httpd server has started and the port 80 is open

And the Web page is displayed.

Now if you want to create a copy of this container, and make an image out of it we can use docker commit.

docker commit <container_name> <image_name:tag>

And a new image is created for you , which you can make use of for future purposes.

Note: Make sure that the container is in stopped state before you do a docker commit and make an image out of it.

Task 2:

  • Setting up Python Interpreter and running Python Code on Docker Container

for this I created another container from centos base os image

docker run -it — name Python3 centos

and you can install python3 using

yum install python3

The python3 interpreter is now setup.

Thanks for reading :)

You can connect with me on Linkedin here , if you have any suggestions.

--

--