自学内容网 自学内容网

6. docker-compose

一、docker-compose

单机版的容器编排工具
作用: 批量管理容器的

1、安装docker-compose

[root@laobideng ~]# mv docker-compose /usr/local/bin/
[root@laobideng ~]# chmod a+x /usr/local/bin/docker-compose 
[root@laobideng ~]# 
[root@laobideng ~]# docker-compose version
docker-compose version 1.29.0, build 07737305
docker-py version: 5.0.0
CPython version: 3.7.10
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

2、docker-compose使用流程

2.1 编写compose文件

[root@laobideng compose]# cat docker-compose.yaml 
version: '3'
services:
   test_nginx:
       image: nginx:1.18
   test_redis:
       image: redis

2.2 创建

[root@laobideng compose]# docker-compose up -d 

2.3 删除

[root@laobideng compose]# docker-compose down

二、docker-compose常用指令

https://docs.docker.com/compose/compose-file/07-volumes/

1、整体结构

version: '3'
services:
服务名称/容器名称:
容器选项
networks:
volumes:
  • version
    指定docker-compose和docker对接的API版本

  • services
    要创建的服务信息,容器

  • networks
    指定要创建的网络信息,自定义网络

  • volumes
    指定要创建的数据卷信息, 持久化存储

2、常用选项

  • 指定镜像名称
image: 镜像名称 
  • 持久化存储
volumes:
    - 物理目录:容器目录
    - 物理目录:容器目录
  • 指定容器自动运行的命令
version: '3'
services:
   test:
     image: centos:7
     command:
       - "sleep"
       - "3600"
  • 定义容器通信别名
links:
    - 容器名称:别名
  • 发布容器服务
ports:
    - 物理机端口:容器端口
  • 说明容器中的服务端口
expose:
    - 端口
  • 定义环境变量
environment:
    key: value
    key: value
  • 定义容器依赖关系
depends_on:
    - 容器名称
    - 容器名称
  • 指定网络模式
network_mode: "host"
  • 定义容器的副本数
deploy: 
replicas: 3
  • 资源限制
resources:
    limits:// 硬限制 
cpus: 2
memory: 50M
reservations:// 软限制
cpus: 1
memory: 20M
  • 健康状态检查
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 10s
timeout: 10s
retries: 3
start_period: 40s

三、docker-compose示例

[root@martin-host tomcatCompose]# ls
docker-compose.yaml  nginx.conf  project.war

[root@martin-host tomcatCompose]# cat docker-compose.yaml 
version: '3'
services:
    tomcat_project_01:
        image: tomcat
        volumes:
           - /opt/tomcatCompose/project.war:/usr/local/tomcat/webapps/project.war
        expose:
           - 8080
        deploy:
          resources:
              limits:
                   cpus: 2
                   memory: 2G
              reservations:
                   cpus: 1
                   memory: 1G
        healthcheck:
            test: ["CMD", "curl", "-f", "http://localhost:8080"]
            interval: 10s
            timeout: 10s
            retries: 3
            start_period: 30s
    tomcat_project_02:
        image: tomcat
        volumes:
           - /opt/tomcatCompose/project.war:/usr/local/tomcat/webapps/project.war
        expose:
           - 8080
        deploy:
          resources:
              limits:
                   cpus: 2
                   memory: 2G
              reservations:
                   cpus: 1
                   memory: 1G
        healthcheck:
            test: ["CMD", "curl", "-f", "http://localhost:8080"]
            interval: 10s
            timeout: 10s
            retries: 3
            start_period: 30s
    tomcat_nginx:
         image: nginx:1.18
         expose:
            - 80
         ports:
            - 80:80
         links:
            - tomcat_project_01:tomcat_project_01
            - tomcat_project_02:tomcat_project_02
         volumes:
            - /opt/tomcatCompose/nginx.conf:/etc/nginx/conf.d/default.conf
         depends_on:
            - tomcat_project_01
            - tomcat_project_02
         deploy:
           resources:
              limits:
                   cpus: 2
                   memory: 2G
              reservations:
                   cpus: 1
                   memory: 1G


[root@martin-host tomcatCompose]# cat nginx.conf 
upstream tomcat {
   server tomcat_project_01:8080;
   server tomcat_project_02:8080;
}

server {
   listen 80;
   server_name localhost;

   location / {
       proxy_pass http://tomcat/project/;
   }
}


原文地址:https://blog.csdn.net/u010198709/article/details/140626267

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!