type
status
date
summary
tags
category
URL
password
slug
icon
When building applications with Python, you’ll often run into dependency conflicts, version mismatches, and the like. With Docker, you can package applications—along with the required dependencies, runtime, and config—into a single portable artifact called the image. Which you can then use to spin up a Docker container that runs the app.
So whether it is a simple Python application or a data science application, Docker makes managing dependencies simpler. This is especially helpful in data science projects where you need different libraries and specific versions of these libraries for your application to work without errors. With Docker you can have isolated, consistent, and reproducible environments for all your applications.
As a first step in this direction, let's learn how to containerize a Python application.
Step 1: Get Started
First, install Docker on the platform you use. You can run Docker on Windows, Linux, and MacOs. Here are a couple of things you may want to do after you've installed Docker on your machine.
The Docker daemon binds to a Unix socket, owned by the
root
user by default. So you can access it only using sudo
. To avoid prefixing all your docker commands with sudo
, create a docker
group add a user to the group like so:For newer versions of Docker, BuildKit is the default builder. If you're using an older version of Docker, however, you may get deprecation warnings when you run the
docker build
command. This is because the legacy build client will be deprecated in future releases. As a workaround, you can install buildx, a CLI tool to use BuildKit's capabilities. And use the docker buildx build
command to build with BuildKit.Step 2: Code Your Python Application
Next, code a Python application which we can containerize using Docker. Here we’ll containerize a simple command-line TO-DO list app. The code for this app is on GitHub: todo.py file.
You can containerize any Python app of your choice or follow along with the example we use here. If you’re interested in a step-by-step tutorial on building the command-line TO-DO application, read Build a Command-Line App with Python in 7 Easy Steps.
Step 3: Create the Dockerfile
Next, we’ll create a Dockerfile. Think of it as a recipe that defines how to build the Docker image for the application. Create a file named
Dockerfile
in your working directory with the following:Here, we use Python 3.11 as the base image. We then set the working directory for all the following instructions with the
WORKDIR
command. We then use the COPY
command to copy files from the project into the container’s file system.Because we’re containerizing a command-line app, we specify the command to execute as
“/bin/bash”
. Which starts an interactive bash shell when we run the image and start a container.Step 4: Build the Docker Image
We have our todo.py file and Dockerfile ready. Next, we can build the Docker image with the following command:
With the
-t
option in the build command, you can specify both a name and a tag like so: docker build -t name:tag .
This command builds a Docker image named
todo-app
based on the instructions in the Dockerfile
. The .
at the end specifies that the build context is the current directory.The build takes a couple of minutes:
Step 5: Run Your Docker Container
Once the image is built, you can start a Docker container from the built image with the following command:
The
-it
option is a combination of -i
and -t
:- The
i
option is used to run containers interactively and keeps STDIN open even if not attached.
- The
t
option allocates a pseudo-TTY. So it provides a terminal interface within the container that you can interact with.
Now, our TO-DO app runs inside the Docker container, and we can interact with it at the command line:
Wrapping Up
And there you have it! You've successfully containerized a command-line Python application using Docker. In this tutorial, we looked at containerizing a simple Python application using Docker.
We built this application in Python without using any external Python libraries. So we did not define a requirements.txt file. The requirements.txt file usually lists the various libraries and their versions, which you can install using a simple
pip install
command. If you want a tutorial that focuses on Docker for data science, check out Docker Tutorial for Data Scientists.is a developer and technical writer from India. She likes working at the intersection of math, programming, data science, and content creation. Her areas of interest and expertise include DevOps, data science, and natural language processing. She enjoys reading, writing, coding, and coffee! Currently, she's working on learning and sharing her knowledge with the developer community by authoring tutorials, how-to guides, opinion pieces, and more. Bala also creates engaging resource overviews and coding tutorials.
- Author:NetSec
- URL:https://blog.51sec.org/article/8e200341-d7a4-46c2-9176-af17d7c66490
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!