Containerize Python Apps with Docker in 5 Simple Steps – KDnuggets

 


Picture by Writer
 

When constructing purposes with Python, you’ll typically run into dependency conflicts, model mismatches, and the like. With Docker, you may bundle purposes—together with the required dependencies, runtime, and config—right into a single moveable artifact referred to as the picture. Which you’ll then use to spin up a Docker container that runs the app.

So whether or not it’s a easy Python utility or an information science utility, Docker makes managing dependencies easier. That is particularly useful in information science initiatives the place you want completely different libraries and particular variations of  these libraries on your utility to work with out errors. With Docker you may have remoted, constant, and reproducible environments for all of your purposes.

As a primary step on this path, let’s discover ways to containerize a Python utility.

 

Step 1: Get Began

 

First, set up Docker on the platform you utilize. You possibly can run Docker on Home windows, Linux, and MacOs. Listed here are a few issues you could wish to do after you have put in Docker in your machine.

The Docker daemon binds to a Unix socket, owned by the root person by default. So you may entry it solely utilizing sudo. To keep away from prefixing all of your docker instructions with sudo, create a docker group add a person to the group like so:

$ sudo groupadd docker

$ sudo usermod -aG docker $USER

 

For newer variations of Docker, BuildKit is the default builder. When you’re utilizing an older model of Docker, nevertheless, you could get deprecation warnings once you run the docker construct command. It’s because the legacy construct consumer will likely be deprecated in future releases. As a workaround, you may set up buildx, a CLI software to make use of BuildKit’s capabilities. And use the docker buildx construct command to construct with BuildKit.

 

Step 2: Code Your Python Software

 

Subsequent, code a Python utility which we will containerize utilizing Docker. Right here we’ll containerize a easy command-line TO-DO checklist app. The code for this app is on GitHub: todo.py file.

You possibly can containerize any Python app of your selection or observe together with the instance we use right here. When you’re excited about a step-by-step tutorial on constructing the command-line TO-DO utility, learn Construct a Command-Line App with Python in 7 Simple Steps.

 

Step 3: Create the Dockerfile

 

Subsequent, we’ll create a Dockerfile. Consider it as a recipe that defines how you can construct the Docker picture for the appliance. Create a file named Dockerfile in your working listing with the next:


# Use Python 3.11 as base picture
FROM python:3.11-slim

# Set the working listing within the container
WORKDIR /app

# Copy the present listing contents into the container at /app
COPY . /app

# Command to run the Python script
CMD ["/bin/bash"]

 

Right here, we use Python 3.11 as the bottom picture. We then set the working listing for all the next directions with the WORKDIR command. We then use the COPY command to repeat recordsdata from the mission into the container’s file system.

As a result of we’re containerizing a command-line app, we specify the command to execute as “/bin/bash”. Which begins an interactive bash shell after we run the picture and begin a container.

 

Step 4: Construct the Docker Picture

 

Now we have our todo.py file and Dockerfile prepared. Subsequent, we will construct the Docker picture with the next command:

docker construct -t todo-app .

 

With the -t possibility within the construct command, you may specify each a reputation and a tag like so: docker construct -t title:tag .

This command builds a Docker picture named todo-app primarily based on the directions within the Dockerfile. The . on the finish specifies that the construct context is the present listing.

The construct takes a few minutes:

Sending construct context to Docker daemon  4.096kB
Step 1/4 : FROM python:3.11-slim
3.11-slim: Pulling from library/python
13808c22b207: Pull full
6c9a484475c1: Pull full
b45f078996b5: Pull full
16dd65a710d2: Pull full
fc35a8622e8e: Pull full
Digest: sha256:dad770592ab3582ab2dabcf0e18a863df9d86bd9d23efcfa614110ce49ac20e4
Standing: Downloaded newer picture for python:3.11-slim
 ---> c516402fec78
Step 2/4 : WORKDIR /app
 ---> Working in 27d02ba3a48d
Eradicating intermediate container 27d02ba3a48d
 ---> 7747abda0fc0
Step 3/4 : COPY . /app
 ---> fd5cb75a0529
Step 4/4 : CMD ["/bin/bash"]
 ---> Working in ef704c22cd3f
Eradicating intermediate container ef704c22cd3f
 ---> b41986b633e6
Efficiently constructed b41986b633e6
Efficiently tagged todo-app:newest

 

Step 5: Run Your Docker Container

 

As soon as the picture is constructed, you can begin a Docker container from the constructed picture with the next command:

 

The -it possibility is a mixture of -i and -t:

  • The -i possibility is used to run containers interactively and retains STDIN open even when not connected.
  • The -t possibility allocates a pseudo-TTY. So it offers a terminal interface throughout the container that you may work together with.

Now, our TO-DO app runs contained in the Docker container, and we will work together with it on the command line:

root@9d85c09f01ec:/app# python3 todo.py
utilization: todo.py [-h] [-a] [-l] [-r]

Command-line Todo Listing App

choices:
  -h, --help  	present this assist message and exit
  -a , --add  	Add a brand new process
  -l, --list  	Listing all duties
  -r , --remove   Take away a process by index
root@9d85c09f01ec:/app# python3 todo.py -a 'stroll 2 miles'
root@9d85c09f01ec:/app# python3 todo.py -l
1. stroll 2 miles

 

Wrapping Up

 

And there you’ve gotten it! You’ve got efficiently containerized a command-line Python utility utilizing Docker. On this tutorial, we checked out containerizing a easy Python utility utilizing Docker.

We constructed this utility in Python with out utilizing any exterior Python libraries. So we didn’t outline a necessities.txt file. The necessities.txt file normally lists the assorted libraries and their variations, which you’ll set up utilizing a easy pip set up command. If you’d like a tutorial that focuses on Docker for information science, try Docker Tutorial for Knowledge Scientists.

 

 

Bala Priya C is a developer and technical author from India. She likes working on the intersection of math, programming, information science, and content material creation. Her areas of curiosity and experience embrace DevOps, information science, and pure language processing. She enjoys studying, writing, coding, and occasional! At present, she’s engaged on studying and sharing her information with the developer neighborhood by authoring tutorials, how-to guides, opinion items, and extra. Bala additionally creates participating useful resource overviews and coding tutorials.

Recent articles