码迷,mamicode.com
首页 > 其他好文 > 详细

DOCKER学习_018:Docker-Compose文件简介

时间:2020-01-08 00:31:06      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:https   之间   pwd   open   日志输出   hostname   starting   proxy   pat   

通过之前的示例,其实我们可以看到,所有服务的管理,都是依靠docker-compose.yml文件来实现的。那么我们接下来就详细说一说docker-compose.yml文件中的常用指令。

compose文件使用yml格式,docker规定了一些指令,使用它们可以去设置对应的东西,主要分为了四个区域:

  • version:用于指定当前docker-compose.yml语法遵循哪个版本
  • services:服务,在它下面可以定义应用需要的一些服务,每个服务都有自己的名字、使用的镜像、挂载的数据卷、所属的网络、依赖哪些其他服务等等。
  • networks:应用的网络,在它下面可以定义应用的名字、使用的网络类型等。
  • volumes:数据卷,在它下面可以定义数据卷,然后挂载到不同的服务下去使用。

官方网站:https://docs.docker.com/compose/

1 version

用于指定当前compose文件语法遵循哪个版本,下面这张表是不同的Compose文件版本兼容的Docker版本:

[root@docker-server3 docker-compose]# docker-compose version

docker-compose version 1.25.0, build 0a186604
docker-py version: 4.1.0
CPython version: 3.7.4
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

这里的版本是这个指令的版本,上面的版本是语法的版本,语法不停的更新,version就是指定语法版本

也可以精确到小版本

将前面的版本修改为1,尝试

[root@docker-server3 docker-compose]#  vi docker-compose.yml 

version: 1
services:
  httpd-test:
    image: httpd:2.4
  httpd-test-2:
    image: httpd:2.4
    volumes:
      - "/data:/var/www/html"
    ports:
      - "80:80"
  httpd-test-3:
    image: httpd:2.4
    volumes:
      - "/data:/usr/local/apache2/htdocs"
    ports:
      - "8080:80"

[root@docker-server3 docker-compose]# docker-compose stop

使用指令报版本错误

ERROR: Version in "./docker-compose.yml" is invalid. You might be seeing this error because youre using the wrong Compose file version. Either specify a supported version (e.g "2.2" or "3.3") and place your service definitions under the `services` key, or omit the `version` key and place your service definitions at the root of the file to use version 1.
For more on the Compose file format versions, see https://docs.docker.com/compose/compose-file/

2 services

定义 docker- compose管理的服务,包括服务的名称、使用的镜像、挂载的数据卷、所属的网络、依赖等等

上面的实例

[root@docker-server3 docker-compose]# vi docker-compose.yml 

version: ‘3
services:
  #docker run -d --name docker-compose_httpd-test_1 httpd:2.4
  httpd-test:
    image: httpd:2.4
  #docker run -d --name docker-compose_httpd-test-2_1 -v /data:/var/www/html -p 80:80 httpd:2.4
  httpd-test-2:
    image: httpd:2.4
    volumes:
      - "/data:/var/www/html"
    ports:
      - "80:80"
  #docker run -d --name docker-compose_httpd-test-3_1 -v /data:/usr/local/apache2/htdocs:rw -p 8080:80 httpd:2.4
  httpd-test-3:
    image: httpd:2.4
    volumes:
      - "/data:/usr/local/apache2/htdocs:rw"
    ports:
      - "8080:80"

上面一共三个容器,使用dockers run运行就是

docker run -d --name docker-compose_httpd-test_1 httpd:2.4
docker run -d --name docker-compose_httpd-test-2_1 -v /data:/var/www/html -p 80:80 httpd:2.4
docker run -d --name docker-compose_httpd-test-3_1 -v /data:/usr/local/apache2/htdocs:rw -p 8080:80 httpd:2.4

第一行缩进就是容器的名字,但是在启动后,docker-compose会自动添加信息作为容器的名字

image:就是镜像的名字

容器在docker-compose都是后台运行的

3 volumes

自定义数据卷

下面的- 表示可以跟多个

version: ‘3
services:
  httpd-test-2:
    image: httpd:2.4
    volumes:
      - "/data:/var/www/html"
      - "/etc/localtime:/etc/localtime"
    ports:
      - "80:80"
      - "2222:22"

