Step 1: Install Docker Desktop on Windows 11
To begin, you need to install Docker Desktop. Here’s how:
-
Download Docker: Visit the Docker Hub and download the latest version of Docker Desktop for Windows.
-
Install Docker: Run the downloaded installer. Follow the on-screen instructions. Make sure to enable the WSL 2 feature when prompted as it’s essential for running Docker on Windows 11.
-
Complete the Installation: After installation, reboot your system if prompted. Launch Docker Desktop and ensure it’s running by checking the Docker icon in your system tray.
-
Verify Installation: Open Command Prompt (cmd) or PowerShell and type:
docker --versionThis command should display the installed version of Docker.
Step 2: Enable WSL 2
If WSL 2 is not enabled, follow these steps:
-
Open PowerShell as Administrator.
-
Run the WSL Installation Command:
wsl --set-default-version 2This command sets WSL 2 as the default version.
-
Install a Linux Distribution: Go to the Microsoft Store and download a Linux distribution like Ubuntu.
Step 3: Install Python
You need Python to run your application inside Docker. Follow these steps:
-
Open WSL: Launch your installed Linux distribution.
-
Update the Package List:
sudo apt update -
Install Python: Use the following command to install Python:
sudo apt install python3 python3-pip -
Verify Installation: Check the Python installation by running:
python3 --version
Step 4: Create a Sample Python Application
Before you can create a Docker container, you need some Python code to run.
-
Navigate to Your Project Directory:
mkdir ~/docker-python-app cd ~/docker-python-app -
Create a Python File: Create a file named
app.py:nano app.py -
Write Sample Code: Add the following code to
app.py:from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, Docker with Python!' if __name__ == '__main__': app.run(host='0.0.0.0') -
Save and Exit: Press
CTRL + X, thenY, and hitEnterto save the file.
Step 5: Create a Requirements File
Next, create a requirements.txt file to specify the packages needed.
-
Create requirements.txt:
nano requirements.txt -
Add Required Packages: Add the following line:
flask -
Save and Exit: Save the file as you did earlier.
Step 6: Write a Dockerfile
This file contains specifications on how to build your Docker image.
-
Create a Dockerfile:
nano Dockerfile -
Add Dockerfile Instructions: Add the following instructions:
# Use an official Python runtime as a parent image FROM python:3.9-slim # Set the working directory in the container WORKDIR /usr/src/app # Copy the current directory contents into the container at /usr/src/app COPY . . # Install any needed packages specified in requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Make port 5000 available to the world outside this container EXPOSE 5000 # Define the command to run your app using CMD which defines your runtime CMD ["python3", "app.py"] -
Save and Exit.
Step 7: Build the Docker Image
Now, you will build the Docker image from the Dockerfile.
-
Build the Image: Run the following command to build your Docker image:
docker build -t python-docker-app . -
Wait for the Build to Complete: This process may take some time as Docker downloads the base image and installs the dependencies.
Step 8: Run the Docker Container
Once the image is built, you can run it.
-
Run the Container:
docker run -d -p 5000:5000 python-docker-app -
Verify Running Container: To see if your container is running, execute:
docker psThis command will list running containers. Look for
python-docker-appin the list.
Step 9: Access Your Application
You should now be able to access your application from your browser.
-
Open a Web Browser: Go to
http://localhost:5000. -
View the Result: You should see “Hello, Docker with Python!” displayed in your browser.
Step 10: Managing Your Docker Container
Managing containers is crucial for deploying applications.
-
Stop the Container: If you need to stop your container, use:
docker stop <container_id> -
Remove the Container: Once stopped, removing the container is easy:
docker rm <container_id>
Step 11: Cleanup Unused Resources
To clean up unused images and containers, get in the habit of running:
docker system prune -f
This command helps free up disk space on your machine.
Step 12: Utilize Docker Compose (Optional)
For more complex applications, Docker Compose can help manage multi-container applications.
-
Create a docker-compose.yml File:
nano docker-compose.yml -
Add Configuration: Include the following YAML configuration:
version: '3' services: app: build: . ports: - "5000:5000" -
Run with Docker Compose:
docker-compose up -
Access the Application: Again, navigate to
http://localhost:5000to see your app.
Step 13: Review Docker Desktop GUI
Docker Desktop provides a user-friendly GUI to manage your containers.
-
Open Docker Desktop: Click on the Docker icon in your system tray.
-
Navigate to Containers/Apps: Here, you can see running containers, images, and perform actions like stopping or removing them.
Step 14: Learn and Explore More
Continue to enhance your knowledge of Docker and Python. Resources such as the Docker Documentation or Python Flask documentation can help with advanced features and best practices to improve your applications.