
Python Example in Docker
To run a Python application in Docker, you can create a Dockerfile that sets up the environment for your Python code to run inside a container. Below is a simple example that shows how to containerize a Python script.
Steps to Dockerize a Python Application
Let's walk through creating a simple Python application and running it in Docker.
1. Prepare Your Python Application
First, create a simple Python script. For example, let's create a file named app.py
.
app.py
:
print("Hello from Docker and Python!")
This is a basic Python script that prints a message to the console.
2. Create a Dockerfile
Next, we’ll create a Dockerfile to containerize our Python application. The Dockerfile defines the environment that will run the Python app inside a Docker container.
Example Dockerfile:
# Step 1: Use the official Python image from Docker HubFROM python:3.9-slim# Step 2: Set the working directory inside the containerWORKDIR /usr/src/app# Step 3: Copy the Python script into the containerCOPY app.py .# Step 4: Define the command to run the Python scriptCMD ["python", "app.py"]
Explanation of the Dockerfile:
- FROM python:3.9-slim: This line tells Docker to use an official Python image based on Debian Linux (a slim version of it, which keeps the image size smaller). It will have Python 3.9 pre-installed.
- WORKDIR /usr/src/app: This sets the working directory inside the container to
/usr/src/app
. Any subsequentCOPY
orRUN
commands will execute within this directory. - COPY app.py .: This copies the
app.py
script from your local machine into the working directory of the container. - CMD ["python", "app.py"]: This tells Docker to run the Python script using the
python
command when the container starts.
3. Build the Docker Image
Now that you have both the Python script and the Dockerfile, you can build the Docker image.
- Open a terminal (or command prompt).
- Navigate to the directory where your
Dockerfile
andapp.py
file are located. - Run the following command to build the Docker image:
docker build -t python-docker-app .
This command will build a Docker image named python-docker-app
. The .
tells Docker to use the current directory to find the Dockerfile
.
4. Run the Docker Container
Once the image is built, you can run the container and execute your Python application.
To run the container:
docker run python-docker-app
This command will start the container and run the Python script inside it. You should see the following output:
Hello from Docker and Python!
5. (Optional) Share the Image on Docker Hub
If you want to share the Docker image, you can push it to Docker Hub. Here's how you can do it:
Login to Docker Hub:
docker login
Enter your Docker Hub username and password.
Tag the Image:
Tag the image so it's linked to your Docker Hub repository. Replace
yourusername
with your Docker Hub username.docker tag python-docker-app yourusername/python-docker-app
Push the Image to Docker Hub:
Push the tagged image to Docker Hub:
docker push yourusername/python-docker-app
Now your image is available on Docker Hub, and anyone with the correct access can pull it using:
docker pull yourusername/python-docker-app
6. (Optional) Create a Requirements File (for more complex applications)
If your Python application has dependencies (for example, third-party libraries), you can include them in the Docker image by creating a requirements.txt
file.
Example requirements.txt
:
flaskrequests
Then, update the Dockerfile to install the dependencies:
Updated Dockerfile:
# Step 1: Use the official Python image from Docker HubFROM python:3.9-slim# Step 2: Set the working directory inside the containerWORKDIR /usr/src/app# Step 3: Copy the requirements file and install dependenciesCOPY requirements.txt ./RUN pip install --no-cache-dir -r requirements.txt# Step 4: Copy the Python script into the containerCOPY app.py .# Step 5: Define the command to run the Python scriptCMD ["python", "app.py"]
7. Cleanup
After you are done testing, you can clean up by stopping and removing the container, and removing the image if no longer needed:
List running containers:
docker ps
Stop and remove the container:
docker stop <container_id>docker rm <container_id>
Remove the Docker image:
docker rmi python-docker-app
Conclusion
You have successfully containerized a simple Python application using Docker. By following the steps outlined, you can easily package your Python app into a Docker container, making it portable and ensuring it runs consistently across different environments.
You can expand on this by adding more complex Python applications, including additional dependencies, or using Docker Compose to manage multi-container applications.