docker compose最全文档

#官方文档地址
https://docs.docker.com/compose/

1.docker compose安装

#自动
sudo yum update
sudo yum install docker-compose-plugin
docker compose version

 
#手动安装
DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
mkdir -p $DOCKER_CONFIG/cli-plugins
curl -SL https://github.com/docker/compose/releases/download/v2.11.1/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
此命令下载最新版本的 Docker Compose(从 Compose 发布存储库)并在$HOME目录下为活动用户安装 Compose。

#安装:
Docker Compose 为您系统上的所有用户,替换~/.docker/cli-plugins为/usr/local/lib/docker/cli-plugins.
不同版本的 Compose,替换v2.11.1为您要使用的 Compose 版本。

#对二进制文件应用可执行权限:
chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose

#或者,如果您选择为所有用户安装 Compose:
sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose

#测试安装
docker compose version

2.docker compose配置文件编写

[root@localhost shell]# cat docker-compose-test.yml 
version: '3.9'
services: 
  #elastic
  es: 
    image: 172.16.4.17:8090/public/elastic:7.1.1
    restart: always
    deploy:
      resources: 
        limits:
          memory: '4G'
        reservations: 
          memory: '3G'
    container_name: es
    environment: 
      - TZ=Asia/Shanghai
      - ELASTIC_PASSWORD=ytx@1234
    volumes: 
      - type: bind
        source: /etc/localtime
        target: /etc/localtime:ro
      - type: bind
        source: /data/aibox-common/elastic/data
        target: /usr/share/elasticsearch/data
      - type: bind
        source: /data/aibox-common/elastic/config
        target: /usr/share/elasticsearch/config
    ports: 
      - 9200:9200
      - 9300:9300
    network_mode: "host"
  #kibanan
  kibanan: 
    image: 172.16.4.17:8090/public/kibana:7.1.1
    restart: always
    deploy:
      resources:
        limits:
          memory: '2G'
        reservations:
          memory: '1G'
    container_name: kibanan
    environment:
      - TZ=Asia/Shanghai
    volumes: 
      - type: bind
        source: /etc/localtime
        target: /etc/localtime:ro
      - type: bind
        source: /data/aibox-common/kibana/config/kibana.yml
        target: /usr/share/kibana/config/kibana.yml
    ports: 
      - 5601:5601
    network_mode: "host"
  #mysql
  mysql: 
    image: 172.16.4.17:8090/public/mysql:5.7.29
    restart: always
    deploy:
      resources:
        limits:
          memory: '3G'
        reservations:
          memory: '2G'
    container_name: mysql
    environment:
      - TZ=Asia/Shanghai
      - MYSQL_ROOT_PASSWORD=ytx@1234
    volumes:
      - type: bind
        source: /etc/localtime
        target: /etc/localtime:ro
      - type: bind
        source: /data/aibox-common/mysql/config/my.cnf
        target: /etc/mysql/my.cnf
      - type: bind
        source: /data/aibox-common/mysql/datadir
        target: /var/lib/mysql
    ports:
      - 3306:3306
    network_mode: "host"
  #redis
  redis:
    image: 172.16.4.17:8090/public/redis:6.0
    restart: always
    deploy:
      resources:
        limits:
          memory: '3G'
        reservations:
          memory: '2G'
    container_name: redis
    volumes: 
      - type: bind
        source: /etc/localtime
        target: /etc/localtime:ro
      - type: bind
        source: /data/aibox-common/redis/conf/redis.conf
        target: /usr/local/etc/redis/redis.conf
    ports: 
      - 6379:6379
    command: redis-server /usr/local/etc/redis/redis.conf
    network_mode: "host"
  #nginx
  nginx:
    image: 172.16.4.17:8090/public/nginx:v1.20.1
    restart: always
    container_name: nginx
    volumes:
      - type: bind
        source: /etc/localtime
        target: /etc/localtime:ro
      - type: bind
        source: /data/service/nginx/conf
        target: /data/service/nginx/conf
      - type: bind
        source: /data/service/nginx/logs 
        target: /data/service/nginx/logs 
      - type: bind
        source: /data/service/nginx/html
        target: /data/service/nginx/html
    ports:
      - 80:80
      - 28882:28882
    network_mode: "host"
  #device-server
  device-server:
    image: #镜像地址
    container_name: device-server
    restart: always
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - type: bind
        source: /etc/localtime
        target: /etc/localtime:ro
      - type: bind
        source: /data/aibox-common/device-server/config
        target: /device-server/config 
      - type: bind
        source: /data/aibox-common/device-server/logs
        target: /device-server/logs 
      - type: bind 
        source: /data/pictures/
        target: /device-server/pic/
    ports:
      - 24444:24444
      - 23333:23333
    depends_on:
      - mysql
      - redis
      - es
    network_mode: "host"
  #cm-server
  cm-server:
    image: #镜像地址
    container_name: cm-server
    restart: always
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - type: bind
        source: /etc/localtime
        target: /etc/localtime:ro
      - type: bind
        source: /data/aibox-common/cm-server/config
        target: /aibox-control-manage/config
      - type: bind
        source: /data/aibox-common/cm-server/logs
        target: /aibox-control-manage/logs
      - type: bind
        source: /data/pictures/
        target: /aibox-control-manage/pic
      - type: bind
        source: /data/aibox-common/cm-server/rlface_cpu
        target: /aibox-control-manage/rlface_cpu
    ports:
      - 28877:28877
    depends_on:
      - mysql
      - redis
      - es
      - device-server
    network_mode: "host"
  #device-agent
  device-agent:
    image: #镜像地址
    container_name: device-agent
    restart: always
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - type: bind
        source: /etc/localtime
        target: /etc/localtime:ro
      - type: bind
        source: /data/aibox-common/ai-server
        target: /home/nvidia/aibox/ai-server 
      - type: bind
        source: /data/aibox-common/device-agent/logs
        target: /home/nvidia/aibox/logs 
      - type: bind
        source: /data/aibox-common/aimodel
        target: /home/nvidia/aibox/aimodel 
      - type: bind
        source: /data/aibox-common/common
        target: /home/nvidia/aibox/common
    ports:
      - 28888:28888
    depends_on:
      - mysql
      - redis
      - es
      - device-server
      - cm-server
    network_mode: "host"
  #ai-server
  ai-server:
    image: #镜像地址
    container_name: ai-server
    restart: always
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - type: bind
        source: /etc/localtime
        target: /etc/localtime:ro
      - type: bind
        source: /data/aibox-common/ai-server/logs
        target: /home/nvidia/aibox/logs 
      - type: bind
        source: /data/aibox-common/aimodel
        target: /home/nvidia/aibox/aimodel
      - type: bind
        source: /data/aibox-common/common
        target: /home/nvidia/aibox/common
    ports:
      - 28889:28889
    depends_on:
      - mysql
      - redis
      - es
      - device-server
      - cm-server
      - device-agent
    ulimits:
      core: 0
    deploy:
      resources:
        reservations:
          devices:
            - capabilities: [gpu]   #使用所有gpu
    network_mode: "host"