docker命令:docker run -d --name docker-compose_httpd-test-2_1 -v /data:/var/www/html  -v /etc/localtime:/etc/localtime  -p 2222:22  -p 80:80 httpd:2.4 

volumes,可以不指定挂载路径,则会把路径挂载在本地的默认路径下使用driver:local

4 environment

相当于-e传参数

version: 3
services:
  httpd-test:
    image: httpd:2.4
  httpd-test-2:
    image: httpd:2.4
    volumes:
      - "/data:/var/www/html"
      - "/etc/localtime:/etc/localtime"
    ports:
      - "80:80"
      - "2222:22"
  httpd-test-3:
    image: httpd:2.4
    volumes:
      - "/data:/usr/local/apache2/htdocs:rw"
    ports:
      - "8080:80"
    environment:
      index: "test"

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

Recreating docker-compose_httpd-test-2_1 ... 
Recreating docker-compose_httpd-test-2_1 ... done
Recreating docker-compose_httpd-test-3_1 ... done

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                                      NAMES
ab6ac7926b25        httpd:2.4           "httpd-foreground"   6 seconds ago       Up 4 seconds        0.0.0.0:8080->80/tcp                       docker-compose_httpd-test-3_1
c47a18060afe        httpd:2.4           "httpd-foreground"   6 seconds ago       Up 4 seconds        0.0.0.0:80->80/tcp, 0.0.0.0:2222->22/tcp   docker-compose_httpd-test-2_1
a1dabe398019        httpd:2.4           "httpd-foreground"   24 hours ago        Up 24 hours         80/tcp                                     docker-compose_httpd-test_1

[root@docker-server3 docker-compose]# docker exec -it ab6ac7926b25 /bin/bash

root@ab6ac7926b25:/usr/local/apache2# env
index=test      #定义的环境变量
HOSTNAME=ab6ac7926b25
PWD=/usr/local/apache2
HTTPD_VERSION=2.4.41
HOME=/root
HTTPD_PATCHES=
TERM=xterm
HTTPD_SHA256=133d48298fe5315ae9366a0ec66282fa4040efa5d566174481077ade7d18ea40
SHLVL=1
PATH=/usr/local/apache2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HTTPD_PREFIX=/usr/local/apache2
_=/usr/bin/env

5 networks

网络决定了服务之间以及服务和外界之间如何去通信,在执行docker-compose up的时候,docker会默认创建一个默认网络,创建的服务也会默认的使用这个默认网络。服务和服务之间,可以使用服务的名字进行通信,也可以自己创建网络,并将服务加入到这个网络之中,这样服务之间可以相互通信,而外界不能够与这个网络中的服务通信,可以保持隔离性。

使用host网络

version: 3
services:
  httpd-test:
    image: httpd:2.4
  httpd-test-2:
    image: httpd:2.4
    volumes:
      - "/data:/var/www/html"
      - "/etc/localtime:/etc/localtime"
    ports:
      - "2222:22"
  httpd-test-3:
    image: httpd:2.4
    volumes:
      - "/data:/usr/local/apache2/htdocs:rw"
    network_mode: host
    #ports:
    #  - "8080:80"
    environment:
      index: "test"

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

Recreating docker-compose_httpd-test-3_1 ... 
Recreating docker-compose_httpd-test-3_1 ... done
Recreating docker-compose_httpd-test-2_1 ... done

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                          NAMES
5ab1c92c7465        httpd:2.4           "httpd-foreground"   4 seconds ago       Up 3 seconds                                       docker-compose_httpd-test-3_1
84bc3e5e4622        httpd:2.4           "httpd-foreground"   4 seconds ago       Up 3 seconds        80/tcp, 0.0.0.0:2222->22/tcp   docker-compose_httpd-test-2_1
a1dabe398019        httpd:2.4           "httpd-foreground"   24 hours ago        Up 24 hours         80/tcp                         docker-compose_httpd-test_1

进入容器确认配置

[root@docker-server3 docker-compose]#  docker exec -it  5ab1c92c7465  /bin/bash

root@docker-server3:/usr/local/apache2# apt update

root@docker-server3:/usr/local/apache2# apt install net-tools

