1、Docker-compose简介
Docker-Compose项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排。 Docker-Compose将所管理的容器分为三层,分别是工程(project),服务(service)以及容器(container)。 工程:Docker-Compose运行目录下的所有文件(docker-compose.yml,extends文件或环境变量文件等)组成一个工程,若无特殊指定工程名即为当前目录名。 服务:一个工程当中可包含多个服务,每个服务中定义了容器运行的镜像,参数,依赖。 容器:一个服务当中可包括多个容器实例,Docker-Compose并没有解决负载均衡的问题,因此需要借助其它工具实现服务发现及负载均衡。 Docker-Compose的工程配置文件默认为docker-compose.yml,可通过环境变量COMPOSE_FILE或-f参数自定义配置文件,其定义了多个有依赖关系的服务及每个服务运行的容器。 使用一个Dockerfile模板文件,可以让用户很方便的定义一个单独的应用容器。在工作中,经常会碰到需要多个容器相互配合来完成某项任务的情况。例如要实现一个Web项目,除了Web服务容器本身,往往还需要再加上后端的数据库服务容器,甚至还包括负载均衡容器等。Compose允许用户通过一个单独的docker-compose.yml模板文件(YAML 格式)来定义一组相关联的应用容器为一个项目(project)。 Docker-Compose项目由Python编写,调用Docker服务提供的API来对容器进行管理。因此,只要所操作的平台支持Docker API,就可以在其上利用Compose来进行编排管理。
2、Docker-compose安装
有pip和下面安装方式。
安装
[root@mydockerhub ~]# wget https://github.com/docker/compose/releases/download/1.26.2/docker-compose-Linux-x86_64 [root@mydockerhub ~]# cp /root/docker-compose-Linux-x86_64 /usr/local/bin/docker-compose [root@mydockerhub ~]# chmod +x /usr/local/bin/docker-compose [root@mydockerhub ~]# docker-compose -version docker-compose version 1.26.2, build eefe0d31
示例
[root@mydockerhub ~]# cat docker-compose.yml version: '3.1' services: frontend: image: konradkleine/docker-registry-frontend:v2 ports: - 8080:80 volumes: - ./certs/frontend.crt:/etc/apache2/server.crt:ro - ./certs/frontend.key:/etc/apache2/server.key:ro environment: - ENV_DOCKER_REGISTRY_HOST=mydockerhub.com - ENV_DOCKER_REGISTRY_PORT=5000
运行
[root@mydockerhub ~]# docker-compose -f docker-compose.yml up -d
3、Docker-compose配置
说明:文件中涉及的宿主机挂载目录、挂载文件事先要准备好。
[root@mydockerhub ~]# mkdir lamp [root@mydockerhub ~]# cd lamp [root@mydockerhub ~]# vi docker-compose.yml version: '3' services: php: image: php7.2.10:centos7.8 hostname: php privileged: true restart: always container_name: php7.2.10 ports: - "9000:9000" volumes: - /lamp/conf/php:/usr/local/php/etc - /lamp/data/website:/opt/XYZ/website - /lamp/data/nfs:/opt/XYZ/nfs dns: - 114.114.114.114 - 8.8.8.8 networks: lamp-net: ipv4_address: 172.16.9.10 command: /usr/sbin/init apache1: image: apache2.4.25:centos7.8 hostname: apache1 privileged: true restart: always container_name: apache2.4.25_node1 ports: - "50080:50080" volumes: - /lamp/conf/apache:/etc/httpd - /lamp/data/website:/opt/XYZ/website - /lamp/data/nfs:/opt/XYZ/nfs depends_on: - php links: - php dns: - 114.114.114.114 - 8.8.8.8 networks: lamp-net: ipv4_address: 172.16.9.20 command: /usr/sbin/init apache2: image: apache2.4.25:centos7.8 hostname: apache2 privileged: true restart: always container_name: apache2.4.25_node2 ports: - "50088:50080" volumes: - /lamp/conf/apache:/etc/httpd - /lamp/data/website:/opt/XYZ/website - /lamp/data/nfs:/opt/XYZ/nfs depends_on: - php links: - php dns: - 114.114.114.114 - 8.8.8.8 networks: lamp-net: ipv4_address: 172.16.9.21 command: /usr/sbin/init nginx: image: nginx1.18.0:centos7.8 hostname: nginx privileged: true restart: always container_name: nginx1.18.0 ports: - "80:80" volumes: - /lamp/conf/nginx:/usr/local/nginx/conf - /lamp/logs/nginx:/usr/local/nginx/logs depends_on: - apache1 - apache2 - php links: - apache1 - apache2 dns: - 114.114.114.114 - 8.8.8.8 networks: lamp-net: ipv4_address: 172.16.9.30 command: /usr/sbin/init networks: lamp-net: driver: bridge ipam: config: - subnet: 172.16.9.0/24 检查配置: [root@mydockerhub lamp]# docker-compose config ERROR: The Compose file './docker-compose.yml' is invalid because: networks.lamp-net.ipam.config value Additional properties are not allowed ('gateway' was unexpected) 备注:3以上的版本就不需要设置gateway(网关),删除即可 启动 [root@mydockerhub lamp]# docker-compose up -d(停止和删除是docker-compose down) Creating network "lamp_lamp-net" with driver "bridge" Creating php7.2.10 ... done Creating apache2.4.25_node2 ... done Creating apache2.4.25_node1 ... done Creating nginx1.18.0 ... done [root@mydockerhub lamp]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eba53df185d5 nginx1.18.0:centos7.8 "/usr/sbin/init" 57 seconds ago Up 55 seconds 0.0.0.0:80->80/tcp nginx1.18.0 62f88748905b apache2.4.25:centos7.8 "/usr/sbin/init" 59 seconds ago Up 56 seconds 80/tcp, 0.0.0.0:50088->50080/tcp apache2.4.25_node2 920640c7929d apache2.4.25:centos7.8 "/usr/sbin/init" 59 seconds ago Up 56 seconds 80/tcp, 0.0.0.0:50080->50080/tcp apache2.4.25_node1 81fb160f85eb php7.2.10:centos7.8 "/usr/sbin/init" About a minute ago Up 58 seconds 0.0.0.0:9000->9000/tcp php7.2.10 [root@mydockerhub lamp]# docker-compose ps Name Command State Ports ------------------------------------------------------------------------------ apache2.4.25_node1 /usr/sbin/init Up 0.0.0.0:50080->50080/tcp, 80/tcp apache2.4.25_node2 /usr/sbin/init Up 0.0.0.0:50088->50080/tcp, 80/tcp nginx1.18.0 /usr/sbin/init Up 0.0.0.0:80->80/tcp php7.2.10 /usr/sbin/init Up 0.0.0.0:9000->9000/tcp [root@mydockerhub lamp]# docker network ls NETWORK ID NAME DRIVER SCOPE be6c8eb1bb36 lamp_lamp-net bridge local [root@mydockerhub lamp]# docker network inspect lamp_lamp-net 访问http://192.168.146.199测试,查看apache1和apache2容器是否都有访问日志。 备注:更改docker-compose.yml文件后重新启动,他会删除并重新创建容器