Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we mount sub-directories of a named volume in docker?

The docker-compose file https://docs.docker.com/compose/compose-file/#/volumes-volume-driver shows various ways to mount host sub-directories relative to the compose file.

For example:

volumes:    # Just specify a path and let the Engine create a volume    - /var/lib/mysql      # Specify an absolute path mapping    - /opt/data:/var/lib/mysql      # Path on the host, relative to the Compose file    - ./cache:/tmp/cache      # User-relative path    - ~/configs:/etc/configs/:ro      # Named volume    - datavolume:/var/lib/mysql 

Is is possible to mount a sub-directory of a named volume at a specific location? For example something like below, which I tried, but does not seem to work.

# Named volume   - datavolume/sql_data:/var/lib/mysql 

I am assuming I might be able to manage this by mounting the data volume to a location like /data and then in the Dockerfiles for each container, create symbolic links from the sub-directories to the locations.

for example in a docker-compose.yml file

volumes:   - datavolume:/data 

and then in the container Dockerfile

RUN ln -s /data/sql_data /var/lib/mysql 

I started going down this path but it was getting messy and was not working. Before I either abandon that approach or invest the time debugging it, I wanted to make sure there was no way to just specify sub-directories of a named vollume.

like image 709
nPn Avatar asked Jul 02 '16 22:07

nPn


People also ask

What are a different kind of volume mount types available in Docker?

Basically, there are 3 types of mounts which you can use in your Docker container viz. Volumes, Bind mount and tmpfs mounts.

Can a Docker container have multiple volumes?

Overview. Docker has multiple options to persist and share data for a running container. However, we may need more than one file storage for a running container, for example, to create backups or grant different access. Or for the same container, we may need to add named volumes and bind them to specific paths.

Where are Docker named volumes stored?

Volumes are stored in a part of the host filesystem which is managed by Docker ( /var/lib/docker/volumes/ on Linux).


1 Answers

No because compose/config/config.py#load(config_details) check if datavolume/sql_data matches a named volume (in compose/config/validation.py#match_named_volumes())

datavolume would, datavolume/sql_data would not.

As memetech points out in the comments, the is an issue tracking this since April 2017:
moby/moby issue 32582: "[feature] Allow mounting sub-directories of named volumes".

In that issue, Joohansson adds (see comment)

In the meantime, I use this workaround to mount the whole volume on a separate path and then symlink it to the sub path.

# In the Dockerfile: RUN mkdir -p /data/subdir RUN ln -s /data/subdir /var/www/subdir 

Then mount the volume as normal.
The /subdir must exist in the volume.

docker run -d -v myvol:/data mycontainer 

Now anything read or written by the webserver will be stored in the volume subdir and can't access the other data.

like image 109
VonC Avatar answered Oct 11 '22 15:10

VonC