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

Docker install Python

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.

  1. Prerequisites:

    • Install Docker on your system
  2. 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.

  3. 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.

  4. 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.

  5. 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.

  1. Running Python in Docker Container:

    • Description: Run a Python application in a Docker container for a consistent and isolated execution environment.
    • Code Example:
      docker run -it --rm python:latest
      
  2. How to Create a Docker Image with Python:

    • Description: Create a custom Docker image with Python installed using a Dockerfile.
    • Code Example (Dockerfile):
      FROM python:latest
      # Add custom configurations or install dependencies
      
  3. Docker-Compose Python Installation:

    • Description: Use Docker Compose to define and run multi-container Docker applications, including Python.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        python:
          image: python:latest
          command: python -m http.server 8000
          ports:
            - "8000:8000"
      
  4. Python Dockerfile Example:

    • Description: A Dockerfile example for creating a custom Python image with additional configurations or application files.
    • Code Example (Dockerfile):
      FROM python:latest
      WORKDIR /usr/src/app
      COPY requirements.txt ./
      RUN pip install --no-cache-dir -r requirements.txt
      COPY . .
      
  5. Setting Up Python in a Docker Environment:

    • Description: Configure Python settings, such as dependencies or application startup commands, within a Docker environment.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        python:
          image: python:latest
          command: python -m http.server 8000
          ports:
            - "8000:8000"
      
  6. Configuring Python in Docker-Compose:

    • Description: Customize Python configurations using Docker Compose for a multi-container environment.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        python:
          image: python:latest
          command: python -m http.server 8000
          ports:
            - "8000:8000"
          volumes:
            - ./app:/usr/src/app
      
  7. Docker Python Image Customization:

    • Description: Customize the Python image by adding configurations, dependencies, or specific application files.
    • Code Example (Dockerfile):
      FROM python:latest
      WORKDIR /usr/src/app
      COPY requirements.txt ./
      RUN pip install --no-cache-dir -r requirements.txt
      COPY . .
      
  8. Docker Python Volume Mounts:

    • Description: Use Docker volumes to mount external directories or Python scripts into Python containers for code persistence.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        python:
          image: python:latest
          command: python -m http.server 8000
          volumes:
            - ./app:/usr/src/app
      
  9. Custom Python Image Creation in Docker:

    • Description: Create a custom Python image with specific configurations and dependencies tailored to your application.
    • Code Example (Dockerfile):
      FROM python:latest
      WORKDIR /usr/src/app
      COPY requirements.txt ./
      RUN pip install --no-cache-dir -r requirements.txt
      COPY . .
      
  10. Docker Swarm and Python Service:

    • Description: Deploy Python as a service in a Docker Swarm to scale and manage multiple Python containers.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        python:
          image: python:latest
          command: python -m http.server 8000
          deploy:
            replicas: 3
            # Other Swarm deployment configurations
      
  11. Troubleshooting Python in Docker:

    • Description: Troubleshoot Python-related issues in Docker by examining logs, checking configurations, and verifying container settings.
    • Code Example:
      docker logs <python_container_id_or_name>