我应该使用docker-compose启动还是运行? [英] Should I use docker-compose start up or run?
问题描述
docker-compose.yml
文件,或者你应该使用 up
? 据我所知,运行
可以启动特定的容器,但我指的情况下你使用它但不指定一个容器,这样它启动所有阳明海运集装箱。
作为的 docker-compose run
:
运行传递的命令将覆盖服务配置中定义的命令。
对于例如,如果Web服务配置以bash
开始,那么docker-compose run web python app.py
覆盖它与python app.py
。
第二个区别是
docker-compose run
命令的不ç reate任何在服务配置强>指定的端口。结果,
这防止了端口冲突与已经打开的端口。如果您希望创建服务的端口并映射到主机,请指定--service-ports标志:
$ docker-compose run --service-ports web python manage.py shell
所以除非你有这些特定的需求(覆盖一个命令或在不同的端口上只运行一个容器),否则 docker-compise up
(即使是一个容器)就足够了。 >
你能帮助解释为什么或当你不希望将要创建的端口?这就是为什么或什么时候可能与已经开放的端口冲突
只是因为 docker-compose run
是为您的服务运行一次性命令。
这意味着,如果您已经执行了一个 docker-compose up
您的所有容器已经从 docker-compose.yml
在其指定的端口上运行。
执行 docker-compose run
在此阶段(以执行一断开指令),如果它是关于相同的端口,将立即失效。因此,这些端口的默认非创建。
另一个用例(在编写环境变量参考):
要查看服务可用的环境变量,请运行
docker-compose run SERVICE env
。
Is there a reason to use run to start up a docker-compose.yml
file or should you just use up
?
I understand that run
can start up a specific container, but I am referring to the case where you use it with out specifying a container so that it starts up all of your yml containers.
As mentioned in docker-compose run
:
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
So unless you have those specific needs (overriding a command or running only one container on different ports), docker-compose up
(even for one container) is enough.
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
Simply because docker-compose run
is made to run one-off commands for your services.
That means that, if you already did a docker-compose up
, all your containers are already running on their specified ports from docker-compose.yml
.
Doing a docker-compose run
at this stage (to execute a one-off command), if it was respecting the same port, would fail immediately. Hence the default non-creation of those ports.
Another use case (in Compose environment variables reference):
To see what environment variables are available to a service, run
docker-compose run SERVICE env
.
这篇关于我应该使用docker-compose启动还是运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!