Docker Compose with Symfony + React on ngnix Webserver doesnt compile assets correctly

Hey.

I am building a hardware booking system for a project on my university. I want to build a single page applikation, running with symfony 5 and reactjs on a ngnix webserver.

Therefore I have seperated symfony and react to two different containers. First, the symfony project is built. Then I want to compile the react assets into the assets directory.

Dockerfile for php:

# Basisimage
FROM php:8.1-fpm

RUN apt-get update && apt-get install -y \
    curl \
    git \
    unzip \
    libicu-dev \
    libonig-dev \
    libzip-dev

# Installiere die PHP-Erweiterungen
RUN docker-php-ext-install \
    pdo_mysql \
    intl \
    zip

WORKDIR /var/www/html

COPY . /var/www/html

# Installiere Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Installiere die Composer-Abhängigkeiten
RUN composer install

# Generiere den Autoloader
RUN composer dump-autoload --optimize

# Setze die Dateiberechtigungen
RUN chown -R www-data:www-data /var/www/html/var

# Exponiere den Container-Port
EXPOSE 9000

# Starte den PHP-FPM-Server
CMD ["php-fpm"]

Dockerfile for the assets:

# Base image
FROM node:14

# Set the working directory inside the container
WORKDIR /var/www/html/assets

COPY . /var/www/html/assets/

RUN yarn global add webpack

RUN yarn install

# Set the command to run for the asset service
CMD ["yarn", "build"]

Docker-Compose:

version: '3.8'
services:
  php:
    build:
      context: .
      dockerfile: Docker/php/Dockerfile
    volumes:
       - shared:/var/www/html/
    networks:
      - app-network

  assets:
    build:
      context: ./assets
      dockerfile: ../Docker/assets/Dockerfile
    volumes:
      - shared:/var/www/html/assets/
    depends_on:
      php:
        condition: service_started
    networks:
      - app-network

  nginx:
    image: nginx:latest
    ports:
      - 80:80
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
    networks:
      - app-network

  db:
    image: mysql:latest
    environment:
      MYSQL_DATABASE: db
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_ROOT_USER: root
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - db-data:/var/lib/mysql
    networks:
      - app-network

volumes:
  db-data:
  shared:

networks:
  app-network:

Build of the assets container is failing:

2023-06-30 15:04:19 yarn run v1.22.19
2023-06-30 15:04:19 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
2023-06-30 15:04:19 error Couldn't find a package.json file in "/var/www/html/assets"

The whole project can be found here:

You mounted the volume called “shared” into two different directories. You need to mount it to the same mount point in the container.

This is your PHP container

This is your assets container

This will not work of course as you will mount the content of the /var/www/html created by the PHP container into /var/www/html/assets in the assets container so the package.json will be at /var/www/html/assets/assets/package.json

You can easily confirm it by entering the container and listing the files:

docker compose exec assets ls -l /var/www/html/assets