dockerdocker-compose

Docker containers can't see files from other containers


I want my client and server (and more later) containers to share an image folder

I have my docker compose like this:

  client:
    build:
      context: ./admin/client
      args:
        NODE_VERSION: ${NODE_VERSION}
    restart: on-failure
    stdin_open: true
    volumes:
      - ./admin/client:/usr/src/app/admin/client
      - ./admin/client/public/images:/usr/src/app/images # This should work

  server:
    build:
      context: ./admin/server
      args:
        NODE_VERSION: ${NODE_VERSION}
    restart: on-failure
    stdin_open: true
    volumes:
      - ./admin/server:/usr/src/app/admin/server
      - ./admin/server/upload/images:/usr/src/app/images # This should work

(There's no network on the others containers so they all should be in the default one right?)

If I dive in the containers with exec bash, I can see that there's an images folder but it's separated per container, server only see what I've put in server/upload/images and client only see what I've put in client/public/images despite them sharing the same bind mount

Here are the dockerfile if needed:

For client container

ARG NODE_VERSION

FROM node:${NODE_VERSION}

WORKDIR /usr/src/app/admin/client

COPY package*.json ./

RUN npm install

CMD ["npm", "run", "dev"]

For server container

ARG NODE_VERSION

FROM node:${NODE_VERSION}

WORKDIR /usr/src/app/admin/server

COPY package*.json ./
COPY server.js ./

RUN npm install

CMD ["npm", "run", "dev"]

Solution

  • You currently have two different host folders being mounted to the same internal container path.

    Client

    volumes:     - ./admin/client/public/images:/usr/src/app/images   # Host path A  
    

    Server

    volumes:     - ./admin/server/upload/images:/usr/src/app/images   # Host path B 
    

    Both containers see /usr/src/app/images, but each of them are seeing a different directory on your host system:

    Client sees: ./admin/client/public/images

    Server sees: ./admin/server/upload/images

    So they do not share files, because the host directories are different.

    In order for them to see the same folder, you should do something like this:

    client:
      volumes:
        - ./shared/images:/usr/src/app/images
    
    server:
      volumes:
        - ./shared/images:/usr/src/app/images
    

    both containers are now connected to the same host folder, and will see the same files under /usr/src/app/images.