How to make containers access image contents from one another?

I am trying to inset data files from different images to the named volume so that I can use it in another service for creating database along with seed data. In the below scenario I can see the data-db1 folder created but not data-db2 when I do the compose up. Is this the right way to do or am I missing something here?
The requirement is to have one directory which contains all the different db folders with respective scripts.
Since I can’t mount image as read only volume, I am focusing on this approach.
Appreciate the help if there is a better pattern to this issue.

version:` "3.9"

services:
  data-db1:
    image: data-db1
    build: 
        context: ./multiple-db
        dockerfile: data-db1.Dockerfile
    container_name: data-db1
    volumes:
      - type: volume
        source: data
        target: /data
    command: /usr/bin/tail -f /dev/null

  data-db2:
    image: data-db2
    build: 
        context: ./multiple-db
        dockerfile: data-db2.Dockerfile
    container_name: data-db2
    volumes:
      - type: volume
        source: data
        target: /data
    command: /usr/bin/tail -f /dev/null
    depends_on: 
      - data-db1

volumes:
  data:

data-db1.Dockerfile

FROM alpine AS base
WORKDIR /data

COPY ./data-db1 /data/data-db1

data-db2.Dockerfile

FROM alpine AS base
WORKDIR /data

COPY ./data-db2 /data/data-db2
1 Like

It seems you may not need the shared folder to also be available on the host?

See also How do named volumes work in docker? - Stack Overflow which explains:

  1. If you create another container binds to an existing named volume, no files from the new image/container will be copied/overwritten, it will use the existing data inside the named volume.

In case the volume is empty and both containers have data in the target directory the first container to be run will mount its data into the volume and the other container will see that data (and not its own).

Thanks for the reference answer. I didn’t find this in the documentation. If this approach doesn’t solve my problem what’s the best pattern here.

Now knowing that only the data from the container that started first is copied, I guess it’s documented, as it won’t be empty for the container that starts second:

Tips for using bind mounts or volumes

If you use either bind mounts or volumes, keep the following in mind:

  • If you mount an empty volume into a directory in the container in which files or directories exist, these files or directories are propagated (copied) into the volume. Similarly, if you start a container and specify a volume which does not already exist, an empty volume is created for you. This is a good way to pre-populate data that another container needs.
  • If you mount a bind mount or non-empty volume into a directory in the container in which some files or directories exist, these files or directories are obscured by the mount, just as if you saved files into /mnt on a Linux host and then mounted a USB drive into /mnt . The contents of /mnt would be obscured by the contents of the USB drive until the USB drive were unmounted. The obscured files are not removed or altered, but are not accessible while the bind mount or volume is mounted.
1 Like