Lab Workbook: A comprehensive guide for students to dockerize a Python application in a Windows environment.
Student Lab Learning Guide: Dockerizing Your Python Program on Windows
In this lab, you'll learn how to use Docker, a powerful tool for developing, shipping, and running applications.
We will focus on dockerizing a Python application and running it on Windows using Docker Desktop.
### Prerequisites
- Windows 10 or newer.
- Basic knowledge of command line interface (CLI) commands.
- A Python program you wish to containerize.
## Step-by-Step Instructions
1. Visit [Docker Hub](https://hub.docker.com/editions/community/docker-ce-desktop-windows).
2. Download Docker Desktop for Windows.
3. Run the `.exe` file and follow the installation prompts.
4. After installation, open Docker Desktop from your Start menu.
*Note: Initial start-up might take a few minutes.*
Step 2: Prepare Your Python Program
1. Ensure your Python program runs correctly locally.
2. Create a new directory for your project.
3. Place your Python script (e.g., `app.py`) in this directory.
Step 3: Handle Dependencies with `requirements.txt`
1. If your program has dependencies, generate a `requirements.txt` file:
- In your project directory, activate your Python environment.
- Run `pip freeze > requirements.txt` to create the file with all dependencies.
Dependencies are what you pip install.
If you don't have any dependencies for your Python application, you won't need to include a "requirements.txt" file or run the "pip install" command to install any dependencies. This can happen when your application doesn't rely on any external Python packages or libraries.
In such a scenario, you can simply omit the "pip install" instruction from your Dockerfile. The absence of a "requirements.txt" file or the "pip install" command indicates that your application doesn't require any external dependencies to function.
For example → If you have used "pip install wmi" to install the "wmi" package for your Python application, your `requirements.txt` file would typically contain a single line specifying this package.
Here is how your `requirements.txt` file would look like:
```plaintext
wmi
```
This simple `requirements.txt` file lists the specific package "wmi" that needs to be installed when the `pip install -r requirements.txt` command is executed within your Docker container.
Each line in the `requirements.txt` file represents a separate Python package to be installed. The Docker PYTHON RUNTIME will do the work of installing these.
Step 4: Create a `Dockerfile`
1. In your project directory, create a file named `Dockerfile`.
2. Edit the `Dockerfile` to include:
```dockerfile
# Use an official Python runtime as a parent image
FROM python:3.8-slim
# Set the working directory in the container
WORKDIR c:\dockerdemo
— or whatever is the name of the directory your Python app code is in —
# Copy the current directory contents into the container
COPY . .
# Install any dependencies
RUN pip install --no-cache-dir -r requirements.txt
# (Optional) Expose port 80 if your app is a web application
EXPOSE 80
# Run your application
CMD ["python", "./pythonprogram1.py"]
The "CMD" instruction is not something you would type at the command line. Instead, it forms part of a Dockerfile, which is a set of instructions used to create a Docker image.
Here's what "CMD ["python", "./app.py"]" means in the context of a Dockerfile:
→ This line specifies the command that should be executed when the Docker container is started — within the Docker Desktop application — from the containerized image you have provided.
In this case, it indicates that the Python interpreter should run the "app.py" file within the container.
To use this in practice, you would typically create a file named `Dockerfile` containing all the necessary instructions, including the "CMD" instruction, and then build the Docker image using the `docker build` command.
Once the image is built, you can create and start a container from the image using the `docker run` command.
The "CMD" instruction ensures that the specified command is automatically executed when the container is started.
Remember that the Dockerfile is used to:
define the environment and behavior of a Docker container,
the "CMD" instruction is just one part of this definition.
*Note: Adjust the Python version as needed.*
Step 5: Build and Run Your Docker Container
1. Open your CLI Command Line Interface (PowerShell Terminal) and navigate to your project directory.
2. Build your Docker image:
- Run `docker build -t my-python-app .`
3. Run your Docker container:
- For non-web apps: `docker run my-python-app`
- For web apps: `docker run -p 80:80 my-python-app`
Step 6: Volume Mounting for Development (Optional)
1. To see code changes without rebuilding the image, use volume mounting:
- Run `docker run -p 80:80 -v $(pwd):/usr/src/app my-python-app`
### Additional Information
- **Exposing Ports**: Only expose ports necessary for your application (like port 80 for web apps).
- **Networking**: For advanced applications requiring inter-container communication, explore Docker networking features.
- **Docker Compose**: For multi-container applications, consider learning Docker Compose.
### Conclusion
You've now learned the basics of Dockerizing a Python application on Windows! Explore further to understand more complex Docker functionalities and how they can enhance your development workflow.