Home > Enterprise >  How to share build app directory with nginx directory path with docker compose
How to share build app directory with nginx directory path with docker compose

Time:10-15

I have a react app, which creates a build folder as path /app/admin/build and I want this directory to share as volume to the other nginx container that is running as mentioned in my docker-compose.yaml below. Am I missing anything? I want to share the app build directory to nginx. However, /var/www/admin/build is getting created when checked login into the container but its empty. Can anyone help with this?

Error: *1 directory index of "/app/admin/build/" is forbidden, client: 172.18.x.x, server: _, request: "GET / HTTP/1.1", host: "localhost"

Thanks in advance

version: '3'
services:
  admin:
    build:
      dockerfile: Dockerfile
      context: ./admin
    volumes:
      - /app/admin/build
  web:
    restart: always
    build:
      dockerfile: Dockerfile
      context: ./nginx
    ports:
      - '80:3000'
    volumes:
      - /app/admin/build:/var/www/admin/build

CodePudding user response:

I'd include the files in your custom image for the Nginx component. The COPY --from=... option is typically used with a multi-stage build, but you can also name a preexisting image:

FROM nginx
...
COPY --from=project_admin /app/admin/build /var/www/admin/build

Check with docker images to see the name Compose assigns to the built admin image, or put image: in the Dockerfile to set it yourself.

Then in the Compose setup, you don't need volumes: at all:

version: '3.8'
services:
  admin:
    build: ./admin
  web:
    restart: always
    build: ./nginx
    ports:
      - '80:3000'

The one particular complication here is that Compose doesn't know that one image can depend on another, so you may need to manually docker-compose build admin before starting the rest of the application docker-compose up --build.

In general, exporting files from an image is tricky. The syntax you show replaces the admin container's build directory with an anonymous volume, then mounts an unrelated host directory into the web container. There's a different syntax that involves Docker named volumes, but it doesn't work in other environments (like Kubernetes) or with host directories, and it ignores updates in the application. COPYing the files into the image will be the most reliable approach; redesigning your application to avoid that (maybe letting the backend serve its own static files) is even better.

  • Related