Python > Deployment and Distribution > Containerization > Introduction to Docker
Dockerfile Example: A Simple Python Application
This example demonstrates how to create a Dockerfile for a basic Python application. It covers the essential steps for containerizing a Python application, including defining the base image, copying source code, installing dependencies, and specifying the command to run the application.
Dockerfile Content
This Dockerfile starts with a slim Python 3.9 image, sets the working directory to /app, copies the requirements file, installs the Python dependencies, copies the application source code, and finally defines the command to execute the 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"]
Explanation of Dockerfile Instructions
Creating a Python Application (app.py)
This is a simple Flask application that serves 'Hello, Dockerized World!' when you access the root URL. The `host='0.0.0.0'` part is crucial for making the application accessible from outside the container.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, Dockerized World!'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
Creating a requirements.txt File
The `requirements.txt` file lists the Python dependencies required by your application. In this example, we only need Flask. You can add more dependencies as needed.
Flask==2.3.2
Building the Docker Image
Open your terminal, navigate to the directory containing the Dockerfile, app.py, and requirements.txt files, and run the following command: bash docker build -t my-python-app . This command builds a Docker image named `my-python-app` from the Dockerfile in the current directory.
Running the Docker Container
Once the image is built, you can run a container from it using the following command: bash docker run -p 5000:5000 my-python-app This command runs a container from the `my-python-app` image and maps port 5000 on your host machine to port 5000 inside the container. You can then access the application by opening your web browser and navigating to `http://localhost:5000`.
Concepts Behind the Snippet
This snippet demonstrates core Docker concepts like image creation using Dockerfiles, layering, and containerization. Dockerfiles provide a declarative way to define the environment for your application. Images are read-only templates used to create containers. Containers are isolated environments where your application runs.
Real-Life Use Case
Imagine you have a complex data processing pipeline written in Python with numerous dependencies. Containerizing this pipeline with Docker ensures that it runs consistently across different environments (development, testing, production) without dependency conflicts. It also simplifies deployment and scaling.
Best Practices
Interview Tip
Be prepared to explain the purpose of each instruction in the Dockerfile and how Docker containers isolate applications. Also, understand the difference between Docker images and containers.
When to Use Docker
Use Docker when you need to ensure consistent execution of your application across different environments, simplify deployment, isolate dependencies, and scale your application efficiently.
Memory Footprint
The memory footprint of a Docker container depends on the base image, installed dependencies, and the application itself. Using a slim base image and optimizing dependencies can significantly reduce the memory footprint.
Alternatives
Alternatives to Docker include Podman, rkt, and LXC. However, Docker is the most widely used containerization platform.
Pros
Cons
FAQ
-
What is the difference between a Docker image and a Docker container?
A Docker image is a read-only template that contains instructions for creating a container. A Docker container is a runnable instance of a Docker image. -
How do I stop a Docker container?
You can stop a Docker container using the command `docker stop` or `docker stop `. -
How do I remove a Docker image?
You can remove a Docker image using the command `docker rmi` or `docker rmi `.