Detailed usage of Docker Compose

What is Docker Compose

Docker Compose is a python tool for defining and running multi-container Docker applications. It allows you to use a single configuration file to define and configure services for multiple related containers so that they work together.

Install Docker Compose

To this day, Docker Compose is still an external Python tool that needs to be installed on the Docker host.
The following describes how to install the Docker Compose tool under the Linux system.

# 下载二进制文件
# 如果下载失败可以到github上去直接下载二进制文件
sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# 赋予可执行权限
sudo chmod +x /usr/local/bin/docker-compose

# 查看docker compse的版本信息
docker-compse --version

Compose file writing

Docker Compose uses YAML files to define multi-service applications. YAML is a subset of JSON, so JSON can also be used. Docker Compose uses the filename docker-compose.yml by default. Of course, users can also use the -f parameter to specify specific files. Let's take a docker-compose.yml file as an example to illustrate the basic syntax of docker-compose:

# version必须指定
# version 并非定义Docker Compose或Docker引擎的版本号
# 它定义了Compose文件格式(主要是API)的版本,建议使用最新版本
version: "3.5"
# services 用于定义不同的应用服务, 也就是不同的镜像
services:
   # 服务1 
  services1:
    # 从当前目录下找Dockerfile构建新镜像
    build: .
    # 指定容器的名称
    container_name: my-web-container
    # 指定Docker在容器中执行名为app.py的Python脚本作为主程序
    # 可覆盖Dockerfile中的CMD命令
    command: python app.py
    # 指定Docker将容器内(-target)的5000端口映射到主机(published)的5000端口
    ports:
      - target: 5000
        published: 5000
    # 连接到my-net网络    
    networks:
      - my-net
    # 指定Docker将counter-vol卷(source:)挂载到容器内的/code(target:)
    volumes:
        - type: volume
            source: counter-vol
            target: /code
    volumes:
      # 将本机的./host/path目录映射到Docker容器中的/container/path
      - ./host/path:/container/path   
      # 只读映射     
       - /host/path:/container/path:ro
    
    # 给服务设置环境变量 
    environment:
       - ENV_VAR1=value1
       - ENV_VAR2=value2
       - ENV_VAR3=value3   

    # 指定重启策略   
    restart: "no" / always  / on-failure  / unless-stopped
    
  # 服务2          
  services2:
    # Docker可以基于redis:alpine 镜像启动一个独立的名为redis的容器
    image: "redis:alpine"
    
    # 连接到my-net网络
    networks:
      - my-net

# 用于指引Docker创建新的网络
# 默认情况下,Docker Compose会创建bridge网络
# 这是一种单主机网络,只能够实现同一主机上容器的连接
networks:
  # 创建名为my-net的Overlay网络
  # 允许独立的容器连接到该网络上
  my-net:
  driver: overlay
  attachable: true

# volumes用于指引Docker来创建新的卷
# 创建了一个名为data-vol的卷
volumes:
  data-vol:

docker-compose allows some variables to be defined through external files. The definition method is as follows:
In the docker-compose directory, create a file named .env, and define environment variables in the following format:

ENV_VAR1=value1
ENV_VAR2=value2

Use ${ENV_VAR} in the Compose file to refer to the environment variable, so that Compose will automatically load the environment variable in the .env file and pass it to the service

services:
  my-service:
    image: my-image:latest
    environment:
      - ENV_VAR1=${ENV_VAR1}
      - ENV_VAR2=${ENV_VAR2}

For more syntax instructions, see the official documentation:
https://docs.docker.com/compose/compose-file/03-compose-file/

Deploy-manage applications with Docker Compose

Here is an introduction to the common commands when using Docker Compose to manage and deploy images.

# 在compose的目录下执行下面的命令, 启动应用
# 它会构建所需的镜像,创建网络和卷,并启动容器
# docker-compose up会查找名为docker-compose.yml 或docker-compose.yaml 的Compose文件
docker-compose up &

# 停止和关闭应用
docker-compose down 

# 指定特定的配置文件
# 使用-d 参数在后台启动应用
docker-compose -f prod-equus-bass.yml up

# 查看应用的状态
docker-compose ps

# 列出各个服务(容器)内运行的进程
docker-compose top

# 命令会停止应用,但并不会删除资源
docker-compose stop

# 删除应用相关的容器和网络  但不会删除卷和镜像
docker-compose rm 

# 重启应用
docker-compose restart 

# 当第一次部署该应用的时候,Docker Compose会检查是否有同名的卷存在。
# 如果不存在,则会创建它。可使用docker volume ls 查看

Guess you like

Origin blog.csdn.net/yang1fei2/article/details/132215605