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。
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容