root@docker-server3:/usr/local/apache2# netstat -ntlp 

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::2222                 :::*                    LISTEN      -                   
tcp6       0      0 :::80                   :::*                    LISTEN      1/httpd             
tcp6       0      0 :::22                   :::*                    LISTEN      -                   
tcp6       0      0 ::1:25                  :::*                    LISTEN      -  

root@docker-server3:/usr/local/apache2# apt install iproute2

root@docker-server3:/usr/local/apache2# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:f0:04:c9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.132.133/24 brd 192.168.132.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::96b0:35ea:c6b0:7bbb/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:ec:71:fc:86 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.1/24 brd 192.168.0.255 scope global docker0
       valid_lft forever preferred_lft forever
4: br-8f5945aac199: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:b0:d4:21:b4 brd ff:ff:ff:ff:ff:ff
    inet 172.22.16.1/24 brd 172.22.16.255 scope global br-8f5945aac199
       valid_lft forever preferred_lft forever
5: br-a1db538e7759: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:cf:8b:20:fd brd ff:ff:ff:ff:ff:ff
    inet 172.18.0.1/16 brd 172.18.255.255 scope global br-a1db538e7759
       valid_lft forever preferred_lft forever
    inet6 fe80::42:cfff:fe8b:20fd/64 scope link 
       valid_lft forever preferred_lft forever
12475: br-4b1032d9c860: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:77:39:81:1e brd ff:ff:ff:ff:ff:ff
    inet 172.19.0.1/16 brd 172.19.255.255 scope global br-4b1032d9c860
       valid_lft forever preferred_lft forever
    inet6 fe80::42:77ff:fe39:811e/64 scope link 
       valid_lft forever preferred_lft forever
12487: veth5185239@if12486: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-4b1032d9c860 state UP group default 
    link/ether d2:ea:d1:45:fb:fd brd ff:ff:ff:ff:ff:ff link-netnsid 1
    inet6 fe80::d0ea:d1ff:fe45:fbfd/64 scope link 
       valid_lft forever preferred_lft forever
12493: veth4949bb2@if12492: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-4b1032d9c860 state UP group default 
    link/ether 8e:ff:3d:8b:a7:07 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::8cff:3dff:fe8b:a707/64 scope link 
       valid_lft forever preferred_lft forever

6 安装Harbor的各个文件解释

[root@docker-server3 docker-compose]# cd /usr/local/harbor/

[root@docker-server3 harbor]# ll

drwxr-xr-x 3 root root    20 Jan  3 03:34 common
-rw-r--r-- 1 root root  5345 Jan  3 03:57 docker-compose.yml
-rw-r--r-- 1 root root  5821 Jan  3 03:57 harbor.yml
-rwxr-xr-x 1 root root  5088 Nov 18 03:37 install.sh
-rw-r--r-- 1 root root 11347 Nov 18 03:37 LICENSE
drwxr-xr-x 2 root root    60 Jan  3 03:33 pki
-rwxr-xr-x 1 root root  1748 Nov 18 03:37 prepare

最初只有harbor.yml,install.sh,和prepare三个文件

修改harbar.yml文件,然后执行prepare,就会把所有的需要的文件放在common里面。同时生成一个docker-compose,yml文件

install.sh就是把docker-compose.yml文件启动起来

所以也可以不用instll.sh.直接启动

[root@docker-server3 harbor]# docker-compose up -d

Creating harbor-log ... done
Creating registryctl   ... done
Creating harbor-portal ... done
Creating harbor-db     ... done
Creating registry      ... done
Creating redis         ... done
Creating harbor-core   ... done
Creating nginx             ... done
Creating harbor-jobservice ... done

[root@docker-server3 harbor]# docker ps -a

