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 https://docs.docker.com/install/.
Furthermore, you should make sure to create a free account at https://hub.docker.com, 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 hub.docker.com
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
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.
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 first.py
$ python3 first.py
Step 7: Commit this Docker Image
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 hub.docker.com 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 first.py
That’s all for today! I’ll be back with some new articles very soon, thanks!
Muhammad Tabish Khanday