

Picture by Writer
When constructing functions with Python, you’ll usually run into dependency conflicts, model mismatches, and the like. With Docker, you possibly can package deal functions—together with the required dependencies, runtime, and config—right into a single transportable artifact known 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 knowledge science tasks the place you want completely different libraries and particular variations of these libraries on your utility to work with out errors. With Docker you possibly can have remoted, constant, and reproducible environments for all of your functions.
As a primary step on this course, let’s learn 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 below are a few issues you could need to do after you have put in Docker in your machine.
The Docker daemon binds to a Unix socket, owned by the root consumer by default. So you possibly can entry it solely utilizing sudo. To keep away from prefixing all of your docker instructions with sudo, create a docker group add a consumer to the group like so:
$ sudo groupadd docker
$ sudo usermod -aG docker $USER
For newer variations of Docker, BuildKit is the default builder. In case you’re utilizing an older model of Docker, nonetheless, you could get deprecation warnings while you run the docker construct command. It’s because the legacy construct shopper shall be deprecated in future releases. As a workaround, you possibly can 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 record app. The code for this app is on GitHub: todo.py file.
You possibly can containerize any Python app of your alternative or comply with together with the instance we use right here. In case you’re fascinated by a step-by-step tutorial on constructing the command-line TO-DO utility, learn Construct a Command-Line App with Python in 7 Straightforward Steps.
Step 3: Create the Dockerfile
Subsequent, we’ll create a Dockerfile. Consider it as a recipe that defines the right way to construct the Docker picture for the applying. 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 information 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 once we run the picture and begin a container.
Step 4: Construct the Docker Picture
We’ve got 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 possibly can specify each a reputation and a tag like so: docker construct -t title:tag .
This command builds a Docker picture named todo-app based mostly 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
---> Operating in 27d02ba3a48d
Eradicating intermediate container 27d02ba3a48d
---> 7747abda0fc0
Step 3/4 : COPY . /app
---> fd5cb75a0529
Step 4/4 : CMD ["/bin/bash"]
---> Operating 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 mix of -i and -t:
- The
-ipossibility is used to run containers interactively and retains STDIN open even when not connected. - The
-tpossibility allocates a pseudo-TTY. So it gives 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 activity
-l, --list Listing all duties
-r , --remove Take away a activity 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 got 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 be able to set up utilizing a easy pip set up command. In order for you a tutorial that focuses on Docker for knowledge science, try Docker Tutorial for Information Scientists.
Bala Priya C is a developer and technical author from India. She likes working on the intersection of math, programming, knowledge science, and content material creation. Her areas of curiosity and experience embrace DevOps, knowledge science, and pure language processing. She enjoys studying, writing, coding, and occasional! Presently, she’s engaged on studying and sharing her data with the developer neighborhood by authoring tutorials, how-to guides, opinion items, and extra. Bala additionally creates partaking useful resource overviews and coding tutorials.
