2

I am having issues with my docker-compose to point the nginx container to serve the directory ${APP_CONTAINER_DIR} from my app container

right now, connecting to 127.0.0.1:8000 I get nginx error 404; I am guessing it is not finding the files since I have no error in the nginx logs

I think the problem comes from root /var/www/app/public; in dev.conf but not sure how to do it; I tried app:root /var/www/app/public; but this is not working

here is my docker-compose

version: "3"
services:

  nginx:
    container_name: nginx
    image: "${NGINX_IMAGE}"
    build: build/nginx
    restart: 'always'
    env_file: .env
    ports:
      - "8000:443"
    volumes:
      - "./build/nginx/build/dev.conf:/etc/nginx/conf.d/default.conf:ro"
      - "./build/nginx/build/certs:/etc/nginx/certs"
    networks:
      - app_network
    depends_on:
      - app

  app:
    container_name: app
    image: "${APP_IMAGE}"
    restart: always
    build: build/app
    env_file: .env
    volumes:
      - "${APP_HOST_DIR}:${APP_CONTAINER_DIR}"
    depends_on:
      - database
    networks:
      - app_network

  database:
    container_name: mariadb
    image: "mariadb:${MARIADB_VERSION}"
    restart: 'always'
    env_file: .env
    volumes:
      - "${SQL_INIT}:/docker-entrypoint-initdb.d"
      - "db_data:${MARIADB_DATA_DIR}"
      - "db_logs:${MARIADB_LOG_DIR}"
    environment:
      MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
      MYSQL_DATABASE: "${MYSQL_DATABASE}"
      MYSQL_USER: "${MYSQL_USER}"
      MYSQL_PASSWORD: "${MYSQL_PASSWORD}"
    ports:
      - "3306:3306"
    networks:
      - app_network

volumes:
  db_data:
  db_logs:

networks:
  app_network:

dev.conf

#./images/nginx/build/default.conf

server {
    listen 443 ssl;
    server_name  127.0.0.1;
    ssl_certificate /etc/nginx/certs/dev.crt;
    ssl_certificate_key /etc/nginx/certs/dev.key;
    index index.php index.html;
    root /var/www/app/public;

        location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    error_log /var/log/nginx/app.error.log;
    access_log /var/log/nginx/app.access.log;
}

.env

APP_HOST_DIR=./app
APP_CONTAINER_DIR=/var/www/app/
Sam
  • 1,557
  • 3
  • 26
  • 51
  • Did you bash into your ./app directory to check if there is a public directory inside the container? – Soheb Jan 17 '22 at 13:15
  • @Soheb yes I have index.php at this location – Sam Jan 17 '22 at 13:16
  • Would you try removing ```:ro```from the end of the line ```./build/nginx/build/dev.conf:/etc/nginx/conf.d/default.conf:ro``` to ```./build/nginx/build/dev.conf:/etc/nginx/conf.d/default.conf``` I know it is mentioned in the docs, but please try and let me know if that helps – Soheb Jan 17 '22 at 13:26
  • @Soheb i tried already actually; did not help – Sam Jan 17 '22 at 13:35
  • The linked question goes through pretty much all the alternatives on this: having the application serve its own static files, copying the static assets into the reverse proxy image, or a couple of approaches around shared named volumes. – David Maze Jan 17 '22 at 15:02

2 Answers2

2

Your docker-compose.yml is not complete, you have to mount the path /var/www/app/public to your php-code, e.g. - "./${APP_HOST_DIR}:/var/www/app/public:ro".

Furthermore you should remove the variable $APP_CONTAINER_DIR in favor of the configured path /var/www/app/public.

So your file should look like this:

version: "3"
services:

  nginx:
    container_name: nginx
    image: "${NGINX_IMAGE}"
    build: build/nginx
    restart: 'always'
    env_file: .env
    ports:
      - "8000:443"
    volumes:
      - "./build/nginx/build/dev.conf:/etc/nginx/conf.d/default.conf:ro"
      - "./build/nginx/build/certs:/etc/nginx/certs"
      - "${APP_HOST_DIR}:/var/www/app/public:ro"
    networks:
      - app_network
    depends_on:
      - app

  app:
    container_name: app
    image: "${APP_IMAGE}"
    restart: always
    build: build/app
    env_file: .env
    volumes:
      - "${APP_HOST_DIR}:/var/www/app/public:ro"
    depends_on:
      - database
    networks:
      - app_network

  database:
    container_name: mariadb
    image: "mariadb:${MARIADB_VERSION}"
    restart: 'always'
    env_file: .env
    volumes:
      - "${SQL_INIT}:/docker-entrypoint-initdb.d"
      - "db_data:${MARIADB_DATA_DIR}"
      - "db_logs:${MARIADB_LOG_DIR}"
    environment:
      MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
      MYSQL_DATABASE: "${MYSQL_DATABASE}"
      MYSQL_USER: "${MYSQL_USER}"
      MYSQL_PASSWORD: "${MYSQL_PASSWORD}"
    ports:
      - "3306:3306"
    networks:
      - app_network

volumes:
  db_data:
  db_logs:

networks:
  app_network:
nemoinho
  • 604
  • 4
  • 15
0

You should go with the given folder mounted inside nginx docker.

If there is some dynamic files/content that the app is generating, you should also mount the same host folder in app docker container.

Rok
  • 61
  • 3