美文网首页
6. 单机编排之Docker Compose

6. 单机编排之Docker Compose

作者: 随便写写咯 | 来源:发表于2021-02-09 05:57 被阅读0次

    1 Docker Compose

    Docker Compose是一个单机的容器编排工具, 可以用来解决不同容器内应用之间的依赖关系
    并且可以代替docker命令对容器进行创建, 启动和停止等手工操作
    
    docker-compose项目是Docker官方的开源项目, 负责实现对docker容器集群的快速编排
    其将管理的容器分为三层, 分别是工程(project), 服务(service),以及容器(container)
    
    docker-compose只适用于单机的容器编排, 对于容器集群, 还需要使用k8s去实现
    

    2 安装和准备

    2.1 安装

    docker compose多种安装方式
    1. pip安装, 因为docker-compose本身就是Python开发的
    2. 通过github下载对应版本, 安装
    3. 利用deb或yum安装, 不过版本比较老旧
    

    github安装docker-compose

    官方在github提供docker-compose资源, 而其本身就是一个二进制程序, 因此, 直接下载, 保存到PATH变量目录, 修改权限即可使用
    
    v1.26.2
    
    root@docker-compose:~# ll -t /usr/sbin
    total 24320
    -rwxr-xr-x  1 root root 12254160 Feb  9 04:34 docker-compose* # 需要添加x权限, 本身是没有执行权限的
    

    2.2 命令使用

    常用选项:
    -f File # 指定docker-compose使用的文件, 默认会使用同目录下的docker-compose.yml文件
    -p NAME # 指定项目名称, 默认将使用当前所在目录名称作为项目名, 因此, 执行docker-compose命令最好进入到对应的项目目录下
    常用命令:
    build 构建镜像
    bundle 从当前docker-compose文件生成一个以<当前目录>为名称的json格式的Docker Bundle备份文件
    config -q 查看当前配置, 没有错误不输出任何信息
    create 创建服务, 较少使用
    down 停止和删除所有容器, 网络, 镜像和数据卷
    events 从容器接收实时事件可以指定json日志格式, 较少使用
    exec 进入指定容器进行操作
    help 显示帮助信息
    images 显示镜像信息, 较少使用
    kill 强制终止运行中的容器
    logs 查看容器的日志
    pause 暂停服务
    port 查看端口
    ps 列出容器, 较少使用
    pull 重新拉取镜像, 镜像发生变化后, 需要重新拉取镜像, 较少使用
    push 上传镜像
    restart 重启服务, 较少使用
    rm 删除已经停止的服务
    run 一次性运行容器
    scale 设置指定服务运行的容器个数
    start 启动服务, 较少使用
    stop 停止服务, 较少使用
    top 显示容器运行状态
    unpause 取消暂停
    up 创建并启动容器, 较少使用
    

    3 docker-compose 文件格式

    docker-compose使用yaml文件, 格式要求严格的行首缩进
    
    默认docker-compose命令会调用当前目录下的docker-compose.yml的文件, 因此一般执行docker-compose命令前, 先进入docker-compose.yml文件所在目录
    

    4 从docker compose启动容器

    4.1 创建docker compose文件

    docker compose文件可在任意目录, 创建文件名为docker-compose.yml配置文件, 要注意前后的缩进
    
    root@docker-compose:~# mkdir /data/docker-compose
    root@docker-compose:~# cd /data/docker-compose
    root@docker-compose:/data/docker-compose# vim docker-compose.yml
    server-nginx-web:                                                                                                                           
        image: nginx
        container_name: nginx-web
        expose:
          - 80
          - 443
        ports:
          - "80:80"
          - "443:443"
    

    4.2 检查配置和格式

    root@docker-compose:/data/docker-compose# docker-compose config
    services:
      server-nginx-web:
        container_name: nginx-web
        expose:
        - 80
        - 443
        image: nginx
        network_mode: bridge
        ports:
        - 80:80/tcp
        - 443:443/tcp
    version: '2.1'
    
    root@docker-compose:/data/docker-compose# docker-compose config -q
    

    4.3 启动容器

    必须在docker-compose文件所在的目录执行

    前台启动
    docker-compose up
    Status: Downloaded newer image for nginx:latest
    Creating nginx-web ... done
    Attaching to nginx-web
    nginx-web           | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
    nginx-web           | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
    nginx-web           | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
    nginx-web           | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
    nginx-web           | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
    nginx-web           | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
    nginx-web           | /docker-entrypoint.sh: Configuration complete; ready for start up
    
    root@docker-compose:~# docker ps 
    CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                                      NAMES
    ba4372e946d5        nginx               "/docker-entrypoint.…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   nginx-web
    
    图片.png
    换到另一个终端, 进入容器
    root@docker-compose:/data/docker-compose# docker-compose exec server-nginx-web bash
    

    4.4 退出前台运行的容器

    ctrl + c 
    
    Stopping nginx-web ... done
    root@docker-compose:/data/docker-compose# docker-compose ps
      Name                 Command               State    Ports
    -----------------------------------------------------------
    nginx-web   /docker-entrypoint.sh ngin ...   Exit 0 
    

    4.5 启动退出的容器

    root@docker-compose:/data/docker-compose# docker-compose start 
    Starting server-nginx-web ... done
    
    docker-compose up 前台启动
    docker-composeu up -d  后台启动
    

    4.6 退出容器

    root@docker-compose:/data/docker-compose# docker-compose kill
    Killing nginx-web ... done
    root@docker-compose:/data/docker-compose# docker-compose ps
      Name                 Command                State     Ports
    -------------------------------------------------------------
    nginx-web   /docker-entrypoint.sh ngin ...   Exit 137 
    

    4.7 删除容器

    4.7.1 只删除停止的容器

    root@docker-compose:/data/docker-compose# docker-compose rm
    Going to remove nginx-web
    Are you sure? [yN] y
    Removing nginx-web ... done
    

    4.7.2 直接删除正在工作的容器

    后台启动
    root@docker-compose:/data/docker-compose# docker-compose up -d
    Creating nginx-web ... done
    root@docker-compose:/data/docker-compose# docker-compose ps
      Name                 Command               State                    Ports                  
    ---------------------------------------------------------------------------------------------
    nginx-web   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
    
    停止并删除容器
    root@docker-compose:/data/docker-compose# docker-compose down
    Stopping nginx-web ... done
    Removing nginx-web ... done
    root@docker-compose:/data/docker-compose# docker-compose ps
    Name   Command   State   Ports
    ------------------------------
    
    镜像也会被删除
    root@docker-compose:/data/docker-compose# docker-compose images
    Container   Repository   Tag   Image Id   Size
    ----------------------------------------------
    
    基础镜像还在, 所以在启动,也无需重新拉镜像, 但是是重新创建docker-compose
    相当于利用拉取的镜像, 在创建为docker-compose镜像
    [root@Ubuntu-1804-2:/apps/harbor]# docker images
    REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
    10.0.0.29/alpine-test/alpine    v1.0                e50c909a8df2        11 days ago         5.61MB
    alpine                          latest              e50c909a8df2        11 days ago         5.61MB
    goharbor/chartmuseum-photon     v0.8.1-v1.7.6       ca4e65cc8cbf        17 months ago       114MB
    goharbor/harbor-migrator        v1.7.6              bd65976b2563        17 months ago       680MB
    goharbor/redis-photon           v1.7.6              477066fd0e02        17 months ago       109MB
    goharbor/clair-photon           v2.0.8-v1.7.6       a65550304aa5        17 months ago       165MB
    goharbor/notary-server-photon   v0.6.1-v1.7.6       1bfca6aac750        17 months ago       136MB
    goharbor/notary-signer-photon   v0.6.1-v1.7.6       8535add7bfa5        17 months ago       133MB
    goharbor/harbor-registryctl     v1.7.6              bb06dcda87fa        17 months ago       103MB
    goharbor/registry-photon        v2.6.2-v1.7.6       8fa930eedbea        17 months ago       87.7MB
    goharbor/nginx-photon           v1.7.6              fea7c162d250        17 months ago       37MB
    goharbor/harbor-log             v1.7.6              f9b50bc6e136        17 months ago       82.6MB
    goharbor/harbor-jobservice      v1.7.6              cfac2ab2d45a        17 months ago       85.1MB
    goharbor/harbor-core            v1.7.6              37379145c410        17 months ago       96.6MB
    goharbor/harbor-portal          v1.7.6              eafab006217d        17 months ago       41.7MB
    goharbor/harbor-adminserver     v1.7.6              2d91210e25ed        17 months ago       73.3MB
    goharbor/harbor-db              v1.7.6              f28a4ae69c04        17 months ago       146MB
    

    4.8 后台执行

    root@docker-compose:/data/docker-compose# docker-compose up -d
    Creating nginx-web ... done
    root@docker-compose:/data/docker-compose# docker-compose images
    Container   Repository    Tag       Image Id      Size 
    -------------------------------------------------------
    nginx-web   nginx        latest   f6d0b4767a6c   133 MB
    

    4.9 停止, 启动, 重启

    root@docker-compose:/data/docker-compose# docker-compose stop
    Stopping nginx-web ... done
    root@docker-compose:/data/docker-compose# docker-compose start
    Starting server-nginx-web ... done
    root@docker-compose:/data/docker-compose# docker-compose ps
      Name                 Command               State                    Ports                  
    ---------------------------------------------------------------------------------------------
    nginx-web   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
    root@docker-compose:/data/docker-compose# docker-compose restart
    Restarting nginx-web ... done
    root@docker-compose:/data/docker-compose# docker-compose ps
      Name                 Command               State                    Ports                  
    ---------------------------------------------------------------------------------------------
    nginx-web   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
    

    4.10 日志查看

    json格式查看
    
    root@docker-compose:/data/docker-compose# docker-compose restart
    Restarting nginx-web ... done
    
    root@docker-compose:/data/docker-compose# docker-compose events --json
    
    
    
    {"time": "2021-02-09T05:35:15.211387", "type": "container", "action": "kill", "id": "aa33b6a73eb3687896c5b1a0a9f55273a2e9fdd052b9cc8639ec22b5d4529a40", "service": "server-nginx-web", "attributes": {"name": "nginx-web", "image": "nginx"}}
    {"time": "2021-02-09T05:35:15.282536", "type": "container", "action": "die", "id": "aa33b6a73eb3687896c5b1a0a9f55273a2e9fdd052b9cc8639ec22b5d4529a40", "service": "server-nginx-web", "attributes": {"name": "nginx-web", "image": "nginx"}}
    {"time": "2021-02-09T05:35:15.405425", "type": "container", "action": "stop", "id": "aa33b6a73eb3687896c5b1a0a9f55273a2e9fdd052b9cc8639ec22b5d4529a40", "service": "server-nginx-web", "attributes": {"name": "nginx-web", "image": "nginx"}}
    {"time": "2021-02-09T05:35:16.433548", "type": "container", "action": "start", "id": "aa33b6a73eb3687896c5b1a0a9f55273a2e9fdd052b9cc8639ec22b5d4529a40", "service": "server-nginx-web", "attributes": {"name": "nginx-web", "image": "nginx"}}
    {"time": "2021-02-09T05:35:16.433600", "type": "container", "action": "restart", "id": "aa33b6a73eb3687896c5b1a0a9f55273a2e9fdd052b9cc8639ec22b5d4529a40", "service": "server-nginx-web", "attributes": {"name": "nginx-web", "image": "nginx"}}
    
    默认格式查看
    
    root@docker-compose:/data/docker-compose# docker-compose events
    2021-02-09 05:36:07.859265 container kill aa33b6a73eb3687896c5b1a0a9f55273a2e9fdd052b9cc8639ec22b5d4529a40 (image=nginx, name=nginx-web)
    2021-02-09 05:36:07.961174 container die aa33b6a73eb3687896c5b1a0a9f55273a2e9fdd052b9cc8639ec22b5d4529a40 (image=nginx, name=nginx-web)
    2021-02-09 05:36:08.132842 container stop aa33b6a73eb3687896c5b1a0a9f55273a2e9fdd052b9cc8639ec22b5d4529a40 (image=nginx, name=nginx-web)
    2021-02-09 05:36:08.893008 container start aa33b6a73eb3687896c5b1a0a9f55273a2e9fdd052b9cc8639ec22b5d4529a40 (image=nginx, name=nginx-web)
    2021-02-09 05:36:08.893067 container restart aa33b6a73eb3687896c5b1a0a9f55273a2e9fdd052b9cc8639ec22b5d4529a40 (image=nginx, name=nginx-web)
    

    4.11 指定同时启动的容器的数量

    通过docker-compose文件可以同时启动多个不同服务的容器
    
    root@docker-compose:/data/docker-compose# vim docker-compose.yml 
    server-nginx-web:
        image: nginx
        #container_name: nginx-web # 同时启动多个同一镜像的容器, 不要自定容器名称, 否则会冲突
        expose:
          - 80
          - 443
        #ports: # 同时启动多个同一镜像的容器, 不要指定端口号, 否则会冲突
        #  - "80:80"
        #  - "443:443"
    
    #再添加一个服务
    server-tomcat-web:
        image: tomcat
    
    --scale用来指定要启动的各种服务容器的数量, 如果, 没有指定--scale, 而yml文件有写了服务, 那么就会默认启动一个
    root@docker-compose:/data/docker-compose# docker-compose up -d --scale server-nginx-web=2
    root@docker-compose:/data/docker-compose# docker-compose ps
                   Name                             Command               State        Ports     
    ---------------------------------------------------------------------------------------------
    docker-compose_server-nginx-web_1    /docker-entrypoint.sh ngin ...   Up      443/tcp, 80/tcp
    docker-compose_server-nginx-web_2    /docker-entrypoint.sh ngin ...   Up      443/tcp, 80/tcp
    docker-compose_server-tomcat-web_1   catalina.sh run                  Up      8080/tcp 
    

    相关文章

      网友评论

          本文标题:6. 单机编排之Docker Compose

          本文链接:https://www.haomeiwen.com/subject/mbrmzktx.html