docker-compose 中 service name和container name的关系

docker-compose service name和container name的关系

问题

我们在定义docker-compose.yaml文件里面经常会有service name和container name,这两者有什么关系呢?

$ cat docker-compose.yaml 
version: '2'
networks:
  mynet:

services:
  linuxservice:
    image: oraclelinux
    container_name: linuxservice.example.com
    command: sleep 5000
    networks:
        - mynet 

基本概念

  1. 一个service可以拥有一个或多个container。
  2. container是docker的概念,因此我们在docker域里面,处理的是container。
  3. service是docker-compose概念, 因此我们在docker-compose域里面,才处理的是service。(当然docker-compose也能处理container)。

以上述为例:

$ docker-compose up

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
94e6bc9e06a6        oraclelinux         "sleep 5000"        17 seconds ago      Up 15 seconds                           linuxservice.example.com

$ docker-compose stop linuxservice
Stopping linuxservice.example.com ... done

$ docker-compose start linuxservice
Starting linuxservice ... done

$ docker-compose stop linuxservice.example.com
ERROR: No such service: linuxservice.example.com

$ docker-compose start linuxservice.example.com
ERROR: No such service: linuxservice.example.com

我们可以看到docker-compose start/stop处理的service name,而不是container name。

例子1:如果container name没有定义

$ cat docker-compose.yaml 
version: '2'
networks:
  mynet:

services:
  linuxservice:
    image: oraclelinux
   #container_name: linuxservice.example.com
    command: sleep 5000
    networks:
        - mynet 

起来之后:

$ docker-compose up
Creating network "test_mynet" with the default driver
Creating test_linuxservice_1 ... done
Attaching to test_linuxservice_1

$ docker-compose ps
       Name            Command     State   Ports
------------------------------------------------
test_linuxservice_1   sleep 5000   Up          

我们看到docker-compose自动给container分配了一个名字,其格式为:<当前工作路径名>_<servicename>_<sequencenumber>。

sequencenumber是干什么用的呢,我们看后面的例子。

例子2:一个service包含多个container

我们一次启动5个linuxservice containers:

$ docker-compose up --scale linuxservice=5
Creating test_linuxservice_1 ... done
Creating test_linuxservice_2 ... done
Creating test_linuxservice_3 ... done
Creating test_linuxservice_4 ... done
Creating test_linuxservice_5 ... done
Attaching to test_linuxservice_1, test_linuxservice_3, test_linuxservice_2, test_linuxservice_5, test_linuxservice_4

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
111c825dba18        oraclelinux         "sleep 5000"        38 seconds ago      Up 33 seconds                           test_linuxservice_4
c5a555dd53aa        oraclelinux         "sleep 5000"        39 seconds ago      Up 33 seconds                           test_linuxservice_5
76b377abf70a        oraclelinux         "sleep 5000"        39 seconds ago      Up 34 seconds                           test_linuxservice_2
ec5e33032dd0        oraclelinux         "sleep 5000"        39 seconds ago      Up 35 seconds                           test_linuxservice_3
b15cf589db88        oraclelinux         "sleep 5000"        39 seconds ago      Up 37 seconds                           test_linuxservice_1

这个例子中我们看到service有5个container被创建出来,每一container的sequence是从1开始累加。

注意:

  1. 前面的docker-compose stop/start会对5个container一起操作。
  2. 此时就不能指定container name,因为不能5个container使用同样的container name。

容器间通讯 

可以是 container 名称也可以说 service 名称

 

version: '3'
services:
    bbox0001:
       container_name: cname-bbox0001
       image: busybox:latest
       restart: always
       privileged: true
       networks:
         - busybox-networks
       ports:
         - 3003:3003
       command: ping cname-bbox0002:3004
    bbox0002:
       container_name: cname-bbox0002
       image: busybox:latest
       restart: always
       privileged: true
       networks:
         - busybox-networks
       ports:
         - 3004:3004
       command: sh -c "ping bbox0001:3003 -c 1 ;ping cname-bbox0001:3003 -c 1"
        # - ping cname-bbox0001:3003
        # - ls
        # - echo 'success'
networks:
  busybox-networks:
    driver: bridge

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值