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 Compose

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can define the entire application stack, including services, networks, and volumes, in a single YAML file. This simplifies the process of deploying and managing complex applications with multiple interconnected components. In this tutorial, we'll cover the basics of Docker Compose, including creating a docker-compose.yml file and using the docker-compose command.

  • Installation:

Docker Compose is not included in the default Docker installation. To install Docker Compose, follow the instructions in the official documentation: https://docs.docker.com/compose/install/

  • Create a docker-compose.yml file:

To define your application stack, create a docker-compose.yml file in the root directory of your project. The file should include the following sections:

  • version: The version of the Docker Compose file format.
  • services: A list of services that make up your application.
  • networks: (Optional) A list of custom networks to be created for your application.
  • volumes: (Optional) A list of named volumes to be created and used by your services.

Here's a basic example of a docker-compose.yml file:

version: "3.9"
services:
  web:
    build: .
    ports:
      - "80:80"
  db:
    image: "postgres:latest"
    environment:
      - POSTGRES_USER=myuser
      - POSTGRES_PASSWORD=mypassword
networks:
  default:
    external:
      name: my-network
volumes:
  db-data:

In this example, we define two services: web and db. The web service is built from the current directory, while the db service uses the official postgres image. We also define a custom network and a named volume.

  • Docker Compose commands:

Some common Docker Compose commands include:

  • docker-compose up: Build, (re)create, and start all services and containers defined in the docker-compose.yml file. Use the -d option to run containers in the background (detached mode).

    docker-compose up -d
    
  • docker-compose down: Stop and remove all services, containers, networks, and volumes defined in the docker-compose.yml file.

    docker-compose down
    
  • docker-compose ps: List the containers managed by Docker Compose.

    docker-compose ps
    
  • docker-compose logs: View the logs of all services and containers managed by Docker Compose. Use the -f option to follow the log output.

    docker-compose logs -f
    
  • docker-compose exec: Execute a command in a running container.

    docker-compose exec SERVICE_NAME COMMAND
    

    Replace SERVICE_NAME with the name of the service, and COMMAND with the command you want to execute.

  • Updating and scaling services:

To update a service, modify its definition in the docker-compose.yml file and run docker-compose up -d. Docker Compose will recreate the affected containers and apply the changes.

To scale a service, use the --scale option followed by the service name and the desired number of replicas:

docker-compose up -d --scale SERVICE_NAME=NUM_REPLICAS

Replace SERVICE_NAME with the name of the service, and NUM_REPLICAS with the desired number of replicas.

This tutorial provides a basic introduction to Docker Compose, including creating a docker-compose.yml file and using the docker-compose command.

  1. Defining Services in Docker Compose:

    • Description: Docker Compose allows you to define services, specifying their image, dependencies, ports, and configurations in a docker-compose.yml file.
    • Code: Example of a docker-compose.yml file:
      version: '3'
      services:
        web:
          image: nginx:latest
        db:
          image: postgres:latest
      
  2. Docker Compose Environment Variables:

    • Description: Use environment variables in the docker-compose.yml file to pass configurations to services.
    • Code: Example of using environment variables in Docker Compose:
      version: '3'
      services:
        web:
          image: nginx:latest
          environment:
            - MY_ENV_VARIABLE=value
      
  3. Docker Compose Volumes:

    • Description: Docker Compose volumes allow persisting data outside containers and sharing data between services.
    • Code: Example of using volumes in Docker Compose:
      version: '3'
      services:
        web:
          image: nginx:latest
          volumes:
            - ./app:/app
      
  4. Docker Compose Scaling Services:

    • Description: Use the docker-compose up --scale command to scale services horizontally.
    • Code: Example of scaling services with Docker Compose:
      docker-compose up --scale web=3
      
  5. Docker Compose Build and Up Commands:

    • Description: Use docker-compose build to build images defined in the docker-compose.yml file, and docker-compose up to start the services.
    • Code: Example of building and starting services with Docker Compose:
      docker-compose build
      docker-compose up -d
      
  6. Docker Compose Down and Stop Commands:

    • Description: Use docker-compose down to stop and remove containers, networks, and volumes defined in the docker-compose.yml file. docker-compose stop only stops services.
    • Code: Example of stopping and removing services with Docker Compose:
      docker-compose down