简体   繁体   中英

Flask can't find .env file when run from docker

I'm running a flask web server from inside a docker container, and using an environment file to set the FLASK_APP variable. Whenever I try to start the server, I get an error saying FLASK_APP wasn't set correctly. I think this has to do with the commands in my Dockerfile, because when I copy the environment directory to the container root (ie COPY .env.example /.env ) instead of to the working directory, everything works fine. But that doesn't seem right. I'm running the server with docker-compose up flask-dev .

Directory structure:

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

My 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:

Thanks so much for any help!

You should be passing the .env file to your flask-dev definition section on your docker-compose.yml via env_file instruction.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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