当前位置:   article > 正文

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

container_name

docker-compose service name和container name的关系

问题

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

  1. $ cat docker-compose.yaml
  2. version: '2'
  3. networks:
  4. mynet:
  5. services:
  6. linuxservice:
  7. image: oraclelinux
  8. container_name: linuxservice.example.com
  9. command: sleep 5000
  10. networks:
  11. - mynet

基本概念

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

以上述为例:

  1. $ docker-compose up
  2. $ docker ps
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 94e6bc9e06a6 oraclelinux "sleep 5000" 17 seconds ago Up 15 seconds linuxservice.example.com
  5. $ docker-compose stop linuxservice
  6. Stopping linuxservice.example.com ... done
  7. $ docker-compose start linuxservice
  8. Starting linuxservice ... done
  9. $ docker-compose stop linuxservice.example.com
  10. ERROR: No such service: linuxservice.example.com
  11. $ docker-compose start linuxservice.example.com
  12. ERROR: No such service: linuxservice.example.com

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

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

  1. $ cat docker-compose.yaml
  2. version: '2'
  3. networks:
  4. mynet:
  5. services:
  6. linuxservice:
  7. image: oraclelinux
  8. #container_name: linuxservice.example.com
  9. command: sleep 5000
  10. networks:
  11. - mynet

起来之后:

  1. $ docker-compose up
  2. Creating network "test_mynet" with the default driver
  3. Creating test_linuxservice_1 ... done
  4. Attaching to test_linuxservice_1
  5. $ docker-compose ps
  6. Name Command State Ports
  7. ------------------------------------------------
  8. test_linuxservice_1 sleep 5000 Up

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

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

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

我们一次启动5个linuxservice containers:

  1. $ docker-compose up --scale linuxservice=5
  2. Creating test_linuxservice_1 ... done
  3. Creating test_linuxservice_2 ... done
  4. Creating test_linuxservice_3 ... done
  5. Creating test_linuxservice_4 ... done
  6. Creating test_linuxservice_5 ... done
  7. Attaching to test_linuxservice_1, test_linuxservice_3, test_linuxservice_2, test_linuxservice_5, test_linuxservice_4
  8. $ docker ps
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  10. 111c825dba18 oraclelinux "sleep 5000" 38 seconds ago Up 33 seconds test_linuxservice_4
  11. c5a555dd53aa oraclelinux "sleep 5000" 39 seconds ago Up 33 seconds test_linuxservice_5
  12. 76b377abf70a oraclelinux "sleep 5000" 39 seconds ago Up 34 seconds test_linuxservice_2
  13. ec5e33032dd0 oraclelinux "sleep 5000" 39 seconds ago Up 35 seconds test_linuxservice_3
  14. 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 名称

 

  1. version: '3'
  2. services:
  3. bbox0001:
  4. container_name: cname-bbox0001
  5. image: busybox:latest
  6. restart: always
  7. privileged: true
  8. networks:
  9. - busybox-networks
  10. ports:
  11. - 3003:3003
  12. command: ping cname-bbox0002:3004
  13. bbox0002:
  14. container_name: cname-bbox0002
  15. image: busybox:latest
  16. restart: always
  17. privileged: true
  18. networks:
  19. - busybox-networks
  20. ports:
  21. - 3004:3004
  22. command: sh -c "ping bbox0001:3003 -c 1 ;ping cname-bbox0001:3003 -c 1"
  23. # - ping cname-bbox0001:3003
  24. # - ls
  25. # - echo 'success'
  26. networks:
  27. busybox-networks:
  28. driver: bridge

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/74332
推荐阅读
相关标签
  

闽ICP备14008679号