Background:

Now there is a need: some partners want to run a springcloud application locally. All online docker-deployed in kubernetes cluster. The application is deployed using statefulSet (he wants to register the hostname in nacOS). Then local IDEA development. The host name is the same. So he felt he couldn’t run multiple apps (there are 8 apps), and the end result was that everyone had a different app running on their computer to do……. Isn’t that anti-human? Windows this function runs kubernetes but I also feel trouble…… How to pick up docker compose’s usage again? I still remember the last time docker Compose was used in a gitLab test run in 2016. Docker Compose is basically out of the question from now on

Docker compose usage

Docker Dockercompose for Windows installation will skip…… Wsl2 is not installed on my Windows PC. Docker Compose is composed for Linux. Then the dye program went off to play. We started the recruitment information above write proficient docker virtualization what…… I can only hehe! Example Initialize one centos8

Docker Docker Compose installation

dnf install dnf-utils device-mapper-persistent-data lvm2 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum install docker-ce -y curl -L https://download.fastgit.org/docker/compose/releases/download/1.27.4/docker-compose- ` ` uname - s - ` uname -m ` > /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-composeCopy the code

Docker Compose docker Compose docker Compose docker Compose docker Compose docker Compose docker Compose Docker Compose’s source is a blog. Download the official next move, this is a domestic source.

Get familiar with Docker Compose

Docker-comemage. yaml was copied to a docker-comemage. yaml file

version: '3' services: laya-auth: image: harbor.xxx.com/layatools/openjdk:8-jdk container_name: laya-auth hostname: laya-auth volumes: - / data/laya - verse - service/auth/target/auth - 0.0.1 - the SNAPSHOT. Jar: / data/laya - verse - service/auth/target/auth - 0.0.1 - the SNAPSHOT. Ja R ports: - 8080:8080 command: - / bin/sh -- -- - | c Java jar/data/laya - verse - service/auth/target/auth - 0.0.1 - the SNAPSHOT. The jarCopy the code

Ran up…… Well, put them all on and try:

