Docker Tutorial
Docker Installation
How to use Docker
Docker Instance
Docker Container Lifecycle Command
Docker Container Operation Command
Docker Container rootfs Command
Docker Mirror Repository Command
Docker Local Image Management Command
Docker info|version Command
In this tutorial, you'll learn how to create a Docker container running Python, a popular high-level programming language widely used for web development, data analysis, artificial intelligence, and more. We'll use the official Python image provided by the Docker community.
Prerequisites:
Pull the official Python image:
To pull the official Python image, run the following command:
docker pull python
This will download the latest version of the Python image to your system.
Create a sample Python application:
First, create a new directory for your Python application:
mkdir python-app cd python-app
Next, create a new file named app.py
in the python-app
directory with the following content:
def main(): print("Hello, World!") if __name__ == "__main__": main()
This is a simple Python script that prints "Hello, World!" when executed.
Create a new container with Python:
To create a new Docker container running your Python application, use the docker run
command. We'll mount the local python-app
directory as a volume inside the container:
docker run --name python-container -v $(pwd):/usr/src/app -w /usr/src/app -it python python app.py
This command creates a new container named python-container
, mounts the local python-app
directory as a volume at /usr/src/app
inside the container, sets the working directory to /usr/src/app
, and runs the container in interactive mode (-it
flag) executing the app.py
file using Python.
You should see the "Hello, World!" message printed in the terminal.
Running the container in detached mode (optional):
If you want to run your Python container in the background (detached mode), you can modify the docker run
command:
docker run --name python-container -v $(pwd):/usr/src/app -w /usr/src/app -d python python app.py
The -d
flag runs the container in detached mode. To view the output of the Python script, you can use the docker logs
command:
docker logs python-container
You should see the "Hello, World!" message in the output.
In this tutorial, we covered how to create a Docker container running a simple Python application, execute the application within the container, and view the output. You can now use this containerized Python environment for development, testing, or deployment purposes.
Running Python in Docker Container:
docker run -it --rm python:latest
How to Create a Docker Image with Python:
FROM python:latest # Add custom configurations or install dependencies
Docker-Compose Python Installation:
version: '3' services: python: image: python:latest command: python -m http.server 8000 ports: - "8000:8000"
Python Dockerfile Example:
FROM python:latest WORKDIR /usr/src/app COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY . .
Setting Up Python in a Docker Environment:
version: '3' services: python: image: python:latest command: python -m http.server 8000 ports: - "8000:8000"
Configuring Python in Docker-Compose:
version: '3' services: python: image: python:latest command: python -m http.server 8000 ports: - "8000:8000" volumes: - ./app:/usr/src/app
Docker Python Image Customization:
FROM python:latest WORKDIR /usr/src/app COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY . .
Docker Python Volume Mounts:
version: '3' services: python: image: python:latest command: python -m http.server 8000 volumes: - ./app:/usr/src/app
Custom Python Image Creation in Docker:
FROM python:latest WORKDIR /usr/src/app COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY . .
Docker Swarm and Python Service:
version: '3' services: python: image: python:latest command: python -m http.server 8000 deploy: replicas: 3 # Other Swarm deployment configurations
Troubleshooting Python in Docker:
docker logs <python_container_id_or_name>