Dockerizing Your Python Application and Persisting Data
Have you ever written a Python application and wanted to containerize it using Docker? This approach has many advantages, including isolating the app from other system processes, simplifying the app’s deployment, and offering better resource utilization.
One of the common scenarios while containerizing apps is the need to persist data, or deal with files that change over time, like configuration files, logs, or data files. Docker provides a mechanism for this via volume mounting using the -v command.
This tutorial aims to guide you on how to Dockerize your Python application and use the -it -v command to persist data that needs to change.
Step 1: Create a Simple Python App
First, let’s create a simple Python application. We’ll be building a simple “Hello, World!” application.
print("Hello, World!")Step 2: Write a Dockerfile
The next step involves writing a Dockerfile that instructs Docker on how to build our Python application’s container.
# Use an official Python runtime as a parent image
FROM python:3.8# Set the working directory in the container to /app
WORKDIR /app# Copy the current directory contents into the container at /app
COPY . /app# Run app.py when the container launches
CMD ["python", "app.py"]
Step 3: Build the Docker Image
With the Dockerfile ready, we can build our Docker image using the docker build command:
docker build -t python-app .Step 4: Persist Data Using -it -v
So far, we have created a Docker image for our Python application. But how do we handle the case where our Python application changes data files and we want to persist those changes?
Get Julius Tuliao’s stories in your inbox
Join Medium for free to get updates from this writer.
Docker has a solution for this — the -v flag, which stands for volume. You can use it to mount a local system directory as a volume in the Docker container. Changes to this directory in the container will then be reflected on your local system, allowing data persistence.
Here’s how you can do it:
docker run -it -v $(pwd)/data:/app/data python-appHere, $(pwd)/data is the local system directory, and /app/data is the path in the Docker container. Any changes made inside /app/data will reflect in your local data directory.
The -it option is used to run the Docker container in interactive mode and to allocate a pseudo-TTY for the container, which allows you to interact with the container from your terminal.
Step 5: Making Changes and Observing Persistence
To observe data persistence, modify your Python app to create a file in the /app/data directory.
with open('data/hello.txt', 'w') as f:
f.write("Hello, World!")After running the Docker container as before, you’ll notice that a new file hello.txt has been created in your local data directory.
In conclusion, Dockerizing a Python application offers numerous advantages in terms of deployment and environment consistency. Docker’s -v flag allows you to mount local directories as volumes in Docker containers, which is extremely useful when you want to persist data or deal with files that change regularly. Happy Dockerizing!









