joe_inz
joe_inz

Reputation: 1236

Logs directory permission denied error when running Artisan commands

I have a setup of docker with laravel and apache alongside mysql, when trying to run artisan command in the terminal of vscode i get :

There is no existing directory at "/var/www/html/storage/logs" and its not buildable: Permission denied

Apache setup in docker compose:
laravel-app:
    build:
      context: ./docker/app
      args:
        uid: ${UID}
    container_name: laravel-app
    environment:
      - APACHE_RUN_USER=#${UID}
      - APACHE_RUN_GROUP=#${UID}
    volumes:
      - .:/var/www/html
    ports:
      - ${HOST_PORT}:80
    networks:
      backend:
        aliases:
          - laravel-app
Dockerfile of apache
FROM php:7.2-apache

RUN apt-get update

# 1. development packages
RUN apt-get install -y \
    git \
    zip \
    curl \
    sudo \
    unzip \
    libicu-dev \
    libbz2-dev \
    libpng-dev \
    libjpeg-dev \
    libmcrypt-dev \
    libreadline-dev \
    libfreetype6-dev \
    g++

# 2. apache configs + document root
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

# 3. mod_rewrite for URL rewrite and mod_headers for .htaccess extra headers like Access-Control-Allow-Origin-
RUN a2enmod rewrite headers

# 4. start with base php config, then add extensions
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

RUN docker-php-ext-install \
    bz2 \
    intl \
    iconv \
    bcmath \
    opcache \
    calendar \
    mbstring \
    pdo_mysql \
    zip

# 5. composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# 6. we need a user with the same UID/GID with host user
# so when we execute CLI commands, all the host file's ownership remains intact
# otherwise command from inside container will create root-owned files and directories
ARG uid
RUN useradd -G www-data,root -u $uid -d /home/devuser devuser
RUN mkdir -p /home/devuser/.composer && \
    chown -R devuser:devuser /home/devuser

even though the directory exists, also the commands run successfully from within the container. Should i always run the commands related to laravel artisan from the container, or there is something wrong?

Upvotes: 1

Views: 5671

Answers (2)

Ronald Kimeli
Ronald Kimeli

Reputation: 51

Solved mine by changing start-container for sail laravel

#!/usr/bin/env bash

if [ ! -z "$WWWUSER" ]; then
    usermod -u $WWWUSER sail
fi

// Edit this to match the error "The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied"
// We need three files inside storage. Note: no file named storage by default so we shall create

if [ ! -d /. storage]; then
    mkdir /.storage
    cd storage/
    mkdir -p framework/{sessions,views,cache}
    cd ..
fi

chmod -R ugo+rw /.storage

if [ ! -d /.composer ]; then
    mkdir /.composer
fi

chmod -R ugo+rw /.composer

if [ $# -gt 0 ]; then
    exec gosu $WWWUSER "$@"
else
    /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
fi

This means we create storage folder and have permissions for caching

Upvotes: 0

Shankar S Bavan
Shankar S Bavan

Reputation: 962

Go to your project folder and open terminal then, run this command

sudo chmod -R 775 storage

Upvotes: 3

Related Questions