version: '3' services: laya-auth: image: harbor.xxxx.com/layatools/openjdk:8-jdk container_name: laya-auth hostname: laya-auth volumes: - / data/laya - verse - service/auth/target/auth - 0.0.1 - the SNAPSHOT. Jar: / data/laya - verse - service/auth/target/auth - 0.0.1 - the SNAPSHOT. Ja r ports: - 8080:8080 command: / bin/sh -- -- - | c Java jar/data/laya - verse - service/auth/target/auth - 0.0.1 - the SNAPSHOT. Jar laya - chat: image: harbor.xxxx.com/layatools/openjdk:8-jdk container_name: laya-chat hostname: laya-chat volumes: - / data/laya - verse - service/chat/target/chat - 0.0.1 - the SNAPSHOT. Jar: / data/laya - verse - service/chat/target/chat - 0.0.1 - the SNAPSHOT. Ja r ports: - 8081:8081 command: - / bin/sh -- -- - | c Java jar/data/laya - verse - service/chat/target/chat - 0.0.1 - the SNAPSHOT. Jar laya - datacontrol: image: harbor.xxxx.com/layatools/openjdk:8-jdk container_name: laya-datacontrol hostname: laya-datacontrol volumes: - / data/laya - verse - service/datacontrol/target/datacontrol - 0.0.1 - the SNAPSHOT. Jar: / data/laya - verse - service/datacontrol/target/d Atacontrotrol 0.0.1 -snapshot. jar ports: -8082:8082 Command: / bin/sh -- -- - | c Java jar/data/laya - verse - service/datacontrol/target/datacontrol - 0.0.1 - the SNAPSHOT. Jar laya - gateway: image: harbor.xxxx.com/layatools/openjdk:8-jdk container_name: laya-gateway hostname: laya-gateway volumes: - / data/laya - verse - service/gateway/target/gateway - 0.0.1 - the SNAPSHOT. Jar: / data/laya - verse - service/gateway/target/gateway - 0.0.1 -SNAPSHOT.jar ports: - 8083:8083 command: / bin/sh -- -- - | c Java jar/data/laya - verse - service/gateway/target/gateway - 0.0.1 - the SNAPSHOT. Jar laya - monitor: image: harbor.xxxx.com/layatools/openjdk:8-jdk container_name: laya-monitor hostname: laya-monitor volumes: - / data/laya - verse - service/monitor/target/monitor - 0.0.1 - the SNAPSHOT. Jar: / data/laya - verse - service/monitor/target/monitor - 0.0.1 -SNAPSHOT.jar ports: - 8084:8084 command: / bin/sh -- -- - | c Java jar/data/laya - verse - service/monitor/target/monitor - 0.0.1 - the SNAPSHOT. Jar laya - node: image: harbor.xxxx.com/layatools/openjdk:8-jdk container_name: laya-node hostname: laya-node volumes: - / data/laya - verse - service/node/target/node - 0.0.1 - the SNAPSHOT. Jar: / data/laya - verse - service/node/target/node - 0.0.1 - the SNAPSHOT. Ja r ports: - 8085:8085 command: / bin/sh -- -- - | c Java jar/data/laya - verse - service/node/target/node - 0.0.1 - the SNAPSHOT. Jar laya - nodemanage: image: harbor.xxxx.com/layatools/openjdk:8-jdk container_name: laya-nodemanage hostname: laya-nodemanage volumes: - / data/laya - verse - service/nodemanage/target/nodemanage - 0.0.1 - the SNAPSHOT. Jar: / data/laya - verse - service/nodemanage/target/node Manage -0.0.1 -snapshot. jar ports: -8086:8086 Command: manage-0.0.1 -snapshot. jar ports: -8086:8086 Command: / bin/sh -- -- - | c Java jar/data/laya - verse - service/nodemanage/target/nodemanage - 0.0.1 - the SNAPSHOT. Jar laya - social: image: harbor.xxxx.com/layatools/openjdk:8-jdk container_name: laya-social hostname: laya-social volumes: - / data/laya - verse - service/social/target/social - 0.0.1 - the SNAPSHOT. Jar: / data/laya - verse - service/social/target/social - 0.0.1 - SNA PSHOT.jar ports: - 8087:8087 command: / bin/sh -- -- - | c Java jar/data/laya - verse - service/social/target/social - 0.0.1 - the SNAPSHOT. The jarCopy the code

docker ps
Copy the code

Enter a pod randomly to look at the hostname:

docker exec -it 99c950ebb787 sh 
/ # hostname 
laya-nodemanage
/ # 

Copy the code

Well, it’s kind of fulfilling my personal needs

Docker compose compose Docker compose compose

  • Ps: Lists all running containers
docker-compose ps
Copy the code

  • Logs: View service log output
docker-compose logs
Copy the code

There are too many applications. It’s too troublesome to look at the log. Help me

docker-compose logs -f laya-auth
Copy the code

  • Port: displays the bound public port. The following command displays the bound public port of laya-Auth service port 8080
docker-compose port laya-auth 8080
Copy the code

  • Build: Builds or rebuilds the service
docker-compose build
Copy the code

  • Stop: stops a container for running services
docker-compose stop laya-node
Copy the code

  • Start: starts an existing container for the specified service
docker-compose start laya-node
Copy the code

  • Rm: Delete the container of the specified service
docker-compose stop laya-gateway
docker-compose rm laya-gateway
Copy the code

Note: first stop rm delete oh

  • Up: Builds and starts the container
docker-compose up
docker-compose up laya-gateway
Copy the code

Note: A single service can follow a single service name

  • Kill: Stops a container for a specified service by sending the SIGKILL signal
docker-compose kill laya-gateway
Copy the code

  • Pull: Downloads the service image
  • Scale: Sets the number of service luck containers specified in the form service=num
docker-compose scale xxx=2
Copy the code

Note: I’m not going to demonstrate that I specified the container name scale

  • Run: Executes a command on a service
docker-compose run laya-auth sh
Copy the code

Detailed parameters

From: www.jianshu.com/p/90bf0e231…

Compose和Docker兼容性:
    Compose 文件格式有3个版本,分别为1, 2.x 和 3.x
   
目前主流的为 3.x 其支持 docker 1.13.0 及其以上的版本

