12

I am following the flask/docker tutorial on testdriven.io. Within Part One on the third section Docker Config I am following the instructions in order to mount the project directory as a volume within the docker container for development. Unfortunately following the instructions in the tutorial does not mount the volume correctly with docker-compose. Instead the directory inside the container is empty.

The following is the Dockerfile for the container in question.

FROM python:3.6.1

# set working directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# add requirements (to leverage Docker cache)
ADD ./requirements.txt /usr/src/app/requirements.txt

# install requirements
RUN pip install -r requirements.txt

# add app
ADD . /usr/src/app

# run server
CMD python manage.py runserver -h 0.0.0.0

And this is the docker-compose.yml file

version: '2.1'

services:

  users-service:
    container_name: users-service
    build: .
    volumes:
      - '.:/usr/src/app'
    ports:
      - 5001:5000 # expose ports - HOST:CONTAINER

The directory in question is the /usr/src/app directory inside the users-service container. When I do:

>> docker-compose build 
>> docker-compose up -d
>> docker-compose run users-service bash

When I ls inside the container I am in the correct directory (/usr/src/app) but the directory is empty. This also causes the CMD to fail as it can't find the manage.py file which is in the root of the project directory.

I've seen other posts on stack-overflow with similar titles, unfortunately none of the solutions have been able to solve my problem. I've tried changing the local volume path from relative to absolute with no difference in result.Thank you in advance for anybody who is able to help a fellow out.

EDIT:

Having run docker inspect on the container, below is the information found under Mounts and Config - Volume:

"Mounts": [
            {
                "Type": "bind",
                "Source": "/home/jonathan/projects/flask-microservices-users",
                "Destination": "/usr/src/app",
                "Mode": "rw",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
 ...
 "Config": {
        ...
        "Image": "flaskmicroservicesusers_users-service",
        "Volumes": {
            "/usr/src/app": {}
        },
        "WorkingDir": "/usr/src/app",
        ...
Jonnyishman
  • 365
  • 3
  • 4
  • 12
  • what's in the directory you're mounting? `.` is it not empty? – user3012759 Aug 07 '17 at 15:45
  • 1
    The directory I'm mounting is the root directory for the project. `me@ubuntu:~/project$ ls Dockerfile manage.py README.md docker-compose.yml project requirements.txt` – Jonnyishman Aug 08 '17 at 10:12
  • can you do docker inspect on your user-service container and check what volumes it has? – user3012759 Aug 08 '17 at 10:14
  • Unfortunately the container doesn't stay running. Without the volume being mounted properly, the manage.py file and project directory are missing inside the container so it does not persist when run. – Jonnyishman Aug 08 '17 at 20:22
  • but you can still run inspect on stopped container – user3012759 Aug 09 '17 at 08:44
  • Right you are. I have edited my original post with the results of a `docker inspect` – Jonnyishman Aug 09 '17 at 19:24
  • Oh, so you sure you don't have typos in your paths? you gave ls from `~/project` yet the inspect has `/home/jonathan/projects/` there is an __s__ there, docker will readily create the dirs on your host if they are missing... – user3012759 Aug 10 '17 at 09:28
  • I was going for brevity in my comment above. The actual path I used was: `jonathan@ubuntu:~/projects/flask-microservices-users$ ls` – Jonnyishman Aug 10 '17 at 10:05
  • do you have selinux enabled? I think this may give you some trouble... – user3012759 Aug 10 '17 at 10:33
  • another problem may be your rprivate propagation, have you got any funny mount arrangement in your home dir? – user3012759 Aug 10 '17 at 10:40
  • I've checked selinux, it's disabled. I'm running off a fresh install of ubuntu 16.04 LTS so there shouldn't be any mount arrangements going on. – Jonnyishman Aug 10 '17 at 19:14
  • Hmm this is very peculiar, never had problems like that with volumes, the last thing that springs to mind is overriding volumes, but maybe try to move your stuff to a different place or mount additional path from your host to container and check that out - then you know that there is not a general issue getting in a way. Strip it down imho as it's hard to debug for the obvious 'aha' moments ;) (also absolute > relative imho) – user3012759 Aug 15 '17 at 14:23

2 Answers2

4

The issue is that you can't mount a volume to an existing path in the container and expect the data within that path to be syncd. This is not how unix volume mounts work.

An explanation can be found here:

https://github.com/moby/moby/issues/32203

Have a look here if you need to make data inside a container persistent:

https://github.com/moby/moby/issues/4361#issuecomment-36317097

Pulasthi Bandara
  • 526
  • 4
  • 16
1

Remove the following from your Dockerfile:

# add app
ADD . /usr/src/app

Review the answer here for more info:

https://stackoverflow.com/a/27753725/1799408

Michael
  • 1,177
  • 4
  • 20
  • 71
  • 1
    Hi Michael, thank you for your advice, unfortunately it hasn't solved the problem. Even after clearing the docker cache and starting from scratch. I've edited my original comment to include the results of a `docker compose` on the container. – Jonnyishman Aug 09 '17 at 19:25