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",
...
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With