3.docker compose GPU 配置

ai-server:
    image: #镜像地址
    container_name: ai-server
    restart: always
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - type: bind
        source: /etc/localtime
        target: /etc/localtime:ro
      - type: bind
        source: /data/aibox-common/ai-server/logs
        target: /home/nvidia/aibox/logs 
      - type: bind
        source: /data/aibox-common/aimodel
        target: /home/nvidia/aibox/aimodel
      - type: bind
        source: /data/aibox-common/common
        target: /home/nvidia/aibox/common
    ports:
      - 28889:28889
    depends_on:
      - mysql
      - redis
      - es
      - device-server
      - cm-server
      - device-agent
    ulimits:
      core: 0
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
            device_ids: ['0', '3']   #使用0、3号gpu
            capabilities: [gpu]
network_mode: "host"

4.docker compose常用命令

0.官方文档:https://docs.docker.com/compose/reference/overview/

docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]

部分命令选项如下:

-f,–file:指定使用的 Compose 模板文件,默认为 docker-compose.yml,可以多次指定,指定多个 yml;
-p, --project-name:指定工程名称,默认使用 docker-compose.yml 文件所在目录的名称;
-v:打印版本并退出;
–log-level:定义日志等级(DEBUG, INFO, WARNING, ERROR, CRITICAL)。

1.help
docker-compose -help查看帮助。

