Python > Deployment and Distribution > Containerization > Running Docker Containers
Running a Simple Python Application in a Docker Container
This snippet demonstrates how to run a basic Python application inside a Docker container. It includes creating a Dockerfile, building the image, and running the container.
Dockerfile Creation
The Dockerfile defines the environment for your application.
FROM python:3.9-slim-buster specifies the base image (Python 3.9 slim version on Debian Buster).
WORKDIR /app sets the working directory inside the container.
COPY requirements.txt . copies the requirements.txt file (listing Python dependencies) to the working directory.
RUN pip install --no-cache-dir -r requirements.txt installs the dependencies.
COPY . . copies the entire application code into the container.
CMD ["python", "app.py"] specifies the command to run when the container starts (running your Python application).
FROM python:3.9-slim-buster
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]
Python Application (app.py)
This is a simple Flask application that returns 'Hello, Docker!' when accessed. We use Flask because it's lightweight and easy to demonstrate the principle. host='0.0.0.0' makes the application accessible from outside the container.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, Docker!'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
Requirements File (requirements.txt)
This file lists the Python dependencies. In this case, it specifies that Flask version 2.0.1 is required.
Flask==2.0.1
Building the Docker Image
This command builds the Docker image.
docker build is the Docker command to build an image.
-t my-python-app tags the image with the name 'my-python-app'.
. specifies that the Dockerfile is located in the current directory. Run this command in the directory containing your Dockerfile.
docker build -t my-python-app .
Running the Docker Container
This command runs the Docker container.
docker run is the Docker command to run a container.
-p 5000:5000 maps port 5000 on the host machine to port 5000 inside the container (where the Flask app is running).
my-python-app specifies the name of the image to run. After running this, you can access the application in your web browser at http://localhost:5000.
docker run -p 5000:5000 my-python-app
Concepts Behind the Snippet
This snippet illustrates the core concepts of containerization: encapsulating an application and its dependencies into a single unit (the Docker image). Docker provides isolation, portability, and reproducibility.
Real-Life Use Case
This pattern is used to deploy web applications, APIs, and microservices in production environments. Containerization allows for easy scaling, management, and deployment across different environments (development, testing, production).
Best Practices
Dockerfile instead of just python to avoid unexpected changes..dockerignore file to exclude unnecessary files from being copied into the image.
Interview Tip
Be prepared to explain the purpose of each line in the Dockerfile. Understand the difference between COPY and ADD in the Dockerfile. Also, know the implications of different base images (e.g., Alpine vs. Debian).
When to Use Them
Use Docker containers when you need to ensure consistency across different environments, isolate applications, scale services easily, or manage dependencies effectively.
Memory Footprint
The memory footprint of a Docker container depends on the base image and the application running inside it. Slimmer base images like python:3.9-slim-buster will generally result in smaller memory footprints.
Alternatives
Alternatives to Docker include other containerization technologies like Podman, containerd, and LXC. Also, serverless functions offer an alternative deployment strategy, but they abstract away the underlying infrastructure.
Pros
Cons
FAQ
-
How do I access my application running in the Docker container?
You can access your application by mapping a port on your host machine to the port on which the application is running inside the container (using the-pflag in thedocker runcommand). For example,-p 5000:5000maps port 5000 on your host to port 5000 inside the container. Then access viahttp://localhost:5000. -
How do I stop a running Docker container?
You can stop a running Docker container using thedocker stop [container_id]command. To find the container ID, use thedocker pscommand. -
How do I remove a Docker image?
You can remove a Docker image using thedocker rmi [image_id]command. To find the image ID, use thedocker imagescommand.