DEV Community

Arthur
Arthur

Posted on

Docker for Beginners: Containerize a Python Application Step by Step

Hello, I'm Arthur. Have you ever worked on a Python application that runs perfectly on your computer but fails when you try to run it on another machine?

Maybe the Python version is different, a required package is missing, or the application depends on a system library that is not installed.

Docker helps solve this problem by packaging an application and its dependencies into a container.

In this tutorial, we'll build a simple Python application, create a Docker image, and run it inside a container.

1. Understand the Difference Between an Image and a Container

Before writing any commands, it helps to understand two important Docker concepts.

A Docker image is a packaged template containing the application, its dependencies, and the instructions needed to run it.

A container is a running instance of that image.

You can create multiple containers from the same image without rebuilding the application each time.

For example, you might use one image to run a development environment and another container to run a separate test instance.

2. Install Docker

Docker is available for Linux, Windows, and macOS.

For this tutorial, we'll use Docker on Linux.

On Ubuntu, follow the official Docker installation instructions to install Docker Engine and the Docker CLI.

After installation, check the version:

docker --version
Enter fullscreen mode Exit fullscreen mode

Verify that Docker is running:

sudo systemctl status docker
Enter fullscreen mode Exit fullscreen mode

You can also run a test container:

sudo docker run hello-world
Enter fullscreen mode Exit fullscreen mode

If Docker is installed correctly, the command downloads a small image and runs a container that prints a confirmation message.

3. Create a Python Application

Create a project directory:

mkdir docker-python-app
cd docker-python-app
Enter fullscreen mode Exit fullscreen mode

Create a file named app.py:

import os

app_name = os.getenv("APP_NAME", "My Python App")

print(f"Welcome to {app_name}!")
Enter fullscreen mode Exit fullscreen mode

This application reads a value from an environment variable.

If the variable is not set, it uses the default name.

Test it locally:

python3 app.py
Enter fullscreen mode Exit fullscreen mode

Expected output:

Welcome to My Python App!
Enter fullscreen mode Exit fullscreen mode

Now let's package this application using Docker.

4. Write Your First Dockerfile

Create a file named Dockerfile in the project directory:

FROM python:3.12-slim

WORKDIR /app

COPY app.py .

CMD ["python", "app.py"]
Enter fullscreen mode Exit fullscreen mode

Let's understand what each instruction does.

  • FROM selects the base image containing Python.
  • WORKDIR sets the working directory inside the container.
  • COPY copies the application file into the image.
  • CMD defines the command that runs when the container starts.

The python:3.12-slim image is a smaller Python image based on Debian.

For a real project, choose a Python version that is compatible with your application's dependencies.

5. Build a Docker Image

Run this command from the project directory:

docker build -t my-python-app .
Enter fullscreen mode Exit fullscreen mode

The -t option gives the image a name.

The final dot tells Docker to use the current directory as the build context.

Docker reads the Dockerfile, downloads the required base image if necessary, and creates the application image.

Check that the image exists:

docker images
Enter fullscreen mode Exit fullscreen mode

You should see my-python-app in the list.

6. Run Your Application Inside a Container

Start the container:

docker run my-python-app
Enter fullscreen mode Exit fullscreen mode

You should see:

Welcome to My Python App!
Enter fullscreen mode Exit fullscreen mode

The application is now running inside a Docker container rather than directly in your local Python environment.

You can also override the environment variable:

docker run -e APP_NAME="Docker Demo" my-python-app
Enter fullscreen mode Exit fullscreen mode

The output will be:

Welcome to Docker Demo!
Enter fullscreen mode Exit fullscreen mode

This is useful when the same application needs different configuration values in development, testing, and production.

7. Make the Container Run in the Background

Some applications need to keep running instead of finishing immediately.

Docker can run a container in detached mode using the -d option.

For example, you could use a long-running Python application or a web server.

To see the containers that are currently running:

docker ps
Enter fullscreen mode Exit fullscreen mode

To view stopped containers as well:

docker ps -a
Enter fullscreen mode Exit fullscreen mode

You can stop a running container with:

docker stop CONTAINER_ID
Enter fullscreen mode Exit fullscreen mode

Replace CONTAINER_ID with the actual ID shown by Docker.

To remove a stopped container:

docker rm CONTAINER_ID
Enter fullscreen mode Exit fullscreen mode

These commands help you manage containers during development and testing.

8. Use a .dockerignore File

When building an image, Docker sends files from the build context to the build process.

You generally don't need to include local virtual environments, Git history, or temporary files.

Create a .dockerignore file:

venv/
__pycache__/
.git/
*.pyc
.env
Enter fullscreen mode Exit fullscreen mode

This prevents those files from being included in the build context.

The .env entry is particularly important if your project contains local configuration files with sensitive values.

However, .dockerignore is not a complete secret-management solution. Never copy passwords, API keys, or private credentials into a Docker image.

9. Understand Docker Image Layers

Docker builds images in layers.

Each instruction in a Dockerfile can create a layer that Docker may reuse during later builds.

This is useful because unchanged layers can be cached.

For example, if your application has a requirements.txt file, you can copy it and install dependencies before copying the rest of your source code.

FROM python:3.12-slim

WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["python", "app.py"]
Enter fullscreen mode Exit fullscreen mode

When your application code changes but the dependency file stays the same, Docker can often reuse the dependency installation layer.

This can make repeated builds faster.

10. Run Your Container on a VPS

Docker can be useful when you want to run the same application in different environments.

For example, you might build an image on your development machine and then run it on a Linux VPS.

Before deploying, make sure:

  • Docker is installed and configured correctly.
  • The server has enough CPU, RAM, and disk space.
  • Required ports are configured correctly.
  • Sensitive configuration values are stored securely.
  • Container logs and restart behavior are managed.

If you're comparing server options for containerized applications, HelloServer VPS hosting is one option to explore.

A VPS can provide a separate Linux environment for running your containers and managing your application.

11. Build and Run the Same Image on Another Machine

One of Docker's main benefits is that you can share an image instead of manually recreating the entire application environment.

For example, you can tag your image:

docker tag my-python-app username/my-python-app:1.0
Enter fullscreen mode Exit fullscreen mode

Then push it to a container registry after authenticating:

docker push username/my-python-app:1.0
Enter fullscreen mode Exit fullscreen mode

On another machine, you can pull the image:

docker pull username/my-python-app:1.0
Enter fullscreen mode Exit fullscreen mode

And run it:

docker run username/my-python-app:1.0
Enter fullscreen mode Exit fullscreen mode

This workflow is useful for teams that want a consistent application environment across development and deployment.

For larger workloads that require specialized hardware, you can also compare HelloServer GPU dedicated servers when planning your infrastructure.

Final Thoughts

Docker is a practical tool for packaging applications and making their environments more consistent.

You have learned how to create a Dockerfile, build an image, run a container, and configure an application using environment variables.

The next step is to learn how to manage persistent data, connect containers to networks, and use Docker Compose for applications with multiple services.

Start with a small Python project and practice building and running it in a container.

Once you understand the basics, you'll be better prepared to use Docker in larger development and deployment workflows.

Top comments (0)