Containerize A Python App using Docker

Docker is a free and open-source solution for developing, deploying, and managing apps in lightweight, OS-level virtualization. In this guide, we will demonstrate creating a Python app within a Docker container.

Prerequisites

To perform the steps demonstrated in this guide, you will need the following components:

Docker Containers

When running various programs within the same environment, things can (and will) break. The more programs you add, the more unstable it becomes. It may not be a big deal for average folks. However, when it comes to mission-critical applications, there can be serious consequences.

The more apps are introduced in the system, the bigger the attack surface. A single compromised app can easily lead to the downfall of the entire system.

To solve these issues, we can use Docker containers for software-level sandboxing:

  • Apps within the container will have limited access to files.

  • Containerized apps can’t see other processes running in the system.

  • The container can be allocated a specific amount of hardware resources.

  • Network ports of a container aren’t exposed outside.

  • Consistent packaging of almost anything across local/production environments.

To demonstrate, we are going to build a simple Python server within a Docker container, transform the container into an image, and deploy the image within a dummy production environment.

Step 1 – Filesystem Configuration

To host the project, first, we are going to create a dedicated directory:

Within the directory, create a sub-directory src to store our code:

Step 2 – Building the Python Server

In this step, we are going to create a simple HTTP server in Python. Create the file server.py:

Open it up in a text editor:

Enter the following Python code:

Here,

  • We are using the HTTPServer class and requesting a handler from the standard Python library, keeping the program simple.

  • The function run initiates an instance of the HTTPserver.

  • As the arguments of server_address suggest, the server will listen to any incoming connection on port 8080.

Now, we will verify if the server is working as expected. Launch the server:

From a new terminal, we can use curl to send a request to the server:

Alternatively, you can access the link in a web browser:

Step 3 – Creating a Dockerfile

A Dockerfile  contains the necessary instructions to generate a Docker image. The instructions in the file are followed sequentially. Learn more about Dockerfile.

Create a new Dockerfile for our project:

Now, we will introduce the necessary codes in it. Open it in a text editor:

Enter the following code:

Here,

  • Any Dockerfile must begin with the FROM directive. In our case, we are declaring Python as the base of the Docker image.

  • The ENV SRC_DIR directive specifies the location of the container directory.

  • The COPY directive copies the files from the src directory that currently hosts the Python server.

  • The variable PYTHONBUFFERED=1 specifies that Python will print and log output directly to STDOUT. Otherwise, the logs wouldn’t be sent to any buffer.

  • The CMD directive specifies a default command to run when executing the container. In this case, we are using the directive to launch our Python server.

Step 4 – Docker Image Generation

With the Dockerfile ready, we can now bake an image. Run the following Docker command to start the process:

Here,

  • The -t flag is used to tag our Docker image as python_server.

  • Docker will download all the necessary components and combine them into an image.

Step 5 – Executing the Image

The image is ready for deployment. We can execute it using the following command:

Here, we are forwarding port 8080 from the local machine to the Docker image using the -p flag.

We can easily validate if the server is up and running using curl:

Step 6 – Terminating the Server

From the terminal, press “Ctrl + C” to terminate the Docker process:

Step 7 – Exporting and Importing the Docker Image

We now have a functional Docker image hosting our Python server. With the help of Docker’s export and import features, we can migrate it to any other system.

First, check the list of Docker images in the current system:

Our target is the python_server Docker image we just created. The following command will export it as a TAR archive:

After transferring the python_server.tar file to the target machine, use the following command to import the Docker image:

Final Thoughts

In this guide, we demonstrated building a Docker image out of a Python application. We created a simple Python webserver and built a Docker image out of it. The Docker image can now be deployed in any environment and expect consistent behavior.

Interested in learning more about Docker? Check out the following guides:

Happy Computing!