Docker Compose / Flask / Volumes

Hello everyone,

I try to run a Flask application inside a docker container. Everything works fine until I try to add a volume.
My docker-compose.yml

auth-service:
build: .
container_name: auth-service
working_dir: /usr/local/app/
command: python run.py
#command: [ "uwsgi", "--ini", "uwsgi/uwsgi.ini:auth-service" ]
networks:
  - micronet
ports: [ "5000" ]
volumes:
  - .:/usr/local/app/
environment:
  - SERVICE_NAME=auth-service
  - SERVICE_TAGS=microservice
  # - SERVICE_CHECK_HTTP=/healthcheck seems to break nginx
  # - SERVICE_CHECK_INTERVAL=15s
  - CONSUL_AGENT_ADDR=192.168.99.102
  - CONSUL_AGENT_PORT=8500

My Dockerfile:

FROM python:3.6 
WORKDIR  /usr/local/app/
ADD . /usr/local/app/
RUN apt-get update && apt-get -y install build-essential && pip install -r requirements.txt
 ENV PYTHONPATH=/usr/local/

after docker-compose up --build, I always get the error ‘python: can’t open file ‘run.py’: [Errno 2] No such file or directory’. If I remove the volumes, everything works as expected.
Any help is truly appreciated.
~ Pascal

It looks like the volume you’re trying to mount is the same directory that gets built into the container. My generic advice here is:

(1) Get a good developer environment set up on your host system
(2) Make sure all of your unit tests pass using pytest or whatever other framework you’re using
(3) Try to run your application locally
(4) Then run docker build and build/deploy your application for real; COPY it in a docker build step, don’t try to include it as a volume

That seems like a pretty clear error message. If you launch your container to a shell (IIRC, docker-compose run auth-service bash ) do you see the right directory contents? Does the very first line of run.py point at a directory that’s on your host but not in the container (#!/home/me/virtualenvs/auth/bin/python or something like that)?

Hi David,

Thanks for your response. Unfortunately I’m not quite understand your steps. What I try to achieve is to have this running locally. Auth service is part of a micro service. So there are other services as well, each one in his own machine and docker container. The all communicate with each other and have a nginx in front of it.
What I try to do is to create a local dev environment so that the flask server automatically restarts after a code change is detected. So it was my understanding that you have to mount the same folder.

After running ‘docker-compose run auth-service bash’ I can see the directory that run.py points too. However, I need this mounted to my host machine so I code changes are detected.
Hope this makes sense (: