简体   繁体   English

启动时如何在 docker 容器上运行多个命令?

[英]How to run multiple commands on docker container on startup?

I have Flask application container which has to run on port 5000 .我有Flask应用程序容器,它必须在端口5000上运行。

I also have a background process which is related to queue.我还有一个与队列相关的后台进程。 This process has an infinite loop which is why I want to run it in background.这个过程有一个无限循环,这就是我想在后台运行它的原因。

I'm using docker-compose.yml file for creating container.我正在使用 docker-compose.yml 文件来创建容器。 For now, I'm able to run only one command which is to make the flask server up.目前,我只能运行一个命令来启动 flask 服务器。

flask run --host=0.0.0.0 --port 5000 . flask run --host=0.0.0.0 --port 5000 I'm using the command option in docker-compose.我正在使用 docker-compose 中的command选项。

This is the command I want to run in background python app/workload/services/queue_services.py > qlog.txt .这是我想在后台运行的命令python app/workload/services/queue_services.py > qlog.txt

When I put the background command first in command , the server is not coming up and I'm only seeing the output from background script.当我将后台命令放在command中时,服务器没有启动,我只看到后台脚本中的 output 。

When I put the flask run command first, the background script is not starting at all.当我先放flask run命令时,后台脚本根本没有启动。

Is there any proper way to run these two commands on container start up?是否有任何正确的方法可以在容器启动时运行这两个命令?

create a script.sh创建一个script.sh

#!/bin/bash
python app/workload/services/queue_services.py > qlog.txt &
flask run --host=0.0.0.0 --port 5000

and set it as your command并将其设置为您的命令

Try Adding尝试添加

CMD ["gunicorn", "--bind=0.0.0.0:5000", "--workers=1", "wsgi"]

in DockerFile在 DockerFile

Another approach could be to use a program like supervisord.另一种方法可能是使用像 supervisord 这样的程序。 This way, we can scale it to multiple services without ever modifying the Dockerfile这样,我们可以将其扩展到多个服务,而无需修改 Dockerfile

https://docs.docker.com/config/containers/multi-service_container/ https://docs.docker.com/config/containers/multi-service_container/

I have compiled an image with supervisord and jre.我用 supervisord 和 jre 编译了一个图像。 https://hub.docker.com/r/palashgoel/supervisord-jre https://hub.docker.com/r/palashgoel/supervisord-jre

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM