如何在运行docker-compose时设置容器ID?

当我运行docker build我可以使用-t--tag参数来遏制CONTAINER ID

https://docs.docker.com/engine/reference/commandline/build/

但是,当我使用docker-compose然后我找不到这个选项。

https://docs.docker.com/compose/reference/up/

我知道docker-composer可以创build许多容器,所以也许有docker-compose.yml中设置CONTAINER ID可能性吗? 这个怎么做?

您可以在您的Docker撰写文件中指定它,例如docker-compose.yaml上执行docker-compose.yaml

build设和部署工作方式相同。 只需将图像名称和标记设置为构build的子集或指定单独的dockerfile即可。

 version: '2' services: webapp: build: context: ./dir image: username/repository:tag 

鉴于docker-compose.yaml运行docker-compose.yaml docker-compose up将产生以下内容:

 $ docker-compose up Building webapp Step 1/1 : FROM hello-world latest: Pulling from library/hello-world 5b0f327be733: Pull complete Digest: sha256:1f1404e9ea1a6665e3664626c5d2cda76cf90a4df50cfee16aab1a78f58a3f95 Status: Downloaded newer image for hello-world:latest ---> 05a3bd381fc2 Successfully built 05a3bd381fc2 Successfully tagged myusername/myimagename:1.0 WARNING: Image for service webapp was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`. Creating dockercompose_webapp_1 ... Creating dockercompose_webapp_1 ... done Attaching to dockercompose_webapp_1 webapp_1 | webapp_1 | Hello from Docker! webapp_1 | This message shows that your installation appears to be working correctly. webapp_1 | webapp_1 | To generate this message, Docker took the following steps: webapp_1 | 1. The Docker client contacted the Docker daemon. webapp_1 | 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. webapp_1 | 3. The Docker daemon created a new container from that image which runs the webapp_1 | executable that produces the output you are currently reading. webapp_1 | 4. The Docker daemon streamed that output to the Docker client, which sent it webapp_1 | to your terminal. webapp_1 | webapp_1 | To try something more ambitious, you can run an Ubuntu container with: webapp_1 | $ docker run -it ubuntu bash webapp_1 | webapp_1 | Share images, automate workflows, and more with a free Docker ID: webapp_1 | https://cloud.docker.com/ webapp_1 | webapp_1 | For more examples and ideas, visit: webapp_1 | https://docs.docker.com/engine/userguide/ webapp_1 | dockercompose_webapp_1 exited with code 0 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES da057afd3277 myusername/myimagename:1.0 "/hello" 3 minutes ago Exited (0) 3 minutes ago dockercompose_webapp_1 $ ls Dockerfile docker-compose.yaml 

正如你所看到的docker-compose up都build立了容器启动它。 它也被相应地命名。 因此,不需要同时执行docker-compose builddocker-compose up因为docker-compose up会同时构build和启动容器。 最后,如果因为任何原因需要重build容器,你可以简单地运行docker-compose up --force-recreate这将重新docker-compose up --force-recreate --build容器,并附加build标签将重新创build图像: docker-compose up --force-recreate --build

docker build的-t选项不会设置CONTAINER ID 。 事实上,它与一个容器无关。 docker build的输出是一个基于-t选项命名的映像docker build -t myorg:myimage . 创build一个名为myorg:myimage的图像, 稍后您可以使用它来构build容器,或者推送到dockerregistry,以便稍后使用它来构build容器。

在docker-compose中的等价物是docker-compose build ,而不是docker-compose up 。 要在docker-compose构build中指定一个图片标记,您可以在build文件中的服务上使用buildimage标记 – 在这种情况下,使用docker-compose build将构build基于build指令的图像,并标记使用image标签输出。