首页 > 解决方案 > 启动容器前修改一行

问题描述

我使用以下命令构建了一个 docker 镜像

docker build -t shantanuo/mydash .

dockerfile 是:

FROM continuumio/miniconda3
EXPOSE 8050
RUN cd /tmp/
RUN apt-get update
RUN apt-get install --yes git zip vim
RUN git clone https://github.com/kanishkan91/SuperTrendfor50Stocks.git
RUN pip install -r SuperTrendfor50Stocks/requirements.txt
WORKDIR SuperTrendfor50Stocks/

我可以启动容器,修改应用程序文件,然后启动应用程序。

步骤1:

docker run -p 8050:8050 -it shantanuo/mydash bash

第2步:

vi application.py

更改最后一行

application.run_server(debug=True)

应用程序运行(主机='0.0.0.0')

第 3 步:

python application.py

我可以避免这 3 个步骤并合并我的 dockerfile 中的所有内容吗?

标签: awkseddockerfile

解决方案


我不认为这是更改代码行然后手动运行应用程序的好方法,为什么代码不是自通用的,并根据 ENV 相应地修改应用程序的行为。

你可以试试

# set default value accordingly
app.run(host=os.getenv('HOST', "127.0.0.1") , debug=os.getenv('DEBUG', False))

现在您可以根据 ENV 更改该行为。

web:
  build: ./web
  environment:
    - HOST=0.0.0.0
    - DEBUG=True

或者

docker run -p 8050:8050 -e HOST="0.0.0.0" e DEBUG=True -it shantanuo/mydash

您还需要CMD在 Dockerfile中设置

CMD python app.py

推荐阅读