
Containerize your first Python project in minutes
Docker is one of those environments that I can’t live without anymore. It’s interesting that there aren’t many resources that discuss getting a basic Python script going with Docker and docker-compose. Let’s see how simple this is to do with Python.
Create a new directory for your project and navigate into it:
mkdir python_test
cd python_test
Create a new file called Dockerfile with the following content:
FROM python:3.11-slim-buster
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "python", "app.py" ]
This Dockerfile sets up a Python 3.11 environment, installs the requirements specified in requirements.txt, and runs app.py when the container starts.
Create a new file called docker-compose.yml with the following contents:
version: '3'
services:
app:
build: .
volumes:
- .:/app
This docker-compose.yml file defines a single service called app that builds the image using the Dockerfile in the current directory and mounts the current directory as a volume in the container.
Create a new file called requirements.txt with the following content:
pandas
numpy
This requirements.txt file specifies the Pandas and Numpy libraries as a requirement for our Python application.
Create a new file called app.py with the following content:
import pandas as pd
import numpy as np
print("Here's a Pandas Example")
df = pd.read_csv('testfile.csv')
print(df.to_string())
print("\nHere's a Numpy Example")
arr = np.array([1, 2, 3, 4, 5, 4, 4, 5, 4])
x = np.where(arr == 4)
print(x)
The application imports pandas and numpy and prints out the content of a CSV file and every index that contains the number 4 as an element value.
The testfile.csv file looks like this:
Username; Identifier;One-time password;Recovery code;First name;Last name;Department;Location
booker12;9012;12se74;rb9012;Rachel;Booker;Sales;Manchester
grey07;2070;04ap67;lg2070;Laura;Grey;Depot;London
johnson81;4081;30no86;cj4081;Craig;Johnson;Depot;London
jenkins46;9346;14ju73;mj9346;Mary;Jenkins;Engineering;Manchester
smith79;5079;09ja61;js5079;Jamie;Smith;Engineering;Manchester
We now simply need to do docker-compose up and our application will execute.
dinocajic@Alien-Laptop:~/test$ docker-compose up
[+] Running 0/1
⠋ Container test-app-1 Recreate 0.1s
Error response from daemon: invalid volume specification: '/run/desktop/mnt/host/wsl/docker-desktop-bind-mounts/Ubuntu-22.04/dca32ca337b1b506fe21ca43b7478a309dfd52e4a09ae2f750e2f3235b75606e:.:rw': invalid mount config for type "bind": invalid mount path: '.' mount path must be absolute
dinocajic@Alien-Laptop:~/test$ docker-compose down
[+] Running 2/2
⠿ Container a5cd7b8677f0_test-app-1 Removed 0.0s
⠿ Network test_default Removed 0.3s
dinocajic@Alien-Laptop:~/test$ docker-compose down
dinocajic@Alien-Laptop:~/test$ docker-compose up
[+] Building 0.5s (10/10) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 207B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/python:3.11-slim-buster 0.3s
=> [internal] load build context 0.0s
=> => transferring context: 9.53kB 0.0s
=> [1/5] FROM docker.io/library/python:3.11-slim-buster@sha256:0db2395b8e68d4d0ab4f5212136603d6cac793f6850149396acd2228b51f60a7 0.0s
=> CACHED [2/5] WORKDIR /app 0.0s
=> CACHED [3/5] COPY requirements.txt ./ 0.0s
=> CACHED [4/5] RUN pip install --no-cache-dir -r requirements.txt 0.0s
=> [5/5] COPY . . 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:dd1ae99fdbd1ca81148b924c9db0472390344b4dee2c3f9212598e99b7855ce4 0.0s
=> => naming to docker.io/library/test-app 0.0s
[+] Running 2/2
⠿ Network test_default Created 0.0s
⠿ Container test-app-1 Created 0.1s
Attaching to test-app-1
test-app-1 | Here's a Pandas Example
test-app-1 | Hello; Identifier;One-time password;Recovery code;First name;Last name;Department;Location
test-app-1 | 0 booker12;9012;12se74;rb9012;Rachel;Booker;Sales;Manchester
test-app-1 | 1 grey07;2070;04ap67;lg2070;Laura;Grey;Depot;London
test-app-1 | 2 johnson81;4081;30no86;cj4081;Craig;Johnson;Depot;London
test-app-1 | 3 jenkins46;9346;14ju73;mj9346;Mary;Jenkins;Engineering;Manchester
test-app-1 | 4 smith79;5079;09ja61;js5079;Jamie;Smith;Engineering;Manchester
test-app-1 |
test-app-1 | Here's a Numpy Example
test-app-1 | (array([3, 5, 6]),)
test-app-1 exited with code 0
That’s it. You can now make modifications to the file and rerun the docker-compose up command to see your changes.
Keeping the container running
Sometimes you want to keep the container running and just execute the commands from inside the container. That’s a simple fix as well.
First, we’ll need to add stdin_open and tty arguments to our docker-compose.yml file.
version: '3'
services:
app:
build: .
stdin_open: true
tty: true
volumes:
- .:/app
Remove the CMD command in Dockerfile.
FROM python:3.11-slim-buster
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
Delete the image and the container from Docker Desktop. Simply get into your Docker Desktop environment and press on the trash icon to remove.


Rerun docker-compose up to rebuild the image and the container. You should see that your container is running in both the terminal and your Docker Desktop.
[+] Running 2/1
⠿ Network test_default Created 0.0s
⠿ Container test-app-1 Created 0.1s
Attaching to test-app-1
test-app-1 | Python 3.11.2 (main, Mar 1 2023, 15:15:54) [GCC 8.3.0] on linux
test-app-1 | Type "help", "copyright", "credits" or "license" for more information.

Open the app-1 container and click on the Terminal link.

Execute your Python commands.

Remember that if you ever mess with the Dockerfile, you’ll need to rebuild the image. So, delete the container and the image from Docker Desktop and rerun docker-compose up. That’s it.
Python Series
Continue your Python Learning.

The language that does it all
In part zero of our Python basics series, explore why Python has become the language of choice for web apps, data science, automation, and AI. Unpack its readable syntax, massive ecosystem, and thriving community, and learn where it fits in your tech stack.

Containerize your first Python project in minutes
Python P1: Python P1: Getting started with Python and Docker-Compose
Part one of our Python + Docker series shows you how to spin up a Python development environment with docker-compose. Define services, mount code, install dependencies, and run scripts reproducibly on any machine—no “it works on my box” excuses.

Debug inside the container—directly from PyCharm.
Python P2: PyCharm with a Docker Interpreter
Part two of our Python + Docker series walks through configuring PyCharm to use a Docker interpreter. You’ll attach your IDE to a container, run and debug code inside it, and keep dependencies isolated while enjoying full IntelliSense and breakpoints.