docker-compose文件结构语法解析

version: "3"  # 指定docker-compose语法版本
services:    # 从以下定义服务配置列表
  server_name:   # 可将server_name替换为自定义的名字,如mysql/php都可以
    container_name: container_name  # 指定实例化后的容器名,可将container_name替换为自定义名
    image: xxx:latest # 指定使用的镜像名及标签
    build:  # 如果没有现成的镜像,需要自己构建使用这个选项
      context: /xxx/xxx/Dockerfile  # 指定构建镜像文件的路径
      dockerfile: ....     # 指定Dockerfile文件名,上一条指定,这一条就不要了
    ports:
      - "00:00"  # 容器内的映射端口,本地端口:容器内端口
      - "00:00"  # 可指定多个
    volumes:
      - test1:/xx/xx  # 这里使用managed volume的方法,将容器内的目录映射到物理机,方便管理
      - test2:/xx/xx  # 前者是volumes目录下的名字,后者是容器内目录
      - test3:/xx/xx  # 在文件的最后还要使用volumes指定这几个tests
    volumes_from:  # 指定卷容器
       - volume_container_name  # 卷容器名
    restarts: always  # 设置无论遇到什么错,重启容器
    depends_on:       # 用来解决依赖关系,如这个服务的启动,必须在哪个服务启动之后
      - server_name   # 这个是名字其他服务在这个文件中的server_name
      - server_name1  # 按照先后顺序启动
    links:  # 与depend_on相对应,上面控制容器启动,这个控制容器连接
      - mysql  # 值可以是- 服务名,比较复杂,可以在该服务中使用links中mysql代替这个mysql的ip
    networks: # 加入指定的网络,与之前的添加网卡名类似
      - my_net  # bridge类型的网卡名
      - myapp_net # 如果没有网卡会被创建,建议使用时先创建号,在指定
    environment: # 定义变量,类似dockerfile中的ENV
      - TZ=Asia/Shanghai  # 这里设置容器的时区为亚洲上海,也就解决了容器通过compose编排启动的 时区问题!!!!解决了容器的时区问题!!!
      变量值: 变量名   # 这些变量将会被直接写到镜像中的/etc/profile
    command: [                        #使用 command 可以覆盖容器启动后默认执行的命令
            '--character-set-server=utf8mb4',            #设置数据库表的数据集
            '--collation-server=utf8mb4_unicode_ci',    #设置数据库表的数据集
            '--default-time-zone=+8:00'                    #设置mysql数据库的 时区问题!!!! 而不是设置容器的时区问题!!!!
    ]
  server_name2:  # 开始第二个容器
    server_name:
      stdin_open: true # 类似于docker run -d
      tty: true  # 类似于docker run -t
volumes:   # 以上每个服务中挂载映射的目录都在这里写入一次,也叫作声明volume
  test1:
  test2:
  test3:
networks:  # 如果要指定ip网段,还是创建好在使用即可,声明networks
  my_net:
    driver: bridge  # 指定网卡类型
  myapp_net:
    driver: bridge 

network示例

如下文件中,web1使用dev网卡,web2使用dev/pro网卡,web3使用pro网卡,并且网卡类型都为bridge,最后声明之后,如果默认没有这些网卡,则会创建

[root@localhost ~]# mkdir test
[root@localhost ~]# cd test/
[root@localhost test]# vim docker-compose.yml
version: '3'
services:
  web1:
    image: nginx
    ports:
      - "6060:80"
    container_name: web1
    networks:
      - dev
  web2:
    image: nginx
    ports:
      - "6061:80"
    container_name: web2
    networks:
      - dev
      - pro
  web3:
    image: nginx
    ports:
      - "6062:80"
    container_name: web3
    networks:
      - pro
networks:
  dev:
    driver: bridge
  pro:
    driver: bridge

运行docker-compose.yml

[root@localhost test]# docker-compose pull
Pulling web1 ... done
Pulling web2 ... done
Pulling web3 ... done
[root@localhost test]# docker-compose up -d
Creating network "test_dev" with driver "bridge"
Creating network "test_pro" with driver "bridge"
Creating web1 ... done
Creating web3 ... done
Creating web2 ... done

在启动过程中可以看到创建了两块网卡,查看一下

[root@localhost test]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
b5f7f7433bac        bridge              bridge              local
dc8bfdbda464        host                host                local
ecbab8a758e6        none                null                local
6ce631d172e3        test_dev            bridge              local
481451eceff4        test_pro            bridge              local

查看网卡网段信息

[root@localhost test]# docker network inspect test_dev
[root@localhost test]# docker network inspect test_pro

建议创建好网卡后再使用,更加灵活

访问验证

