我应该使用 docker-compose up 还是 运行?
Should I use docker-compose up or run?
是否有理由使用 run
来启动 docker-compose.yml
文件,还是应该只使用 up
?
我知道run
可以启动一个特定的容器,但我指的是你在没有指定容器的情况下使用它,这样它就启动了你所有的yml容器。
如 docker-compose run
(2015) 所述
The command passed by run overrides the command defined in the service configuration.
For example, if the web service configuration is started with bash
, then docker-compose run web python app.py
overrides it with python app.py
.
The second difference is the docker-compose run
command does not create any of the ports specified in the service configuration.
This prevents the port collisions with already open ports. If you do want the service's ports created and mapped to the host, specify the --service-ports flag:
$ docker-compose run --service-ports web python manage.py shell
因此,除非您有这些特定需求(覆盖命令或 运行在不同端口上只使用一个容器),否则 docker-compose up
(即使对于一个容器)就足够了。
如 by questionto42 中所述,您这样做是为了测试:
Do not forget to add --rm
after the run
, else you create containers each time you run docker-compose run ...
, that do not get removed after exit.
This can lead to a long list of containers when you are not aware of this and test around for a while.
Can you help explain why or when you would not want the ports to be created? That is why or when they might conflict with already open ports
因为 docker-compose run
是为您的服务制定的 运行 一次性命令。
这意味着,如果您已经执行了 docker-compose up
,您的所有容器都已经 运行 从 docker-compose.yml
.
连接到它们的指定端口
在此阶段执行 docker-compose run
(执行一次性命令),如果它涉及同一端口,将立即失败。因此默认不创建这些端口。
另一个用例(在 Compose environment variables reference 中):
To see what environment variables are available to a service, run docker-compose run SERVICE env
.
这些命令的最新 (2019+) 版本在 docker/docker.github.io
:
我想指出,如果您将 Python 与 pdb 调试器一起使用:
import pdb; pdb.set_trace()
如果您使用以下方式执行脚本,它不会掉落到 shell:
docker-compose up
但是,如果您使用 运行,它将按预期下降到调试器:
docker-compose run
有来自 docker docs 的回答。
Typically, you want docker-compose up
. Use up
to start or restart all
the services defined in a docker-compose.yml
. In the default
“attached” mode, you see all the logs from all the containers. In
“detached” mode (-d
), Compose exits after starting the containers, but
the containers continue to run in the background.
The docker-compose run
command is for running “one-off” or “adhoc”
tasks. It requires the service name you want to run and only starts
containers for services that the running service depends on. Use run
to run tests or perform an administrative task such as removing or
adding data to a data volume container. The run
command acts like
docker run -ti
in that it opens an interactive terminal to the
container and returns an exit status matching the exit status of the
process in the container.
是否有理由使用 run
来启动 docker-compose.yml
文件,还是应该只使用 up
?
我知道run
可以启动一个特定的容器,但我指的是你在没有指定容器的情况下使用它,这样它就启动了你所有的yml容器。
如 docker-compose run
(2015) 所述
The command passed by run overrides the command defined in the service configuration.
For example, if the web service configuration is started withbash
, thendocker-compose run web python app.py
overrides it withpython app.py
.
The second difference is the
docker-compose run
command does not create any of the ports specified in the service configuration.
This prevents the port collisions with already open ports. If you do want the service's ports created and mapped to the host, specify the --service-ports flag:
$ docker-compose run --service-ports web python manage.py shell
因此,除非您有这些特定需求(覆盖命令或 运行在不同端口上只使用一个容器),否则 docker-compose up
(即使对于一个容器)就足够了。
如
Do not forget to add
--rm
after therun
, else you create containers each time you rundocker-compose run ...
, that do not get removed after exit.
This can lead to a long list of containers when you are not aware of this and test around for a while.
Can you help explain why or when you would not want the ports to be created? That is why or when they might conflict with already open ports
因为 docker-compose run
是为您的服务制定的 运行 一次性命令。
这意味着,如果您已经执行了 docker-compose up
,您的所有容器都已经 运行 从 docker-compose.yml
.
连接到它们的指定端口
在此阶段执行 docker-compose run
(执行一次性命令),如果它涉及同一端口,将立即失败。因此默认不创建这些端口。
另一个用例(在 Compose environment variables reference 中):
To see what environment variables are available to a service, run
docker-compose run SERVICE env
.
这些命令的最新 (2019+) 版本在 docker/docker.github.io
:
我想指出,如果您将 Python 与 pdb 调试器一起使用:
import pdb; pdb.set_trace()
如果您使用以下方式执行脚本,它不会掉落到 shell:
docker-compose up
但是,如果您使用 运行,它将按预期下降到调试器:
docker-compose run
有来自 docker docs 的回答。
Typically, you want
docker-compose up
. Useup
to start or restart all the services defined in adocker-compose.yml
. In the default “attached” mode, you see all the logs from all the containers. In “detached” mode (-d
), Compose exits after starting the containers, but the containers continue to run in the background.The
docker-compose run
command is for running “one-off” or “adhoc” tasks. It requires the service name you want to run and only starts containers for services that the running service depends on. Userun
to run tests or perform an administrative task such as removing or adding data to a data volume container. Therun
command acts likedocker run -ti
in that it opens an interactive terminal to the container and returns an exit status matching the exit status of the process in the container.