CONTAINER ID        IMAGE                                                    COMMAND                  CREATED             STATUS                             PORTS                                         NAMES
e4c160319a13        goharbor/harbor-jobservice:v1.9.3                        "/harbor/harbor_jobs…"   30 seconds ago      Up 29 seconds (health: starting)                                                 harbor-jobservice
4148f0b1f95f        goharbor/nginx-photon:v1.9.3                             "nginx -g ‘daemon of…"   30 seconds ago      Up 29 seconds (health: starting)   0.0.0.0:80->8080/tcp, 0.0.0.0:443->8443/tcp   nginx
6b29d3e6b527        goharbor/harbor-core:v1.9.3                              "/harbor/harbor_core"    30 seconds ago      Up 29 seconds (health: starting)                                                 harbor-core
e984dd36124a        goharbor/redis-photon:v1.9.3                             "redis-server /etc/r…"   31 seconds ago      Up 30 seconds (health: starting)   6379/tcp                                      redis
9a95a1282c96        goharbor/registry-photon:v2.7.1-patch-2819-2553-v1.9.3   "/entrypoint.sh /etc…"   31 seconds ago      Up 30 seconds (health: starting)   5000/tcp                                      registry
1cb8ff7be670        goharbor/harbor-db:v1.9.3                                "/docker-entrypoint.…"   31 seconds ago      Up 30 seconds (healthy)            5432/tcp                                      harbor-db
88ab0ce7c486        goharbor/harbor-portal:v1.9.3                            "nginx -g ‘daemon of…"   31 seconds ago      Up 30 seconds (healthy)            8080/tcp                                      harbor-portal
6abda5d0dc91        goharbor/harbor-registryctl:v1.9.3                       "/harbor/start.sh"       31 seconds ago      Up 30 seconds (healthy)                                                          registryctl
816b9f7851c3        goharbor/harbor-log:v1.9.3                               "/bin/sh -c /usr/loc…"   31 seconds ago      Up 31 seconds (healthy)            127.0.0.1:1514->10514/tcp                     harbor-log

7 查看docker-compose.yml

[root@docker-server3 harbor]# cat docker-compose.yml

