docker-compose
docker-compose
docker-compose 是docker官方的单机编排工具,适用于多个容器配合工作的场景,Compose项目由Python编写﹐实际上调用了Docker服务提供的API来对容器进管理
[INFO]
Compose 中有两个重要的概念︰
服务(service):一个应用的容器﹐实际上可以包括若干运行相同镜像的容器实例。
项目(project)∶由一组关联的应用容器组成的一个完整业务单元﹐在docker-compose.yml文件中定义。Compose的默认管理对象是项目﹐通过子命令对项目中的一组容器进行便捷地生命周期管理。
安装docker-compose
方法一:通过仓库安装
pip install docker-compose
yum install docker-compose
方法二:二进制安装
方法三:在容器中运行
docker-compose version
我们来熟悉下docker-compose.yml 的基本格式
wordpress:
images: wiki:latest
name: wordpress
restart: always
ports: "80:80"
mysql:
images: 1209233066/mysql:v8.1
volumes: /var/lib/mysql/:/var/lib/mysql/
restart: always
env: MYSQL_ROOT_PASSWORD=123
name: mysql
net: "container:wordpress"
接下来一步步构建自己的docker-compese.yml项目
-
Version
声明项目使用的版本,不声明也不会出现错误version: "3"
-
services
每一个需要启动的容器都是一个服务version: "3" services: python: image: python
-
command
命令会覆盖image中的CMD 命令,如果不声明默认使用image的CMDversion: "3" services: web: image: python command: /bin/sh -c "python -m http.server" #command: ["python","-m","http.server"]
-
ports
指定了端口的映射version: "3" services: web: image: python ports: - "500:8000" command: /bin/sh -c "python -m http.server" #command: ["python","-m","http.server"]
-
working_dir
version: "3" services: web: image: python ports: - "500:8000" # 覆盖image中的WORKDIR working_dir: /tmp command: /bin/sh -c "python -m http.server"
-
build
和image
指定容器使用那个镜像或dockerfileversion: "3" services: web: build: . ports: ["500:80"] # 覆盖image中的WORKDIR working_dir: /root # 覆盖镜像中的CMD command: /bin/sh -c "python -m http.server 80"
# Dockerfile FROM python WORKDIR /bin CMD ["python","-m","http.server"]
-
expose
指定容器对外暴露的端口version: "3" services: web: build: . expose: - "80" ports: ["500:80"] # 覆盖image中的WORKDIR working_dir: /root # 覆盖镜像中的CMD command: /bin/sh -c "python -m http.server 80"
# Dockerfile FROM python WORKDIR /bin CMD ["python","-m","http.server"]
-
volumes
[INFO]
docker-compose down --volumes #移除挂载
version: "3" services: web: build: . expose: - "8000" ports: - "500:8000" volumes: - "/tmp:/tmp" working_dir: "/tmp" command: /bin/sh -c "python -m http.server"
-
network_mode
host 、bridge、none、container:containernameversion: "3" services: web: image: python expose: - "8000" ports: - "500:8000" volumes: - "/tmp:/tmp" working_dir: "/tmp" command: /bin/sh -c "python -m http.server" network_mode: host
-
resatart
version: "3" services: web: image: python expose: - "8000" ports: - "500:8000" volumes: - "/tmp:/tmp" working_dir: "/tmp" command: /bin/sh -c "python -m http.server" network_mode: host restart: unless-stopped #restart: always
-
pid
与宿主机的共享ipc名称空间version: "3" services: web: image: python expose: - "8000" ports: - "500:8000" volumes: - "/tmp:/tmp" working_dir: "/tmp" command: /bin/sh -c "python -m http.server" network_mode: host restart: unless-stopped #restart: always pid: host
-
depends_on
-
links
-
环境变量
- 从文件中加载
- 从系统变量中获取
- 容器中设定变量
- 在容器中指定envfile
- 在使用docker-compose -e
[INFO]
1.28版本之前从当前目录中查找.env
1.28 之后从project 的目录下寻找.env
可以通过
docker-compose --env-file ./config/.env.dev up
指定环境变量的文件# 从文件中加载 cat .env ContainerPort=8000 port=500
version: "3" services: web: build: python expose: - "${ContainerPort}" ports: - "${port}:${ContainerPort}" volumes: - "/tmp:/tmp" working_dir: "/tmp" command: /bin/sh -c "python -m http.server"
可以通过
docker-compose config
验证变量的解析情况# 从系统变量中获取变量 ]# export port=1000
version: "3" services: web: build: python expose: - "${port}" ports: - "${port}:8000" volumes: - "/tmp:/tmp" working_dir: "/tmp" command: /bin/sh -c "python -m http.server"
可以通过
docker-compose config
验证变量的解析情况# 容器中设定变量environment version: "3" services: web: build: python environment: # 可以只用key 没有value - ContainerPort - port=500 expose: - "${port}" ports: - "${port}:8000" volumes: - "/tmp:/tmp" working_dir: "/tmp" command: /bin/sh -c "python -m http.server"
可以通过
docker-compose config
验证变量的解析情况# 在容器中指定envfile version: "3" services: web: build: python env_file: - ./web-variables.env expose: - "${port}" ports: - "${port}:8000" volumes: - "/tmp:/tmp" working_dir: "/tmp" command: /bin/sh -c "python -m http.server"
可以通过
docker-compose config
验证变量的解析情况#在使用docker-compose -e docker-compose run -e DEBUG=1 web python console.py
-
profiles
[INFO]
当docker-compose up 时不启用带有profiles标签的服务
docker-compose --profile profile的标签 up 才会启用
如下例子docker-compose up
只会启动backend和db 容器,
docker-compose --profile frontend --profile debug up
启动所有容器
COMPOSE_PROFILES=frontend,debug docker-compose up
启动所有容器version: "3.9" services: frontend: image: frontend profiles: ["frontend"] phpmyadmin: image: phpmyadmin depends_on: - db profiles: - debug backend: image: backend db: image: mysql
简单的web负载均衡事例
version: '3'
services:
lb:
image: nginx
ports:
- "500:80"
volumes:
- /tml:/html
- .:/etc/nginx/conf.d/
links:
- web
- web1
- web2
command: ["nginx","-g","daemon off;"]
web:
image: python
command: ["python","-m","http.server"]
expose:
- "8000"
working_dir: "/tmp"
web1:
image: tomcat
command: ["bin/catalina.sh","run"]
expose:
- "8080"
web2:
image: nginx
command: ["nginx","-g","daemon off;"]
expose:
- "80"
docker-compose 命令
docker-compose up
docker-compose up -d
docker-compose run web env
docker-compose up -d --scale web=5
docker-compose ps
docker-compose down
docker-compose stop
docker-compose --env-file ./config/.env.dev up
docker-compose --profile debug up
docker-compose --profile frontend --profile debug up
docker-compose config -f test.yaml docker-compose config -f test.yaml
使用docker-compose config
查看docker-compose.yml 中变量解析后的内容
docker-compose --env-file ./config/.env.dev config