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

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

问题描述

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

Is there a reason to use run to start up a docker-compose.yml file or should you just use up?

我知道 run 可以启动特定的容器,但我指的是在不指定容器的情况下使用它的情况,这样它将启动所有yml容器。

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.

推荐答案

中提到docker-compose run (2015)

As mentioned in docker-compose run (2015)


由run传递的命令将覆盖服务配置中定义的命令

例如,如果Web服务配置以 bash 开始,则 docker-compose run web python app.py python app.py 覆盖它。

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.

第二个区别是 docker-compose运行命令不创建服务配置中指定的任何端口

这样可以防止端口与已经打开的端口发生冲突。如果确实要创建服务的端口并将其映射到主机,请指定--service-ports标志:

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 (即使对于一个容器)也足够。

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

简单地是因为 docker-compose运行用于为服务运行一次性命令。

这意味着,如果您已经做了 docker-compose up ,您所有的容器都已经在 docker-compose.yml 的指定端口上运行。

进行 docker-compose run 在此阶段(执行一次性命令),如果它遵循相同的端口,则将立即失败。因此,默认情况下不创建这些端口。

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.

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


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






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

  • docker compose run
  • docker compose up

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

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