version: 2.3
services:
  log:
    image: goharbor/harbor-log:v1.9.3
    container_name: harbor-log
    restart: always
    dns_search: .
    cap_drop:
      - ALL
    cap_add:
      - CHOWN
      - DAC_OVERRIDE
      - SETGID
      - SETUID
    volumes:
      - /var/log/harbor/:/var/log/docker/:z                                                #这里是容器日志的位置
      - ./common/config/log/logrotate.conf:/etc/logrotate.d/logrotate.conf:z
      - ./common/config/log/rsyslog_docker.conf:/etc/rsyslog.d/rsyslog_docker.conf:z
    ports:
      - 127.0.0.1:1514:10514
    networks:
      - harbor
  registry:
    image: goharbor/registry-photon:v2.7.1-patch-2819-2553-v1.9.3
    container_name: registry
    restart: always
    cap_drop:
      - ALL
    cap_add:
      - CHOWN
      - SETGID
      - SETUID
    volumes:
      - /data/registry:/storage:z
      - ./common/config/registry/:/etc/registry/:z
      - type: bind
        source: /data/secret/registry/root.crt
        target: /etc/registry/root.crt
    networks:
      - harbor
    dns_search: .
    depends_on:
      - log
    logging:
      driver: "syslog"
      options:  
        syslog-address: "tcp://127.0.0.1:1514"
        tag: "registry"
  registryctl:  
    image: goharbor/harbor-registryctl:v1.9.3     #镜像
    container_name: registryctl                   #容器的hostname
    env_file:                              
      - ./common/config/registryctl/env
    restart: always                               #容器挂掉自动重启
    cap_drop: 
      - ALL
    cap_add:
      - CHOWN
      - SETGID
      - SETUID
    volumes:
      - /data/registry:/storage:z               #:z表示使用默认权限
      - ./common/config/registry/:/etc/registry/:z
      - type: bind                             #写法不同,功能是一致的
        source: ./common/config/registryctl/config.yml
        target: /etc/registryctl/config.yml
    networks:
      - harbor                                   #使用的自定义的网络
    dns_search: .
    depends_on:                                   #依赖的容器
      - log
    logging:                                      #定义的日志输出
      driver: "syslog"
      options:  
        syslog-address: "tcp://127.0.0.1:1514"    #这是把日志输出到log日志容器里面
        tag: "registryctl"
  postgresql:
    image: goharbor/harbor-db:v1.9.3
    container_name: harbor-db
    restart: always
    cap_drop:
      - ALL
    cap_add:
      - CHOWN
      - DAC_OVERRIDE
      - SETGID
      - SETUID
    volumes:
      - /data/database:/var/lib/postgresql/data:z
    networks:
      harbor:
    dns_search: .
    env_file:                               #单个写是environment定义环境变量,但是也可以统一写道一个文件,然后使用这个调用
      - ./common/config/db/env
    depends_on:
      - log
    logging:
      driver: "syslog"
      options:  
        syslog-address: "tcp://127.0.0.1:1514"
        tag: "postgresql"
  core:
    image: goharbor/harbor-core:v1.9.3
    container_name: harbor-core
    env_file:
      - ./common/config/core/env
    restart: always
    cap_drop:
      - ALL
    cap_add:
      - SETGID
      - SETUID
    volumes:
      - /data/ca_download/:/etc/core/ca/:z
      - /data/psc/:/etc/core/token/:z
      - /data/:/data/:z
      - ./common/config/core/certificates/:/etc/core/certificates/:z
      - type: bind
        source: ./common/config/core/app.conf
        target: /etc/core/app.conf
      - type: bind
        source: /data/secret/core/private_key.pem
        target: /etc/core/private_key.pem
      - type: bind
        source: /data/secret/keys/secretkey
        target: /etc/core/key
    networks:
      harbor:
    dns_search: .
    depends_on:
      - log
      - registry
      - redis
      - postgresql
    logging:
      driver: "syslog"
      options:  
        syslog-address: "tcp://127.0.0.1:1514"
        tag: "core"
  portal:
    image: goharbor/harbor-portal:v1.9.3
    container_name: harbor-portal
    restart: always
    cap_drop:
      - ALL
    cap_add:
      - CHOWN
      - SETGID
      - SETUID
      - NET_BIND_SERVICE
    networks:
      - harbor
    dns_search: .
    depends_on:
      - log
    logging:
      driver: "syslog"
      options:
        syslog-address: "tcp://127.0.0.1:1514"
        tag: "portal"

  jobservice:
    image: goharbor/harbor-jobservice:v1.9.3
    container_name: harbor-jobservice
    env_file:
      - ./common/config/jobservice/env
    restart: always
    cap_drop:
      - ALL
    cap_add:
      - CHOWN
      - SETGID
      - SETUID
    volumes:
      - /data/job_logs:/var/log/jobs:z
      - type: bind
        source: ./common/config/jobservice/config.yml
        target: /etc/jobservice/config.yml
    networks:
      - harbor
    dns_search: .
    depends_on:
      - core
    logging:
      driver: "syslog"
      options:
        syslog-address: "tcp://127.0.0.1:1514"
        tag: "jobservice"
  redis:
    image: goharbor/redis-photon:v1.9.3
    container_name: redis
    restart: always
    cap_drop:
      - ALL
    cap_add:
      - CHOWN
      - SETGID
      - SETUID
    volumes:
      - /data/redis:/var/lib/redis
    networks:
      harbor:
    dns_search: .
    depends_on:
      - log
    logging:
      driver: "syslog"
      options:
        syslog-address: "tcp://127.0.0.1:1514"
        tag: "redis"
  proxy:
    image: goharbor/nginx-photon:v1.9.3
    container_name: nginx
    restart: always
    cap_drop:
      - ALL
    cap_add:
      - CHOWN
      - SETGID
      - SETUID
      - NET_BIND_SERVICE
    volumes:
      - ./common/config/nginx:/etc/nginx:z
      - /data/secret/cert:/etc/cert:z
    networks:
      - harbor
    dns_search: .
    ports:
      - 80:8080
      - 443:8443
    depends_on:
      - registry
      - core
      - portal
      - log
    logging:
      driver: "syslog"
      options:  
        syslog-address: "tcp://127.0.0.1:1514"
        tag: "proxy"
networks:
  harbor:
    external: false

以上就是关于daoker-compose配置文件的一个简单介绍,可以参阅官方的文档,进行更深入的研究


博主声明:本文的内容来源主要来自誉天教育晏威老师,由本人实验完成操作验证,需要的博友请联系誉天教育(http://www.yutianedu.com/),获得官方同意或者晏老师(https://www.cnblogs.com/breezey/)本人同意即可转载,谢谢!

DOCKER学习_018:Docker-Compose文件简介

标签:https   之间   pwd   open   日志输出   hostname   starting   proxy   pat   

原文地址:https://www.cnblogs.com/zyxnhr/p/12164001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!