Configuring APACHE/HTTPD Server, Setting up Python Interpreter and, running Python Code on Docker Container

In this article, we will:

1.Configure Apache2/HTTPD Server on Docker Container

2. Set Up a Python Interpreter, and run our Python Code on Docker Container.

3. Commit the changes in the container and create a Docker Image.

4. Push the Image to the Docker Hub.


Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers. This is often described as containerization. Putting applications into containers leads to several advantages like:

  • Docker containers are always portable. This means that you can build containers locally and deploy containers to any docker environment (other computers, servers, cloud, etc …)
  • Containers are lightweight because containers are sharing the host kernel (the host operating system) but can also handle the most complex applications.

Containers vs. Virtual Machines

When talking about containerization it is very often compared to virtual machines. Let’s take a look at the following image to see the main difference:

The Docker container platform is always running on top of the host operating system. Containers are containing the binaries, libraries, and the application itself. Containers do not contain a guest operating system which ensures that containers are lightweight.

In contrast, virtual machines are running on a hypervisor (responsible for running virtual machines) and include its own guest operating system. This increased the size of the virtual machines significantly, makes setting up virtual machines more complex, and requires more resources to run each virtual machine.


First of all, you need to make sure that Docker is installed on your system. For the following tutorial, we’ll assume that the Docker Community Edition (CE) is installed.

Docker CE is available for all major platforms including macOS, Windows, and Linux. The specific steps needed to install Docker CE on your system can be found at

Furthermore, you should make sure to create a free account at, so that you can use this account to sign in to the Docker Desktop application.

Once Docker is installed and running on your system we’re able to start by entering the following command on the terminal:

$ docker version

$ docker info

The output gives you detailed information about the installed version and more information about Docker Container Engine running on your machine.

Step 1: Pull the Docker Image from the Docker Hub

For the list of already existing Docker images go to

For this, we will be using Docker Image of Ubuntu Linux, Version 14.04. You can also use Centos Docker Image.

To list the Docker Images present in your local machine, you can run this command:

$ docker images

Pull the Ubuntu Linux, Version 14.04 Docker Image from the Docker Hub

$ docker pull ubuntu:14.04

Step 2: RUN/ Start the Docker Container

The docker run command lets you run any Docker image as a container.

$ docker run -it --name mywebserver-httpd -p 8010:80 ubuntu:14.04

  • -it: executes the container in interactive mode (not in the background).
  • -p 8010:80: by using the -p option we're connecting the internal port 80 of the container to the external port 8010. Because the Apache2 Webserver by default is running on port 80. We're then able to send the HTTP requests to the server from our local machine by opening up URL http://[IP_HOST_OS]:8010.

Now we are landed inside the Docker Container.

Step 3: Install Apache2 Webserver and Start the Apache2 Service

Run the following commands to install the Apache2 Webserver and starting the Apache2 Service in this Docker Container.

$ apt-get update

$ apt-get install -y apache2

$ service apache2 start

$ service apache2 status

Step 4: Enable Apache2 Service

By default, Apache2 Service is not enabled permanently, which means that whenever you restart the container or boot up the O.S, the Apache2 Service will not start automatically. You need to manually start the Apache2 service, each time you boot up the O.S or start the container using this command:

$ service apache2 restart

But we can overcome this challenge using some Linux Concepts. In this way, the Apache2 service will be enabled permanently. To achieve this go through these steps:

$ apt-get install -y vim

$ vim /root/.bashrc

Add these lines to this file

If you want to use Centos Image then use these commands:

$ docker pull centos:latest

$ docker run -it --name mywebserver-httpd -p 8010:80 centos:latest

$ yum -y update

$ yum install -y httpd

NOTE: systemctl command doesn’t work in the docker containers. So we need to write the path of the command to start the httpd service.

$ /usr/sbin/httpd

In order to enable httpd service permanently:

$ yum install -y vim

$ vim /root/.bashrc

Step 5: View the Apache2 Webserver Default Page

Now we can send the HTTP requests to the server from our local machine by opening up URL http://[IP_HOST_OS]:8010.

Replace [IP_HOST_OS] with the IP of your local machine/ Host O.S on which Docker Container Engine is running.

Step 6: Install Python3 Interpreter and Pip

To install python3 Interpreter and Pip, run these commands in your Docker Container:

$ apt-get install -y python3

$ apt-get install -y python3-pip

$ pip3 list

Now we are going to create a Python program and run it on this Docker Container.

$ vim

$ python3

Step 7: Commit this Docker Image

docker commit

This command is useful to create a new image from changes to a container, it’s a simple as running just one command.

In this way, all the changes like Installing and Configuring packages that you have done in the container can be saved permanently by committing the change in this container to a new Docker Image.

docker commit [id_of_container] [repository/image_name:tag]


[id_of_container] with the container id of the container launched earlier ( in which we made the changes)

[repository/image_name:tag] repository is the same as your docker hub username, image_name refers to the name that you want to keep to this new Docker Image. By default, the tag is latest. But you can change it according to your need.

For my case:

Step 8: Push this Docker Image to the Docker Hub

In order to push the Docker Image to the Docker Hub.

First, you need to login to the Docker Hub and then push the image using these command:

$ docker login
$ docker push mtabishk/apache2-python3

Now you can visit to view your Docker Image

This Docker image can be used by anyone. It will work in the same way as other Docker Images.

Let’s now use this image to lunch a container and run the python program directly without even going inside the container.

$ docker run -i mtabishk/apache2-python3 python3

That’s all for today! I’ll be back with some new articles very soon, thanks!

Muhammad Tabish Khanday


Peace ✌




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Running a successful Hackathon— From Start to Finish

Deploy webserver on AWS through Ansible

1.Problem: Write a SQL query to find employees who have the highest salary in each of the…

How to Revoke WhatsApp Group Link?

You Can Know More about MemoryFoam

4 Key Kubernetes Features for Virtualized MicroServices Configuration

Haskell CheatSheet-(Types & Polymorphism)

Backpressure in Kotlin Flows

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Muhammad Tabish Khanday

Muhammad Tabish Khanday

Lets Code 💻

More from Medium

What is Docker?

Mock AWS DynamoDB service for unit testing in Python

Set up configs like memory limits in Docker for Windows and WSL2

OpenID connect using Python Flask