johnjullies
johnjullies

Reputation: 755

Docker PHP Nginx 404 Permission denied

I was working on a project developed with Docker on Windows. But when I continued working on it in my Linux machine, I'm getting the 404 Not Found nginx page.

enter image description here

Docker Compose logs on my webserver service says Permission denied

webserver    | 2020/03/14 10:05:29 [crit] 6#6: *1 stat() "/var/www/public/" failed (13: Permission denied), client: <my_ip_address>, server: , request: "GET / HTTP/1.1", host: "localhost"
webserver    | 2020/03/14 10:05:29 [crit] 6#6: *1 stat() "/var/www/public/" failed (13: Permission denied), client: <my_ip_address>, server: , request: "GET / HTTP/1.1", host: "localhost"
webserver    | 2020/03/14 10:05:29 [crit] 6#6: *1 stat() "/var/www/public/index.php" failed (13: Permission denied), client: <my_ip_address>, server: , request: "GET / HTTP/1.1", host: "localhost"
webserver    | <my_ip_address> - - [14/Mar/2020:10:05:29 +0000] "GET / HTTP/1.1" 404 153 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:73.0) Gecko/20100101 Firefox/73.0"

I tried running this on the app's directory on my host machine but the issue persists:

sudo chown -R $USER:$USER ~/my-app

Here's my docker-compose.yml:

version: '3'
#Docker Services
services:
  #PHP + Laravel Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: digitalocean.com/php
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    networks:
      - app-network
    volumes:
      - ./:/var/www
      - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini

  #Nginx Service
  webserver:
    image: nginx:alpine
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    networks:
      - app-network
    volumes:
      - ./:/var/www
      - ./nginx/conf.d/:/etc/nginx/conf.d/

  #MySQL Service
  db:
    image: mysql:5.7.22
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: <password>
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    networks:
      - app-network
    volumes:
      - dbdata:/var/lib/mysql
      - ./mysql/my.cnf:/etc/mysql/my.cnf

#Docker Networks
networks:
  app-network:
    driver: bridge

#Volumes
volumes:
  dbdata:
    driver: local

Here's the Dockerfile:

FROM php:7.2-fpm

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd

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

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www:www . /var/www

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

I tried changing the line 42 on the Dockerfile to this but it doesn't help:

RUN chown -R www:www /var/www

I'm using:

EDIT: Additional info

$ docker-compose exec app ls -la /var/www
total 1076
drwx------  17 www  www    4096 Mar 14 11:05 .
drwxr-xr-x   1 root root   4096 Feb 26 11:59 ..
-rw-rw-r--   1 www  www     235 Mar 10 10:27 .editorconfig
-rw-rw-r--   1 www  www     983 Mar 13 10:26 .env
-rw-rw-r--   1 www  www     824 Mar 10 10:27 .env.example
drwx------   7 www  www    4096 Mar 14 11:05 .git
-rw-rw-r--   1 www  www     116 Mar 10 10:27 .gitattributes
-rw-rw-r--   1 www  www     175 Mar 10 10:27 .gitignore
-rw-rw-r--   1 www  www     187 Mar 10 10:27 .styleci.yml
-rw-rw-r--   1 www  www   34515 Mar 10 10:27 CHANGELOG.md
-rw-rw-r--   1 www  www    1206 Mar 14 11:05 Dockerfile
-rw-rw-r--   1 www  www    1597 Mar 13 20:28 README.md
drwx------   7 www  www    4096 Mar 13 17:18 app
-rw-rw-r--   1 www  www    1739 Mar 10 10:27 artisan
drwx------   3 www  www    4096 Mar 13 17:18 bootstrap
-rw-rw-r--   1 www  www    1731 Mar 13 15:08 composer.json
-rw-rw-r--   1 www  www  215207 Mar 13 15:10 composer.lock
drwx------   2 www  www    4096 Mar 13 17:18 config
drwx------   5 www  www    4096 Mar 13 17:18 database
-rw-rw-r--   1 www  www    1239 Mar 10 11:29 docker-compose.yml
drwx------   2 www  www    4096 Mar 13 17:18 mysql
drwx------   3 www  www    4096 Mar 13 17:18 nginx
drwx------ 711 www  www  102400 Mar 13 17:18 node_modules
-rw-rw-r--   1 www  www  456333 Mar 10 15:43 package-lock.json
-rw-rw-r--   1 www  www    1144 Mar 10 15:43 package.json
drwx------   2 www  www    4096 Mar 13 17:18 php
-rw-rw-r--   1 www  www    1168 Mar 10 10:27 phpunit.xml
drwx------   4 www  www    4096 Mar 13 17:18 public
drwx------   6 www  www    4096 Mar 13 17:18 resources
drwx------   2 www  www    4096 Mar 13 17:18 routes
-rw-rw-r--   1 www  www     584 Mar 10 10:27 server.php
drwx------   5 www  www    4096 Mar 13 17:18 storage
drwx------   4 www  www    4096 Mar 13 17:18 tests
drwx------  48 www  www   12288 Mar 13 17:18 vendor
-rw-rw-r--   1 www  www     553 Mar 10 10:27 webpack.mix.js
failed to resize tty, using default size

Upvotes: 1

Views: 3858

Answers (1)

johnjullies
johnjullies

Reputation: 755

Fixed it! I just needed to run

find /var/www -type d -exec chmod 0775 '{}' \;

on the app container which sets all directories under /var/www (inclusive) to read-exec for group/other users.

Upvotes: 10

Related Questions