[英]Want to run python script and Flask api from single Dockerfile
I am trying to create container with following docker file.我正在尝试使用以下 docker 文件创建容器。 Since it is going to execute last CMD, it ignores
CMD ["sh", "-c", "python3 run_scheduler.py --config=${config}"]
.由于它将执行最后一个 CMD,它忽略
CMD ["sh", "-c", "python3 run_scheduler.py --config=${config}"]
。 Is there a way to run both from single docker file.有没有办法从单个 docker 文件运行两者。 Also, is it possible to pass
${config}
argument to flask api in CMD ["gunicorn", "--bind", "0.0.0.0:5000", "run_api:application", "&"]
Also, is it possible to pass
${config}
argument to flask api in CMD ["gunicorn", "--bind", "0.0.0.0:5000", "run_api:application", "&"]
FROM python:3.8
LABEL version="0.1"
WORKDIR /app
COPY . /app
ARG config=dev
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["sh", "-c", "python3 run_scheduler.py --config=${config}"]
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "run_api:application", "&"]
As mentioned in comments you can pass 2-line bash script in CMD.如评论中所述,您可以在 CMD 中传递 2 行 bash 脚本。 This can cause problems however.
然而,这可能会导致问题。 Docker is not really meant to run multiple processes in single container.
Docker 并不是真的要在单个容器中运行多个进程。 I already see "&" argument in your second CMD.
我已经在您的第二个 CMD 中看到了“&”参数。 I guess you had problems with second process wasn't running so you tried to push it to the background.
我猜你遇到了第二个进程没有运行的问题,所以你试图将它推到后台。
The solution recommended by Docker is to run supervisord
inside the container: https://docs.docker.com/config/containers/multi-service_container/ Docker推荐的解决方案是在容器内运行
supervisord
: https://docs.docker.com/config/containers/multi-service_container/
From naming concepts I guess you can solve your problem with Flask-APScheduler https://github.com/viniciuschiele/flask-apscheduler This way you can get rid of second sidecar script.从命名概念我想你可以用 Flask-APScheduler https://github.com/viniciuschiele/flask-apscheduler解决你的问题这样你就可以摆脱第二个边车脚本。
And lastly you asked:最后你问:
Also, is it possible to pass ${config} argument to flask api
另外,是否可以将 ${config} 参数传递给 flask api
and answer is yes.答案是肯定的。 Declare
ENV
in your Dockerfile like so:在 Dockerfile 中声明
ENV
,如下所示:
FROM python:3.8
...
...
ENV config foo
...
CMD ["whatever_command", "--option", "${config}"]
This way "${config}"
will be replaced with value foo
.这样
"${config}"
将被替换为值foo
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.