docker组成 – 如何设置名称属性

下面这个问题,我想知道是否有一个选项将名称标签添加到compose.yml文件。

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

但是对我而言这并不好,因为我想确保图像只运行一次,并且虽然container_name是唯一的,但在每次运行时,docker-compose都会附加到运行编号,所以只有当我尝试两次同时运行映像一次运行将失败。

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

我不知道为什么container_name不适合你, docker-compose up的输出可能会提示,否则它会创build一个名称为:

 $ 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.