从 docker 运行时,Flask 找不到 .env 文件



我从docker容器内部运行flask web服务器,并使用环境文件设置FLASK_APP变量。每当我尝试启动服务器时,我都会得到一个错误,说FLASK_APP设置不正确。我认为这与我的Dockerfile中的命令有关,因为当我将环境目录复制到容器根目录(即COPY .env.example /.env)而不是工作目录时,一切都很好。但这似乎不对。我正在用docker-compose up flask-dev运行服务器。

目录结构:

.
├── Dockerfile
├── Pipfile
├── Pipfile.lock
├── application
│   ├── __init__.py
│   └── app.py
├── autoapp.py
├── dev.db
└── docker-compose.yml
我Dockerfile

:

ARG INSTALL_PYTHON_VERSION=${INSTALL_PYTHON_VERSION:-PYTHON_VERSION_NOT_SET}
# ================================= PRODUCTION =================================
FROM python:${INSTALL_PYTHON_VERSION}-slim-buster as production
WORKDIR /app
RUN useradd -m sid
RUN chown -R sid:sid /app
USER sid
ENV PATH="/home/sid/.local/bin:${PATH}"
COPY ["Pipfile", "shell_scripts/auto_pipenv.sh", "./"]
RUN pip install --no-cache pipenv
RUN pipenv install
COPY supervisord.conf /etc/supervisor/supervisord.conf
COPY supervisord_programs /etc/supervisor/conf.d
COPY . .
EXPOSE 5000
ENTRYPOINT ["/bin/bash", "shell_scripts/supervisord_entrypoint.sh"]
CMD ["-c", "/etc/supervisor/supervisord.conf"]

# ================================= DEVELOPMENT ================================
FROM python:${INSTALL_PYTHON_VERSION}-slim-buster AS development
WORKDIR /app
COPY ["Pipfile", "shell_scripts/auto_pipenv.sh", "./"]
RUN pip install --no-cache pipenv
COPY autoapp.py autoapp.py
COPY application application
COPY .env.example .env
RUN pipenv install --dev
EXPOSE 2992
EXPOSE 5000
CMD [ "pipenv", "run", "flask", "run" ]

docker-compose:

version: "3.6"
x-build-args: &build_args
INSTALL_PYTHON_VERSION: 3.8
x-default-volumes: &default_volumes
volumes:
- ./:/app
- ./dev.db:/tmp/dev.db
services:
flask-dev:
build:
context: .
target: development
args:
<<: *build_args
image: "application-development"
ports:
- "5000:5000"
- "2992:2992"
<<: *default_volumes
flask-prod:
build:
context: .
target: production
args:
<<: *build_args
image: "application-production"
ports:
- "5000:5000"
environment:
FLASK_ENV: production
FLASK_DEBUG: 0
LOG_LEVEL: info
GUNICORN_WORKERS: 4
<<: *default_volumes
manage:
build:
context: .
target: development
args:
<<: *build_args
entrypoint: pipenv run flask
environment:
FLASK_ENV: production
FLASK_DEBUG: 0
image: "application-manage"
stdin_open: true
tty: true
<<: *default_volumes
volumes:
node-modules:

非常感谢你的帮助!

您应该将.env文件传递给docker-compose上的flask-dev定义部分。

flask-dev:
build:
context: .
target: development
args:
<<: *build_args
image: "application-development"
env_file: .env
ports:
- "5000:5000"
- "2992:2992"
<<: *default_volumes

相关内容

  • 没有找到相关文章

最新更新