skip to Main Content

I am new to Docker world. I am trying to start a laravel project in Docker but when I run docker-compose up nginx I get this error message.

The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied

This is my docker-compose.yml

version: '3.8'

services:
  nginx:
    build:
      context: .
      dockerfile: nginx.dockerfile
    ports:
      - 80:80
    volumes:
      - ./src:/var/www/html
    depends_on:
      - mysql
      - php

  mysql:
    image: mysql:5.7
    ports:
      - 3306:3306
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_USER: laravel
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      - ./mysql:/var/lib/mysql

  php:
    build:
      context: .
      dockerfile: php.dockerfile
    volumes:
      - ./src:/var/www/html

  composer:
    build:
      context: .
      dockerfile: composer.dockerfile
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html

This is my php.dockerfile

FROM php:8-fpm-alpine

ENV PHPGROUP=laravel
ENV PHPUSER=laravel

RUN adduser -g ${PHPGROUP} -s /bin/sh -D ${PHPUSER}

RUN sed -i "s/user = www-data/user = ${PHPUSER}/g" /usr/local/etc/php-fpm.d/www.conf
RUN sed -i "s/group = www-data/group = ${PHPGROUP}/g" /usr/local/etc/php-fpm.d/www.conf

RUN mkdir -p /var/www/html/public

RUN docker-php-ext-install pdo pdo_mysql


CMD ["php-fpm", "-y", "/usr/local/etc/php-fpm.conf", "-R"]

I’ve searched the internet for a solution and executed some commands but I still get the same problem.
Any idea what’s going wrong and how I can fix it?

2

Answers


  1. I was looking for a solution for this problem for the past two days, ended up finding it on a excruciating 15 minute YouTube video that unbelievably actually got it right.

    1. SSH into the container with sail shell or use the Docker container
      terminal directly from its dashboard;
    2. Run chown -R sail:sail storage;
    3. Problem solved.

    This is because the user sail has the same UID of 1000 as your actual Linux user, which owns the directory storage in your host machine.

    Login or Signup to reply.
  2. What worked for me was the following

    I logged in inside the container and I removed the existing laravel.log file

    docker exec -it 0a6ea1039145 bash
    
    root@0a6ea1039145:/var/www/html/storage/logs# ls -la
    -rwxrwxrwx 1 1000 sail    14 Apr  2 21:21 .gitignore
    -rw-r--r-- 1 1000 sail 41664 Apr  3 20:41 laravel.log
    
    root@0a6ea1039145:/var/www/html/storage/logs# rm laravel.log
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search