2.config
docker-compose config -q验证docker-compose.yml文件。当配置正确时,不输出任何内容,当配置错误时,输出错误信息。

3.pull
docker-compose pull拉取服务依赖的镜像。

# 拉取工程中所有服务依赖的镜像
docker-compose pull
# 拉取工程中 nginx 服务依赖的镜像
docker-compose pull nginx
# 拉取镜像过程中不打印拉取进度信息
docker-compose pull -q

4.up
docker-compose up创建并启动所有服务的容器。指定多个yml加-f选项。以守护进程模式运行加-d选项。

# 前台启动
docker-compose up
# 后台启动
docker-compose up -d
# -f 指定使用的 Compose 模板文件,默认为 docker-compose.yml,可以多次指定,指定多个 yml
docker-compose -f docker-compose.yml up -d

5.logs
docker-compose logs查看服务容器的输出日志。默认情况下,docker-compose将对不同的服务输出使用不同的颜色来区分。可以通过--no-color来关闭颜色。

# 输出日志,不同的服务输出使用不同的颜色来区分
docker-compose logs
# 跟踪日志输出
docker-compose logs -f
# 关闭颜色
docker-compose logs --no-color

6.ps
docker-compose ps列出工程中所有服务的容器。

# 列出工程中所有服务的容器
docker-compose ps
# 列出工程中指定服务的容器
docker-compose ps nginx

7.run
docker-compose run在指定服务容器上执行一个命令。

# 在工程中指定服务的容器上执行 echo "helloworld"
docker-compose run nginx echo "helloworld"

8.exec
docker-compose exec进入服务容器。

# 进入工程中指定服务的容器
docker-compose exec nginx bash
# 当一个服务拥有多个容器时,可通过 --index 参数进入到该服务下的任何容器
docker-compose exec --index=1 nginx bash

9.pause
docker-compose pause暂停服务容器

# 暂停工程中所有服务的容器
docker-compose pause
# 暂停工程中指定服务的容器
docker-compose pause nginx

10.unpause
docker-compose unpause恢复服务容器。

# 恢复工程中所有服务的容器
docker-compose unpause
# 恢复工程中指定服务的容器
docker-compose unpause nginx

11.restart
docker-compose restart重启服务容器。

# 重启工程中所有服务的容器
docker-compose restart
# 重启工程中指定服务的容器
docker-compose restart nginx

12.start
docker-compose start启动服务容器。

# 启动工程中所有服务的容器
docker-compose start
# 启动工程中指定服务的容器
docker-compose start nginx

13.stop
docker-compose stop停止服务容器。

# 停止工程中所有服务的容器
docker-compose stop
# 停止工程中指定服务的容器
docker-compose stop nginx

14.kill
docker-compose kill通过发送SIGKILL信号停止指定服务的容器。

# 通过发送 SIGKILL 信号停止工程中指定服务的容器
docker-compose kill nginx

15.rm
docker-compose rm 删除服务(停止状态)容器。

# 删除所有(停止状态)服务的容器
docker-compose rm
# 先停止所有服务的容器,再删除所有服务的容器
docker-compose rm -s
# 不询问是否删除,直接删除
docker-compose rm -f
# 删除服务容器挂载的数据卷
docker-compose rm -v
# 删除工程中指定服务的容器
docker-compose rm -sv nginx

16.down
停止并删除所有服务的容器、网络、镜像、数据卷。

# 停止并删除工程中所有服务的容器、网络
docker-compose stop
# 停止并删除工程中所有服务的容器、网络、镜像
docker-compose down --rmi all
# 停止并删除工程中所有服务的容器、网络、数据卷
docker-compose down -v

17.images
docker-compose images打印服务容器所对应的镜像。

# 打印所有服务的容器所对应的镜像
docker-compose images
# 打印指定服务的容器所对应的镜像
docker-compose images nginx

18.port
docker-compose port打印指定服务容器的某个端口所映射的宿主机端口。

docker-compose port nginx 80

19.top
docker-compose top显示正在运行的进程。

# 显示工程中所有服务的容器正在运行的进程
docker-compose top
# 显示工程中指定服务的容器正在运行的进程
docker-compose top nginx
  

  

posted @ 2022-09-24 21:23  Leonardo-li  阅读(1553)  评论(0编辑  收藏  举报