我应该使用 docker-compose up 还是 run? [英] Should I use docker-compose up or run?

查看:88
本文介绍了我应该使用 docker-compose up 还是 run?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有理由使用 run 来启动 docker-compose.yml 文件,还是应该使用 up ?>

我知道 run 可以启动一个特定的容器,但我指的是你在不指定容器的情况下使用它以便它启动你所有的 yml 容器的情况.

解决方案

docker-compose run (2015)

<块引用>

run 传递的命令会覆盖服务配置中定义的命令.
例如,如果 web 服务配置以 bash 启动,则 docker-compose run web python app.pypython app.py 覆盖它>.

第二个区别是 docker-compose run 命令不会创建服务配置中指定的任何端口.
这可以防止端口与已打开的端口发生冲突.如果您确实希望创建服务的端口并将其映射到主机,请指定 --service-ports 标志:

$ docker-compose run --service-ports web python manage.py shell

所以除非你有那些特定的需求(覆盖一个命令或只在不同端口上运行一个容器),docker-compose up(即使是一个容器)就足够了.

<块引用>

您能帮助解释为什么或何时不希望创建端口吗?这就是为什么或何时它们可能与已经打开的端口发生冲突

仅仅是因为 docker-compose run 用于为您的服务运行一次性命令.
这意味着,如果您已经执行了 docker-compose up,那么您的所有容器都已经在 docker-compose.yml 的指定端口上运行.
在这个阶段执行 docker-compose run (执行一次性命令),如果它尊重同一个端口,将立即失败.因此默认不创建这些端口.

<小时>

另一个用例(在撰写环境变量参考):

<块引用>

要查看服务可用的环境变量,请运行 docker-compose run SERVICE env.

<小时>

这些命令的最新 (2019+) 版本位于 docker/docker.github.io:

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 without specifying a container so that it starts up all of your yml containers.

解决方案

As mentioned in 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

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.


Most recent (2019+) version of those commands are on docker/docker.github.io:

这篇关于我应该使用 docker-compose up 还是 run?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