[root@localhost test]# curl 192.168.1.12:6060
[root@localhost test]# curl 192.168.1.12:6061
[root@localhost test]# curl 192.168.1.12:6062

使用joined的方式查看容器中的网卡信息,因为web2是使用了两块网卡,所以只看web2即可

[root@localhost ~]# docker run -it --network container:web2 --rm busybox /bin/sh
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
12: eth0@if13: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:ac:12:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.18.0.3/16 brd 172.18.255.255 scope global eth0
       valid_lft forever preferred_lft forever
14: eth1@if15: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:ac:13:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.19.0.3/16 brd 172.19.255.255 scope global eth1
       valid_lft forever preferred_lft forever

docker-compose常用命令

只构建镜像

当构建文件(Dockerfile)出错时,修改后,加build会重新构建

docker-compose build

只下载镜像docker-compose.yml文件中的镜像

docker-compose pull

启动容器,-d后台启动

执行此命令会先pull后启动

docker-compose up -d

查看使用docker-compose.yml启动的容器

docker-compose ps

临时进入docker-compose.yml启动的容器(查看不到)

docker-compose run  容器名 /bin/sh

启动docker-compose.yml中的容器

docker-compose start

停止docker-compose.yml中的容器

docker-compose stop

删除使用docker-compose.yml运行的容器(需要先stop)

docker-compose rm

伸缩容器

那个服务的需求量大,可以批量启动同一服务的容器

伸缩的注意事项

docker-compose.yml中的version必须是3

不能指定container_name和映射端口号,否则会冲突

如:web集群

例:构建httpd简单镜像

[root@localhost ~]# mkdir /web
[root@localhost ~]# cd /web
[root@localhost web]# mkdir httpd
[root@localhost web]# echo "This is web cluster" >> httpd/index.html
[root@localhost web]# vim httpd/Dockerfile
FROM httpd:latest
COPY index.html /usr/local/apache2/htdocs
VOLUME /web/httpd/ /usr/local/apache2/htdocs

编写docker-compose.yml

[root@localhost web]# vim docker-compose.yml
version: '3'
services:
  web1:
    build: /web/httpd/
    ports:
      - "80"
    networks:
      - my_net
networks:
  my_net:
    driver: bridge

批量部署5台web

# 先部署一台检测文件
[root@localhost web]# docker-compose up -d
Creating web_web1_1 ... done

伸缩5台

[root@localhost web]# docker-compose scale web1=5
Starting web_web1_1 ... done
Creating web_web1_2 ... done
Creating web_web1_3 ... done
Creating web_web1_4 ... done
Creating web_web1_5 ... done

查看启动容器

[root@localhost web]# docker-compose ps
   Name          Command        State           Ports        
-------------------------------------------------------------
web_web1_1   httpd-foreground   Up      0.0.0.0:32769->80/tcp
web_web1_2   httpd-foreground   Up      0.0.0.0:32770->80/tcp
web_web1_3   httpd-foreground   Up      0.0.0.0:32771->80/tcp
web_web1_4   httpd-foreground   Up      0.0.0.0:32773->80/tcp
web_web1_5   httpd-foreground   Up      0.0.0.0:32772->80/tcp

访问验证

[root@localhost web]# curl 192.168.1.12:32769
This is web cluster
[root@localhost web]# curl 192.168.1.12:32770
This is web cluster
[root@localhost web]# curl 192.168.1.12:32771
This is web cluster
[root@localhost web]# curl 192.168.1.12:32772
This is web cluster
[root@localhost web]# curl 192.168.1.12:32773
This is web cluster

当然也可以直接一步启动多个容器

# 先删除停止5个容器的运行
[root@localhost web]# docker-compose stop
Stopping web_web1_5 ... done
Stopping web_web1_2 ... done
Stopping web_web1_3 ... done
Stopping web_web1_4 ... done
Stopping web_web1_1 ... done
# 删除容器
[root@localhost web]# docker-compose rm
Going to remove web_web1_5, web_web1_2, web_web1_3, web_web1_4, web_web1_1
Are you sure? [yN] y
Removing web_web1_5 ... done
Removing web_web1_2 ... done
Removing web_web1_3 ... done
Removing web_web1_4 ... done
Removing web_web1_1 ... done
# 直接启动3个容器,--no-recreate不重复创建,一步完成必须加
[root@localhost web]# docker-compose up -d scale web1=3 --no-recreate
ERROR: No such service: scale
[root@localhost web]# docker-compose up -d --scale web1=3 --no-recreate
Creating web_web1_1 ... done
Creating web_web1_2 ... done
Creating web_web1_3 ... done

评论




正在载入...
PoweredHexo
HostedAliyun
DNSAliyun
ThemeVolantis
UV
PV
BY-NC-SA 4.0