使用Docker-Compose,如何执行多个命令

本文翻译自:Using Docker-Compose, how to execute multiple commands

I want to do something like this where I can run multiple commands in order. 我想做这样的事情,我可以按顺序运行多个命令。

db:
  image: postgres
web:
  build: .
  command: python manage.py migrate
  command: python manage.py runserver 0.0.0.0:8000
  volumes:
    - .:/code
  ports:
    - "8000:8000"
  links:
    - db

#1楼

参考:https://stackoom.com/question/228zj/使用Docker-Compose-如何执行多个命令


#2楼

Figured it out, use bash -c . 想出来,使用bash -c

Example: 例:

command: bash -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"

Same example in multilines: 多行中的相同示例:

command: >
    bash -c "python manage.py migrate
    && python manage.py runserver 0.0.0.0:8000"

Or: 要么:

command: bash -c "
    python manage.py migrate
    && python manage.py runserver 0.0.0.0:8000
  "

#3楼

Another idea: 另一个想法:

If, as in this case, you build the container just place a startup script in it and run this with command. 如果,就像在这种情况下,您构建容器只需在其中放置一个启动脚本并使用命令运行它。 Or mount the startup script as volume. 或者将启动脚本作为卷安装。


#4楼

I run pre-startup stuff like migrations in a separate ephemeral container, like so (note, compose file has to be of version '2' type): 我在一个单独的短暂容器中运行像启动之类的预启动之类的东西(注意,compose文件必须是版本'2'类型):

db:
  image: postgres
web:
  image: app
  command: python manage.py runserver 0.0.0.0:8000
  volumes:
    - .:/code
  ports:
    - "8000:8000"
  links:
    - db
  depends_on:
    - migration
migration:
  build: .
  image: app
  command: python manage.py migrate
  volumes:
    - .:/code
  links:
    - db
  depends_on:
    - db

This helps things keeping clean and separate. 这有助于保持清洁和分离。 Two things to consider: 需要考虑的两件事:

  1. You have to ensure the correct startup sequence (using depends_on) 您必须确保正确的启动顺序(使用depends_on)

  2. you want to avoid multiple builds which is achieved by tagging it the first time round using build and image; 你想避免多次构建,这是通过使用构建和图像第一次标记它来实现的; you can refer to image in other containers then 你可以参考其他容器中的图像


#5楼

If you need to run more than one daemon process, there's a suggestion in the Docker documentation to use Supervisord in an un-detached mode so all the sub-daemons will output to the stdout. 如果您需要运行多个守护程序进程, Docker文档中建议是在未分离模式下使用Supervisord ,以便所有子守护进程都将输出到stdout。

From another SO question, I discovered you can redirect the child processes output to the stdout. 从另一个SO问题,我发现你可以将子进程输出重定向到stdout。 That way you can see all the output! 这样你就可以看到所有输出!


#6楼

Use a tool such as wait-for-it or dockerize . 使用wait-for-it或dockerize等工具。 These are small wrapper scripts which you can include in your application's image. 这些是小包装脚本,您可以将其包含在应用程序的图像中。 Or write your own wrapper script to perform a more application-specific commands. 或编写自己的包装器脚本以执行更多特定于应用程序的命令。 according to: https://docs.docker.com/compose/startup-order/ 根据: https//docs.docker.com/compose/startup-order/

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值