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.

4

2023: As noted by Michael Bolli in the comments, that feature is now a work-in-progress:

PR 45687: “volumes: Implement subpath mount”

Make it possible to mount subdirectory of a named volume.

Q1 2024: this is merged! Commit 31ccdbb.
Possibly for Moby 26.0.

March 2024, as mentioned in issue 32582:

CLI already supports it (starting from v26.0.0-rc1): docker/cli PR #4331

(and you can already install it from the test channel)

Compose support is still WIP (it needs to move to v26 first).

March 2024: Paweł Gronowski confirms in the same issue:

moby v26.0.0 is released, so you can already try out this feature.


2016: 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.

5

It is not possible.
You can just create normal sub directories into the volume and then in your application link or configure to use that path.

Let’s say you use this compose volume definition :

    volumes:
      - myVolume:/mnt/data
volumes:
  myVolume:

The volume setup is irrelevant, but only the mounpoint matters, assuing the mount point inside container is “/mnt/data/”

Then in you application just point to sub directories:
logging to /mnt/data/log,
database files to /mnt/data/db
configs to /mnt/config

For example doing this in DockeFile

RUN mkdir -p /mnt/data/log
RUN mkdir /mnt/data/db
RUN mkdir /mnt/data/config
RUN sed ... to change logging path to /mnt/data/log
RUN mv /current/db/data/files /mnt/data/db
RUN ln /mnt/data/db /current/db/data/files 
... 

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật