In today’s fast-evolving tech environment, Docker has emerged as a revolutionary tool in software development, allowing developers to package applications into containers. These containers can run consistently across different systems, making development and deployment smoother. If you’re a beginner curious about integrating Python with Docker, this guide will walk you through a straightforward setup process.
Table of Contents
- What is Docker?
- Why Use Docker with Python?
- Getting Started: Installing Docker
- Running Python in a Docker Container
- Conclusion
What is Docker?
Docker is an open-source platform that automates the deployment of applications in lightweight containers, enabling applications to work seamlessly in different environments. Containers encapsulate everything needed to run a piece of software, including the code, runtime, libraries, and system tools, ensuring consistency across various platforms.
Why Use Docker with Python?
Python is a versatile programming language widely used for web development, data science, automation, and more. However, Python applications can sometimes face compatibility issues due to differing environments. Docker resolves these issues by providing an isolated environment that ensures your Python applications run precisely as intended, regardless of where they are deployed.
Getting Started: Installing Docker
Before we proceed, you need to have Docker installed on your machine.
Installation on Windows
- Download Docker Desktop for Windows
- Visit the Docker Official Website and download the Docker Desktop for Windows installer.
- Install Docker Desktop
- Run the installer and follow the on-screen instructions.
- Ensure “Enable WSL 2 feature” is checked during installation.
- Start Docker Desktop
- After installation, start Docker Desktop from the Start menu.
- The Docker icon in your system tray indicates Docker is running successfully.
Installation on macOS
- Download Docker Desktop for Mac
- Head to the Docker Official Website and download the installer.
- Install Docker Desktop
- Open the installer file and drag the Docker icon to your Applications folder.
- Start Docker Desktop
- Open Docker from your Applications and follow the tutorial for an initial setup.
Installation on Linux
- Install Prerequisites
sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common - Add Docker’s official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- Add Docker repository
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable" - Install Docker
sudo apt-get update
sudo apt-get install docker-ce - Verify Docker installation
sudo docker --version
Running Python in a Docker Container
With Docker installed, let’s set up a simple Python application within a Docker container. Let’s first create a simple Python script.
Write a Python Script
Create a folder named python_docker
and navigate to it:
mkdir python_docker
cd python_docker
Create a file named app.py
and add the following code:
# app.py
print("Hello, from Docker!")
Code language: PHP (php)
Create a Dockerfile
A Dockerfile is a text document that contains all the commands to assemble an image.
In the same directory, create a file named Dockerfile
(without any extension) and add the following content:
# Use the official Python image from the Docker Hub
FROM python:3.8-alpine
# Set the working directory
WORKDIR /usr/src/app
# Copy the current directory contents into the container at /usr/src/app
COPY . .
# Run the command
CMD [ "python", "./app.py" ]
Code language: PHP (php)
Build and Run the Docker Container
Now, let’s build a Docker image from your Dockerfile.
Build the Docker Image
<p>docker build -t python-docker-app .</p>
Code language: HTML, XML (xml)
-t python-docker-app
: Tags your image with a name for easier reference..
: Refers to the current directory, where the Dockerfile is located.Run the Docker Container
<p>docker run python-docker-app</p>
Code language: HTML, XML (xml)
- This command will execute the
app.py
script, and you should seeHello, from Docker!
printed.
Understanding the Dockerfile
- FROM python:3.8-alpine: This line defines the base image.
python:3.8-alpine
is a lightweight version of Python, perfect for reducing the size of your final image. - WORKDIR /usr/src/app: Sets the working directory inside the container.
- COPY . .: Copies all files from the current directory on your host machine into the /usr/src/app directory inside the container.
- CMD [ “python”, “./app.py” ]: Specifies the command to execute when the container starts.
Conclusion
You’ve just learned how to run a Python application in a Docker container! This powerful tool not only standardizes development environments but also simplifies the deployment process, ensuring your Python applications are more scalable and portable. Now that you’ve grasped the basics, consider experimenting with more complex projects and explore the vast ecosystem of Docker images available on Docker Hub. How will you use Docker to enhance your Python development process? Share your thoughts and next steps with us in the comments below.