
If you’ve ever tried to share your project with a friend, they may run into issues with trying to get your code to run on their machine. Queue the “but it works on my machine” phrase.
What is Docker?
Docker is a tool that allows developers to create an environment that allows your Python code to run exactly the same way every time through the usage of containers, no matter what machine you’re on (Mac, Windows, Linux).
In simple terms, it takes the “it doesn’t work on my machine” phrase to “it works on my machine!”
Docker creates images, which contains items such as OS, Python version, dependencies, binaries, etc. to be able to create your environment.
Docker images are ran using containers - this is the actual environment where your application executes. Containers contain everything in the image and the runtime state.
How do I create my first Docker container?
To begin working with Docker, you’ll need to install Docker Desktop from their website for free.
If you’re using Visual Studio Code or Cursor, install the Docker plugin.
Next, open a project that you want to containerize. In the root of the repository, create a Dockerfile.
This file contains all of the instructions needed to create your environment.

If you need a very basic Dockerfile, copy and modify the one below to meet your needs:
# Grab the base Python image
FROM python:3.12-slim
# Set the working directory
WORKDIR /app
# Install virtual environment
COPY requirements.txt .
RUN pip install -r requirements.txt
# If using uv instead, uncomment:
# RUN pip install --no-cache-dir uv
# COPY pyproject.toml uv.lock* ./
# RUN uv sync --frozen --no-dev
# Copy application code
COPY main.py .
# This is our "entry point".
CMD ["python", "main.py"]Without going into detail, here’s what each command does:
FROM- This pulls a pre-built image. For Python projects, you’ll be pulling the appropriate Python versions.WORKDIR- Sets the working directory of the application. By convention, this is under/app. Instead of your application working from/home/usr/YourName/Desktop, it runs in the image under/app.RUN- Execute a bash command. Here, we’re using it to install our environment fromrequirements.txtCOPY- Copy a file on your system to the image. You’ll use this command very frequently to get your application in the image.CMD- This is how we run our Python application as we would in the command line. This command is always last.
❗ Note: You can’t assume that images contain everything you need (i.e. apt-get). Chances are, you’ll need to install it by using the RUN command.
From here, open a terminal, make sure in the same directory as your Dockerfile and type docker build -t my-app .. This builds the image.
If you successfully build the image, you’ll be able to run the container: docker run my-app - you should see your application running!
Happy coding!
📧 Join the Python Snacks Newsletter! 🐍
Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:
Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.
Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.
Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.
You can unsubscribe at any time.
Interested in starting a newsletter or a blog?
Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.
If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.
This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

