Containerizing an application typically involves creating a Docker image that packages your application and its dependencies. Below, I’ll walk you through the steps to containerize a simple Python web application using Docker. This example assumes you have Docker installed on your system.
Here’s a step-by-step guide:
- Create Your Application: First, you need to have an application to containerize. Let’s create a simple Python web application using Flask as an example. Create a directory for your project and inside it, create a Python script named
app.py
:
# app.py from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, Docker World!' if __name__ == '__main__': app.run(host='0.0.0.0', port=80)
- Create a Dockerfile: Next, create a
Dockerfile
in the same directory as your application. The Dockerfile defines the instructions for building your Docker image:
# Use an official Python runtime as a parent image FROM python:3.8-slim-buster # Set the working directory to /app WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed packages specified in requirements.txt RUN pip install --trusted-host pypi.python.org -r requirements.txt # Make port 80 available to the world outside this container EXPOSE 80 # Define environment variable ENV NAME World # Run app.py when the container launches CMD ["python", "app.py"]
This Dockerfile uses the official Python 3.8 image from Docker Hub, sets the working directory to /app
, copies your application code into the container, installs any required Python packages from requirements.txt
, exposes port 80, sets an environment variable, and specifies the command to run your application.
- Create a Requirements File: If your application has dependencies, create a
requirements.txt
file listing them. In this example, we don’t have any external dependencies, so you can leave it empty. - Build the Docker Image: Open a terminal and navigate to the directory where your Dockerfile is located. Run the following command to build the Docker image:
docker build -t my-python-app .
The -t
flag assigns a name (tag) to the image, and .
indicates that the Dockerfile is in the current directory. After this command completes, you’ll have a Docker image named my-python-app
.
- Run the Docker Container: You can now run a container from your image:
docker run -p 4000:80 my-python-app
This command maps port 4000 on your host system to port 80 inside the container. You should see output indicating that your Flask application is running.
- Access the Application: Open a web browser and navigate to
http://localhost:4000
. You should see “Hello, Docker World!” displayed in your browser, indicating that your containerized Python web application is running successfully.
That’s it! You’ve containerized a simple Python web application using Docker. This example demonstrates the basic steps involved in containerizing an application, but the process can be adapted to more complex applications with additional dependencies and configurations.