常用参数:
    version           # 指定 compose 文件的版本
    services          # 定义所有的 service 信息, services 下面的第一级别的 key 既是一个 service 的名称
        build                 # 指定包含构建上下文的路径, 或作为一个对象,该对象具有 context 和指定的 dockerfile 文件以及 args 参数值
            context               # context: 指定 Dockerfile 文件所在的路径
            dockerfile            # dockerfile: 指定 context 指定的目录下面的 Dockerfile 的名称(默认为 Dockerfile)
            args                  # args: Dockerfile 在 build 过程中需要的参数 (等同于 docker container build --build-arg 的作用)
            cache_from            # v3.2中新增的参数, 指定缓存的镜像列表 (等同于 docker container build --cache_from 的作用)
            labels                # v3.3中新增的参数, 设置镜像的元数据 (等同于 docker container build --labels 的作用)
            shm_size              # v3.5中新增的参数, 设置容器 /dev/shm 分区的大小 (等同于 docker container build --shm-size 的作用)
        command               # 覆盖容器启动后默认执行的命令, 支持 shell 格式和 [] 格式
        configs               # 不知道怎么用
        cgroup_parent         # 不知道怎么用
        container_name        # 指定容器的名称 (等同于 docker run --name 的作用)
        hostname              #特意找来的指定pod的hostname
        credential_spec       # 不知道怎么用
        deploy                # v3 版本以上, 指定与部署和运行服务相关的配置, deploy 部分是 docker stack 使用的, docker stack 依赖 docker swarm
            endpoint_mode         # v3.3 版本中新增的功能, 指定服务暴露的方式
                vip                   # Docker 为该服务分配了一个虚拟 IP(VIP), 作为客户端的访问服务的地址
                dnsrr                 # DNS轮询, Docker 为该服务设置 DNS 条目, 使得服务名称的 DNS 查询返回一个 IP 地址列表, 客户端直接访问其中的一个地址
            labels                # 指定服务的标签,这些标签仅在服务上设置
            mode                  # 指定 deploy 的模式
                global                # 每个集群节点都只有一个容器
                replicated            # 用户可以指定集群中容器的数量(默认)
            placement             # 不知道怎么用
            replicas              # deploy 的 mode 为 replicated 时, 指定容器副本的数量
            resources             # 资源限制
                limits                # 设置容器的资源限制
                    cpus: "0.5"           # 设置该容器最多只能使用 50% 的 CPU 
                    memory: 50M           # 设置该容器最多只能使用 50M 的内存空间 
                reservations          # 设置为容器预留的系统资源(随时可用)
                    cpus: "0.2"           # 为该容器保留 20% 的 CPU
                    memory: 20M           # 为该容器保留 20M 的内存空间
            restart_policy        # 定义容器重启策略, 用于代替 restart 参数
                condition             # 定义容器重启策略(接受三个参数)
                    none                  # 不尝试重启
                    on-failure            # 只有当容器内部应用程序出现问题才会重启
                    any                   # 无论如何都会尝试重启(默认)
                delay                 # 尝试重启的间隔时间(默认为 0s)
                max_attempts          # 尝试重启次数(默认一直尝试重启)
                window                # 检查重启是否成功之前的等待时间(即如果容器启动了, 隔多少秒之后去检测容器是否正常, 默认 0s)
            update_config         # 用于配置滚动更新配置
                parallelism           # 一次性更新的容器数量
                delay                 # 更新一组容器之间的间隔时间
                failure_action        # 定义更新失败的策略
                    continue              # 继续更新
                    rollback              # 回滚更新
                    pause                 # 暂停更新(默认)
                monitor               # 每次更新后的持续时间以监视更新是否失败(单位: ns|us|ms|s|m|h) (默认为0)
                max_failure_ratio     # 回滚期间容忍的失败率(默认值为0)
                order                 # v3.4 版本中新增的参数, 回滚期间的操作顺序
                    stop-first            #旧任务在启动新任务之前停止(默认)
                    start-first           #首先启动新任务, 并且正在运行的任务暂时重叠
            rollback_config       # v3.7 版本中新增的参数, 用于定义在 update_config 更新失败的回滚策略
                parallelism           # 一次回滚的容器数, 如果设置为0, 则所有容器同时回滚
                delay                 # 每个组回滚之间的时间间隔(默认为0)
                failure_action        # 定义回滚失败的策略
                    continue              # 继续回滚
                    pause                 # 暂停回滚
                monitor               # 每次回滚任务后的持续时间以监视失败(单位: ns|us|ms|s|m|h) (默认为0)
                max_failure_ratio     # 回滚期间容忍的失败率(默认值0)
                order                 # 回滚期间的操作顺序
                    stop-first            # 旧任务在启动新任务之前停止(默认)
                    start-first           # 首先启动新任务, 并且正在运行的任务暂时重叠
            注意:
                支持 docker-compose up 和 docker-compose run 但不支持 docker stack deploy 的子选项
                security_opt  container_name  devices  tmpfs  stop_signal  links    cgroup_parent
                network_mode  external_links  restart  build  userns_mode  sysctls
        devices               # 指定设备映射列表 (等同于 docker run --device 的作用)
        depends_on            # 定义容器启动顺序 (此选项解决了容器之间的依赖关系, 此选项在 v3 版本中 使用 swarm 部署时将忽略该选项)
            示例:
                docker-compose up 以依赖顺序启动服务,下面例子中 redis 和 db 服务在 web 启动前启动
                默认情况下使用 docker-compose up web 这样的方式启动 web 服务时,也会启动 redis 和 db 两个服务,因为在配置文件中定义了依赖关系
                version: '3'
                services:
                    web:
                        build: .
                        depends_on:
                            - db      
                            - redis  
                    redis:
                        image: redis
                    db:
                        image: postgres                             
        dns                   # 设置 DNS 地址(等同于 docker run --dns 的作用)
        dns_search            # 设置 DNS 搜索域(等同于 docker run --dns-search 的作用)
        tmpfs                 # v2 版本以上, 挂载目录到容器中, 作为容器的临时文件系统(等同于 docker run --tmpfs 的作用, 在使用 swarm 部署时将忽略该选项)
        entrypoint            # 覆盖容器的默认 entrypoint 指令 (等同于 docker run --entrypoint 的作用)
        env_file              # 从指定文件中读取变量设置为容器中的环境变量, 可以是单个值或者一个文件列表, 如果多个文件中的变量重名则后面的变量覆盖前面的变量, environment 的值覆盖 env_file 的值
            文件格式:
                RACK_ENV=development 
        environment           # 设置环境变量, environment 的值可以覆盖 env_file 的值 (等同于 docker run --env 的作用)
        expose                # 暴露端口, 但是不能和宿主机建立映射关系, 类似于 Dockerfile 的 EXPOSE 指令
        external_links        # 连接不在 docker-compose.yml 中定义的容器或者不在 compose 管理的容器(docker run 启动的容器, 在 v3 版本中使用 swarm 部署时将忽略该选项)
        extra_hosts           # 添加 host 记录到容器中的 /etc/hosts 中 (等同于 docker run --add-host 的作用)
        healthcheck           # v2.1 以上版本, 定义容器健康状态检查, 类似于 Dockerfile 的 HEALTHCHECK 指令
            test                  # 检查容器检查状态的命令, 该选项必须是一个字符串或者列表, 第一项必须是 NONE, CMD 或 CMD-SHELL, 如果其是一个字符串则相当于 CMD-SHELL 加该字符串
                NONE                  # 禁用容器的健康状态检测
                CMD                   # test: ["CMD", "curl", "-f", "http://localhost"]
                CMD-SHELL             # test: ["CMD-SHELL", "curl -f http://localhost || exit 1"] 或者 test: curl -f https://localhost || exit 1
            interval: 1m30s       # 每次检查之间的间隔时间
            timeout: 10s          # 运行命令的超时时间
            retries: 3            # 重试次数
            start_period: 40s     # v3.4 以上新增的选项, 定义容器启动时间间隔
            disable: true         # true 或 false, 表示是否禁用健康状态检测和 test: NONE 相同
        image                 # 指定 docker 镜像, 可以是远程仓库镜像、本地镜像
        init                  # v3.7 中新增的参数, true 或 false 表示是否在容器中运行一个 init, 它接收信号并传递给进程
        isolation             # 隔离容器技术, 在 Linux 中仅支持 default 值
        labels                # 使用 Docker 标签将元数据添加到容器, 与 Dockerfile 中的 LABELS 类似
        links                 # 链接到其它服务中的容器, 该选项是 docker 历史遗留的选项, 目前已被用户自定义网络名称空间取代, 最终有可能被废弃 (在使用 swarm 部署时将忽略该选项)
        logging               # 设置容器日志服务
            driver                # 指定日志记录驱动程序, 默认 json-file (等同于 docker run --log-driver 的作用)
            options               # 指定日志的相关参数 (等同于 docker run --log-opt 的作用)
                max-size              # 设置单个日志文件的大小, 当到达这个值后会进行日志滚动操作
                max-file              # 日志文件保留的数量
        network_mode          # 指定网络模式 (等同于 docker run --net 的作用, 在使用 swarm 部署时将忽略该选项)         
        networks              # 将容器加入指定网络 (等同于 docker network connect 的作用), networks 可以位于 compose 文件顶级键和 services 键的二级键
            aliases               # 同一网络上的容器可以使用服务名称或别名连接到其中一个服务的容器
            ipv4_address      # IP V4 格式
            ipv6_address      # IP V6 格式
            示例:
                version: '3.7'
                services: 
                    test: 
                        image: nginx:1.14-alpine
                        container_name: mynginx
                        command: ifconfig
                        networks: 
                            app_net:                                # 调用下面 networks 定义的 app_net 网络
                            ipv4_address: 172.16.238.10
                networks:
                    app_net:
                        driver: bridge
                        ipam:
                            driver: default
                            config:
                                - subnet: 172.16.238.0/24
        pid: 'host'           # 共享宿主机的 进程空间(PID)
        ports                 # 建立宿主机和容器之间的端口映射关系, ports 支持两种语法格式
            SHORT 语法格式示例:
                - "3000"                            # 暴露容器的 3000 端口, 宿主机的端口由 docker 随机映射一个没有被占用的端口
                - "3000-3005"                       # 暴露容器的 3000 到 3005 端口, 宿主机的端口由 docker 随机映射没有被占用的端口
                - "8000:8000"                       # 容器的 8000 端口和宿主机的 8000 端口建立映射关系
                - "9090-9091:8080-8081"
                - "127.0.0.1:8001:8001"             # 指定映射宿主机的指定地址的
                - "127.0.0.1:5000-5010:5000-5010"   
                - "6060:6060/udp"                   # 指定协议
            LONG 语法格式示例:(v3.2 新增的语法格式)
                ports:
                    - target: 80                    # 容器端口
                      published: 8080               # 宿主机端口
                      protocol: tcp                 # 协议类型
                      mode: host                    # host 在每个节点上发布主机端口,  ingress 对于群模式端口进行负载均衡
        secrets               # 不知道怎么用
        security_opt          # 为每个容器覆盖默认的标签 (在使用 swarm 部署时将忽略该选项)
        stop_grace_period     # 指定在发送了 SIGTERM 信号之后, 容器等待多少秒之后退出(默认 10s)
        stop_signal           # 指定停止容器发送的信号 (默认为 SIGTERM 相当于 kill PID; SIGKILL 相当于 kill -9 PID; 在使用 swarm 部署时将忽略该选项)
        sysctls               # 设置容器中的内核参数 (在使用 swarm 部署时将忽略该选项)
        ulimits               # 设置容器的 limit
        userns_mode           # 如果Docker守护程序配置了用户名称空间, 则禁用此服务的用户名称空间 (在使用 swarm 部署时将忽略该选项)
        volumes               # 定义容器和宿主机的卷映射关系, 其和 networks 一样可以位于 services 键的二级键和 compose 顶级键, 如果需要跨服务间使用则在顶级键定义, 在 services 中引用
            SHORT 语法格式示例:
                volumes:
                    - /var/lib/mysql                # 映射容器内的 /var/lib/mysql 到宿主机的一个随机目录中
                    - /opt/data:/var/lib/mysql      # 映射容器内的 /var/lib/mysql 到宿主机的 /opt/data
                    - ./cache:/tmp/cache            # 映射容器内的 /var/lib/mysql 到宿主机 compose 文件所在的位置
                    - ~/configs:/etc/configs/:ro    # 映射容器宿主机的目录到容器中去, 权限只读
                    - datavolume:/var/lib/mysql     # datavolume 为 volumes 顶级键定义的目录, 在此处直接调用
            LONG 语法格式示例:(v3.2 新增的语法格式)
                version: "3.2"
                services:
                    web:
                        image: nginx:alpine
                        ports:
                            - "80:80"
                        volumes:
                            - type: volume                  # mount 的类型, 必须是 bind、volume 或 tmpfs
                                source: mydata              # 宿主机目录
                                target: /data               # 容器目录
                                volume:                     # 配置额外的选项, 其 key 必须和 type 的值相同
                                    nocopy: true                # volume 额外的选项, 在创建卷时禁用从容器复制数据
                            - type: bind                    # volume 模式只指定容器路径即可, 宿主机路径随机生成; bind 需要指定容器和数据机的映射路径
                                source: ./static
                                target: /opt/app/static
                                read_only: true             # 设置文件系统为只读文件系统
                volumes:
                    mydata:                                 # 定义在 volume, 可在所有服务中调用
        restart               # 定义容器重启策略(在使用 swarm 部署时将忽略该选项, 在 swarm 使用 restart_policy 代替 restart)
            no                    # 禁止自动重启容器(默认)
            always                # 无论如何容器都会重启
            on-failure            # 当出现 on-failure 报错时, 容器重新启动
        其他选项:
            domainname, hostname, ipc, mac_address, privileged, read_only, shm_size, stdin_open, tty, user, working_dir
            上面这些选项都只接受单个值和 docker run 的对应参数类似
        对于值为时间的可接受的值:
            2.5s
            10s
            1m30s
            2h32m
            5h34m56s
            时间单位: us, ms, s, m, h
        对于值为大小的可接受的值:
            2b
            1024kb
            2048k
            300m
            1gb
            单位: b, k, m, g 或者 kb, mb, gb
    networks          # 定义 networks 信息
        driver                # 指定网络模式, 大多数情况下, 它 bridge 于单个主机和 overlay Swarm 上
            bridge                # Docker 默认使用 bridge 连接单个主机上的网络
            overlay               # overlay 驱动程序创建一个跨多个节点命名的网络
            host                  # 共享主机网络名称空间(等同于 docker run --net=host)
            none                  # 等同于 docker run --net=none
        driver_opts           # v3.2以上版本, 传递给驱动程序的参数, 这些参数取决于驱动程序
        attachable            # driver 为 overlay 时使用, 如果设置为 true 则除了服务之外,独立容器也可以附加到该网络; 如果独立容器连接到该网络,则它可以与其他 Docker 守护进程连接到的该网络的服务和独立容器进行通信
        ipam                  # 自定义 IPAM 配置. 这是一个具有多个属性的对象, 每个属性都是可选的
            driver                # IPAM 驱动程序, bridge 或者 default
            config                # 配置项
                subnet                # CIDR格式的子网,表示该网络的网段
        external              # 外部网络, 如果设置为 true 则 docker-compose up 不会尝试创建它, 如果它不存在则引发错误
        name                  # v3.5 以上版本, 为此网络设置名称


文件格式示例:
    version: "3"
    services:
      redis:
        image: redis:alpine
        ports:
          - "6379"
        networks:
          - frontend
        deploy:
          replicas: 2
          update_config:
            parallelism: 2
            delay: 10s
          restart_policy:
            condition: on-failure
      db:
        image: postgres:9.4
        volumes:
          - db-data:/var/lib/postgresql/data
        networks:
          - backend
        deploy:
          placement:
            constraints: [node.role == manager]
Copy the code

Also did not use what complex network link personal application used the next hostname.

Universal help:

docker-compose --help
Copy the code

Others for reference:

www.jianshu.com/p/658911a8c… www.runoob.com/docker/dock… www.cnblogs.com/minseo/p/11…