Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose volume not mounting correctly

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",
        ...
like image 393
Jonnyishman Avatar asked Aug 07 '17 09:08

Jonnyishman


2 Answers

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

like image 120
Pulasthi Bandara Avatar answered Sep 22 '22 23:09

Pulasthi Bandara


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

like image 45
Michael Avatar answered Sep 24 '22 23:09

Michael