Xen_mar
Xen_mar

Reputation: 9742

Bind Mount & Volumes: Files do not end up in the container?

I am sweating over a weird problem. I am mounting a folder from the host machine to the container (only for development). The folder is called dj_docker and has a folder dj_docker/media with some .pdf files that need to end up in the container and must be accessible for other containers via the volume media_volume. However, as soon as I map the subdirectory /media to the volume (the bind mount as well as the volume now include the folder /media) all the contents of the folder disappear in the container. The folder is empty.

Simplified docker-compose.yml:

version: '3.7'

services:
  restapi:
    volumes: 
      - media_volume:/usr/src/dj/media
      - ./dj_docker:/usr/src/dj/
volumes:
  static_volume:
  media_volume:

If I omit the volume in the docker-compose file, the .pdf files are visible and present in the container:

    volumes: 
      - ./dj_docker:/usr/src/dj/

Is there a way to still use a bind mount as well as the volume with me keeping the files in the container?

Dockerfile

# pull official base image
FROM python:3.7-alpine

# set environment varibles
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# set work directory
WORKDIR /usr/src/dj

# install psycopg2
RUN apk update \
    && apk add --virtual build-deps gcc python3-dev musl-dev \
    && apk add postgresql-dev \
    && pip install psycopg2 \
    && apk del build-deps

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/dj/requirements.txt
RUN pip install -r requirements.txt
VOLUME /usr/src/dj/media

# copy entrypoint.sh
COPY ./entrypoint.sh /usr/src/dj/entrypoint.sh

# run entrypoint.sh
ENTRYPOINT ["/usr/src/dj/entrypoint.sh"]

Upvotes: 0

Views: 4589

Answers (2)

medik
medik

Reputation: 1214

You can have both host and named volumes, but there's a trick to make it work. This article should solve your problem: https://jdlm.info/articles/2019/09/06/lessons-building-node-app-docker.html.

Upvotes: -1

David Maze
David Maze

Reputation: 159781

Is there a way to still use a bind mount as well as the volume with me keeping the files in the container?

No, this isn't possible.

This is the same as ordinary Un*x mount(8) (and in fact if you poke around with low-level tools you'll see filesystem mounts in a Docker setup). A volume mount completely hides everything that was in the directory before. If you have layered mounts like this, the Docker sorts them before applying any, and the innermost directory mount hides the contents in that directory in the next outermost mount.

The easiest thing to change here is probably the container-side directory holding this data. I might pick a root directory path like /media, outside your application's install tree. Set the location through an environment variable (it is different in your non-Docker development environment). Copy the seed content there on startup if it doesn't exist (it will not be hidden by a volume mount now).

Upvotes: 4

Related Questions