docker compose-如何设置name属性

时间:2017-03-02 08:42:17

标签: docker docker-compose

this问题后,我想知道是否有选项可以将名称标签添加到compose.yml文件中。

从我在网上看到的,它说使用container_name属性。

但在我的情况下这并不好,因为我想确保图像只运行一次,而且container_name是唯一的,在每次运行时,docker-compose会向其追加运行编号,所以只有如果我尝试同时运行图像两次,则一次运行将失败。

我需要找到一种在yml文件中设置参数的方法,这将帮助我确保图像只运行一次。 关于如何做到这一点的任何想法?

1 个答案:

答案 0 :(得分:0)

我不确定container_name为什么不适合您,docker-compose up的输出可能会另有建议,但它会创建一个具有该名称的容器:

$ cat docker-compose.name.yml
version: '2'

services:
  test:
    image: busybox:latest
    command: tail -f /dev/null
    container_name: unique_name

$ docker-compose -f docker-compose.name.yml up -d
Recreating test_test_1

$ docker ps -a | grep test_test_1

$ docker ps -a | grep unique
80b44bc94912        busybox:latest      "tail -f /dev/null"      About a minute ago   Up 59 seconds   

$ docker-compose -f docker-compose.name.yml scale test=3
Creating and starting unique_name ... error
Creating and starting unique_name ...

ERROR: for unique_name  Cannot create container for service test: Conflict. The container name "/unique_name" is already in use by container 80b44bc94912b755cf2430b132fa6112f960e2752f69a357c27375bbc905ff76. You have to remove (or rename) that container to be able to reuse that name.