简体   繁体   中英

Docker run application in a volume

In my django docker app i would to use a volume for manage my application file here is my Dockerfile:

FROM python:3.6-alpine
EXPOSE 8000
RUN apk update
RUN apk add --no-cache make linux-headers libffi-dev jpeg-dev zlib-dev
RUN apk add postgresql-dev gcc python3-dev musl-dev

VOLUME /var/lib/cathstudio/data
WORKDIR /var/lib/cathstudio/data

COPY ./requirements.txt .

RUN pip install --upgrade pip
RUN pip install -t /var/lib/cathstudio/data -r requirements.txt
ENV PYTHONUNBUFFERED 1
ENV PYTHONPATH /var/lib/cathstudio/data

COPY . /var/lib/cathstudio/data

ENTRYPOINT python /var/lib/cathstudio/data/manage.py runserver 0.0.0.0:8000

but when i run my app:

docker run -d -it --rm --link postgres:postgres --name=cathstudio myrepo/app_studio:latest

i get

python: can't open file '/var/lib/cathstudio/data/manage.py': [Errno 2] No such file or directory

the same also if i in my Dockerfile write just ENTRYPOINT python manage.py runserver 0.0.0.0:8000

where is my file wrong? how can i run my app using a volume for storing app files?

So many thanks in advance

Can you try to use the -v flag rather than using VOLUME in dockerfile, im not sure why but VOLUME creates the exact empty volume rather than mounting along with all of the data:

remove VOLUME section from docker file and try the following

docker run -d -it --rm -v /var/lib/cathstudio/data/:/var/lib/cathstudio/data --link postgres:postgres --name=cathstudio myrepo/app_studio:latest

Volumes are not intended to hold code. Especially given your comment to @i4nk1t's answer ("I want to distribute my Docker image"), the code should be in the image itself , not in a volume.

You should just delete the VOLUME line.

Declaring VOLUME in a Dockerfile has some key side effects. One of them is that it prevents any RUN command on that directory from having any future effect, which is keeping your application from working.

I tend to recommend a workflow where you make sure your application works locally and then package it in Docker. If you can't use your host Python and really have to do live development in the container environment, you can use the docker run -v option (or equivalent) to mount host content on to any directory in any container, regardless of whether or not it was declared as a VOLUME .

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