Detailed explanation of docker-compose files and common commands

docker-compose.yml

Docker Compose is a tool for defining and running multi-container Docker applications that uses YAML files to configure the application's services, network, and volumes. In a complete docker-compose.ymlfile, you can define multiple services, each containing various configuration options. Here is an example file explained in detail docker-compose.yml, and the various sections it contains:

version: '3'  # Docker Compose 文件版本

services:  # 服务定义的部分开始
  web:  # 第一个服务名称,可以自定义
    image: nginx:latest  # 使用的镜像及版本
    container_name: my_web_app  # 容器的自定义名称
    ports:  # 端口映射配置
      - "8080:80"  # 将容器内部的 80 端口映射到主机的 8080 端口
    volumes:  # 卷挂载配置
      - ./html:/usr/share/nginx/html  # 挂载本地 ./html 目录到容器的 /usr/share/nginx/html

  app:  # 第二个服务名称
    build:  # 构建镜像的配置
      context: ./app  # Dockerfile 所在目录
      dockerfile: Dockerfile.prod  # 自定义 Dockerfile 文件名
    container_name: my_app  # 容器的自定义名称
    environment:  # 环境变量配置
      - DEBUG=True  # 设置环境变量 DEBUG 为 True
    depends_on:  # 依赖关系配置
      - db  # 依赖于名为 "db" 的服务

  db:  # 第三个服务名称
    image: postgres:12  # 使用的 PostgreSQL 镜像及版本
    container_name: my_postgres_db  # 容器的自定义名称
    environment:  # 环境变量配置
      - POSTGRES_PASSWORD=secret  # 设置 PostgreSQL 密码为 "secret"
      - POSTGRES_DB=mydatabase  # 创建数据库名为 "mydatabase"

networks:  # 网络定义的部分开始
  my_network:  # 自定义网络名称
    driver: bridge  # 网络驱动程序,默认为 bridge
    ipam:  # IP 地址管理配置
      config:  # IP 配置
        - subnet: 172.28.0.0/16  # 子网配置

volumes:  # 卷定义的部分开始
  my_volume:  # 自定义卷名称
    driver: local  # 卷驱动程序,默认为 local
    driver_opts:  # 驱动程序选项配置
      type: none  # 驱动程序类型
      device: /mydata  # 卷的本地路径

This example contains a complete docker-compose.ymlfile with its various parts explained in detail:

  • version: Specifies the version of the Docker Compose file. In the example, version 3 is used.

  • services: This is the section that defines each service. Each service can contain the following properties:

    • image: Specifies the image and its version to be used.

    • container_name: Set a custom name for the container.

    • ports: Used to map container internal ports to ports on the host.

    • volumes: Used to mount directories or volumes on the host into the container.

    • build: If you need to build a custom image, you can use buildthe attribute to specify the path of the Dockerfile.

    • environment: Set environment variables inside the container.

    • depends_on: Defines dependencies between services. In the example, appthe service depends on dbthe service.

  • networks: This is the section where custom networks are defined. You can define multiple custom networks and specify network drivers, IP address management configuration, and more.

  • volumes: This is the section where custom volumes are defined. You can define multiple custom volumes and specify the volume's driver, driver options, and more.

This example includes a complete docker-compose.ymlfile that allows you to customize the configuration of services, networks, and volumes to suit your needs. The power of Docker Compose files is that they make it easy to manage the deployment and configuration of complex multi-container applications.

Docker-compose common commands

Docker Compose provides a set of commands to manage the life cycle of container applications. Here are some commonly used commands and their explanations:

  1. docker-compose up: Start the application. It creates and starts docker-compose.ymlall services defined in the file.

  2. docker-compose up -d: Launch the application in background mode, i.e. no log output will be displayed on the terminal.

  3. docker-compose down: Stop and remove all containers, networks, and volumes for the application.

  4. docker-compose ps: Lists running containers.

  5. docker-compose logs: View the application’s log output.

  6. docker-compose build:Builds docker-compose.ymlthe service defined in the file. If you modify the Dockerfile or application code, you need to rebuild the image.

  7. docker-compose stop: Stops a running application but does not remove the container.

  8. docker-compose start: Start a stopped application.

  9. docker-compose restart: Restart the application's container.

  10. docker-compose exec <service> <command>: Execute the command in the specified service container. For example, docker-compose exec app bashyou can appopen a Bash terminal inside the service container.

  11. docker-compose down -v: Stop and remove all of the application's containers, networks, volumes, and associated data volumes. Use with caution, it will delete data.

  12. docker-compose pull: Pulls docker-compose.ymlthe images required for all services defined in the file, but does not start the container.

Guess you like

Origin blog.csdn.net/Mrxiao_bo/article/details/132916489