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

时间:2017-09-13 01:23:25

标签: docker docker-compose dockerfile

当我运行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可以创建多个容器,因此可能在CONTAINER ID中设置docker-compose.yml?怎么做?

2 个答案:

答案 0 :(得分:1)

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

docker-compose中的等效词是docker-compose build,而不是docker-compose up。要在docker-compose构建中指定图像标记,您可以在撰写文件中使用服务上的buildimage标记 - 在这种情况下,使用docker-compose build将构建图像基于build指令,并使用image标记标记输出。

答案 1 :(得分:0)

您在Docker Compose文件中指定了docker-compose正在执行的文件,例如docker-compose.yaml

构建和部署以相​​同的方式工作。只需将图像名称和标记设置为subset of build或指定单独的dockerfile。

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

鉴于运行docker-compose.yaml的{​​{1}}会产生以下结果:

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 同时构建容器 AND 启动它。它也相应地命名。因此,docker-compose updocker-compose build都不需要同时进行,因为docker-compose up将构建并启动容器。最后,如果因任何原因需要重建容器,您只需运行docker-compose up将停靠以重建容器,并附加构建标记将重新创建图像:docker-compose up --force-recreate