Permissions problem with my first Laravel dev environment with Docker - laravel

I'm switching my development environments from virtual machines (with Vagrant and VirtualBox) to containers with Docker. I successfully set-up a simple dev environment for WordPress and now I'm trying to do the same for Laravel.
I'm on Windows 10 Pro, using WSL2 (Ubuntu 20.04 installed).
If I try to on on localhost:8088 (where my local Laravel installation is exposed) I get:
UnexpectedValueException
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 file:
version: '3.3'
services:
nginx:
container_name: 'NGINX_Web_Server'
image: nginx:stable-alpine
depends_on:
- php
- mariadb
ports:
- 8088:80
volumes:
- ./root:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
php:
container_name: 'PHP-FPM_7.4'
build:
context: .
dockerfile: Dockerfile
volumes:
- ./root:/var/www/html
ports:
- 9090:9000
mariadb:
container_name: 'MariaDB_SQL_Server'
image: mariadb
restart: unless-stopped
tty: true
volumes:
- ./mariadb_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: laravel
MYSQL_USER: ll_user
MYSQL_PASSWORD: ll_password
this is the Dockerfile indicated in docker-compose.yml:
FROM php:fpm-alpine3.12
RUN docker-php-ext-install pdo pdo_mysql
#RUN chown -R www-data:www-data /var/www
and the default.conf file for nginx:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
What's wrong?

Could be solved with:
chown -R www-data:www-data /var/www
chmod -R 755 /var/www/storage
If it does not work for you, try this in the container:
Check: docker-compose exec php ls -al /var/www/storage
I saw xfs instead of www-data
Run: docker-compose exec php chown -R $USER:www-data /var/www/storage
Run: docker-compose exec php chown -R $USER:www-data /var/www/bootstrap/cache
Try again and it must work

Related

Laravel Docker On Window Volume mounting

I setup docker on window and setup Laravel project to run on docker, images are created but unfortunately volume is not mounting. Docker Project https://github.com/wariszargardev/Laravel-Nginx-Docker- project is setup on root. I have tried no of solution but unfortunately to setup on window. Same project with no changes working on Mac M2.
docker-compose.yml
version: '3.8'
networks:
laravel-vpn-network:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8080:80"
volumes:
- ./:/var/www/html
- ./nginx/conf.d/:/etc/nginx/conf.d/
depends_on:
- php
# - mysql
networks:
- laravel-vpn-network
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./:/var/www/html
ports:
- "9000:9000"
networks:
- Laravel-vpn-network
**Docker File **
FROM php:8.0-fpm-alpine
WORKDIR /var/www/html
RUN docker-php-ext-install pdo pdo_mysql
EXPOSE 9000
**
App.conf**
Root of project in nginx directory conf.d directory inside this app.conf file present
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
This simple template if anyone available to guide me?

Problem docker-compose with laravel nginx not working on production

I wrote a docker-compose.yml with nginx, mysql, and an application laravel with a dockerfile.
My docker-compose :
version: "3.7"
services:
app_back:
build:
context: .
dockerfile: Dockerfile
image: dreamy_back
container_name: dreamy-back-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- .:/var/www
networks:
- dreamy
db:
image: mysql
container_name: dreamy-db
restart: unless-stopped
depends_on:
- app_back
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: 4ztgeU%
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
networks:
- dreamy
nginx:
image: nginx:1.17-alpine
container_name: dreamy-back-nginx
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./docker-compose/vhosts:/etc/nginx/conf.d
networks:
- dreamy
networks:
dreamy:
driver: bridge
My dockerfile for an laravel application :
I execute composer install inside the container.
FROM php:8.1-fpm
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Set working directory
WORKDIR /var/www
My nginx conf :
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app_back:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
On my laptop it's working but on my vps server I get an error 500.
My docker should be accessible port 8000
My nginx logs
UPDATE - I found the problem, in production the application does not display laravel errors. So it is the default ngynx page that is displayed. The error was the right for the storage file.
Thank you for your help.

Docker Laravel permission errors

I am trying to setup a stack on Docker using Laravel using the following:
I used this command on my Local Machine:
composer create-project --prefer-dist laravel/laravel project
Then made my files inside of it:
Dockerfile:
FROM php:8.2-fpm-alpine
RUN apk update && apk add postgresql-dev && docker-php-ext-install pdo pdo_pgsql
COPY . /var/www/html/
Docker-Compose:
version: '3'
services:
app:
build: .
ports:
- "9000:9000"
links:
- db
db:
image: postgres:15.1-alpine
environment:
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
web:
image: nginx:1.23.3-alpine
ports:
- "80:80"
volumes:
- ./:/var/www/html
- ./nginx.conf:/etc/nginx/nginx.conf
links:
- app
php:
build:
context: .
dockerfile: Dockerfile
volumes:
- ./:/var/www/html
links:
- db
and the result is this error:
The exception occurred while attempting to log:
The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied
Context: {"exception":{}}
and to fix it I added those lines to my Dockerfile
RUN mkdir -p /var/www/html/storage/logs/
RUN chown -R www-data:www-data /var/www/html/storage/logs/
RUN touch /var/www/html/storage/logs/laravel.log && chmod 777 /var/www/html/storage/logs/laravel.log
and the result is this error:
file_put_contents(/var/www/html/storage/framework/sessions/QFHlxtxFBzTsqNNRZwVZADwzdA9XsuFIzujiuqO3): Failed to open stream: Permission denied
then I tried adding these lines to try to make the entire directory writeable/readable in my Dockerfile
RUN mkdir -p /var/www/html/storage/
RUN chown -R www-data:www-data /var/www/html/storage/
RUN chmod -R 777 /var/www/html/storage/
to the final full Dockerfile:
FROM php:8.2-fpm-alpine
RUN apk update && apk add postgresql-dev && docker-php-ext-install pdo pdo_pgsql
RUN mkdir -p /var/www/html/storage/
RUN chown -R www-data:www-data /var/www/html/storage/
RUN chmod -R 777 /var/www/html/storage/
RUN mkdir -p /var/www/html/storage/logs/
RUN chown -R www-data:www-data /var/www/html/storage/logs/
RUN touch /var/www/html/storage/logs/laravel.log && chmod 777 /var/www/html/storage/logs/laravel.log
COPY . /var/www/html/
but nothing changed in the error message.
this is my nginx.conf file:
events {
worker_connections 1024;
}
http {
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
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;
}
}
}
Did I manage something wrong in my installation steps?

Nginx config not working on my docker container

I'm new with dockers.
I'm studying this docker-compose.yml in order to create a this three containers for webserver, workspace for my laravel and the db. And I'm understanding most of it.
It builds just fine but on localhost I get the nginx default page, not my laravel public folder.
I have a app.conf file inside of /nginx/conf.d/
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
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;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
version: '3.2'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: myapp-workspace
container_name: myapp-workspace
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: "/var/www"
volumes:
- ./:/var/www
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: myapp-nginx
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- .:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.22
container_name: myapp-db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: 'database'
MYSQL_ROOT_PASSWORD: 'password'
MYSQL_PASSWORD: 'password'
MYSQL_USER: 'user'
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./dbdata:/var/lib/mysql/
- ./mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
Dockerfile
FROM php:7.3-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
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"]
Found the problem.
Seems that the "app" part on this line "fastcgi_pass app:9000;" on my app.conf should be the name of my workspace container.
So like this it works.
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass myapp-workspace:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}

Ngnix laravel docker project return error 502 bad gateway

I tried run laravel inside docker container with ngnix.
Directory structure:
File docker-compose.yml:
version: '3'
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "${HOST_PORT}:80"
volumes:
- ../:/var/www/html
- ./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
php:
build:
context: .
dockerfile: ../Dockerfile
restart: always
container_name: php
volumes:
- "../:/var/www/html"
ports:
- "9000:9000"
mongodb:
image: mongo:latest
container_name: mongodb
ports:
- "27017:27017"
redis:
image: redis:latest
container_name: redis
restart: always
ports:
- "6379:6379"
File Dockerfile:
FROM php:7.4-apache
RUN apt-get update && apt-get install --yes --no-install-recommends \
libssl-dev
RUN docker-php-ext-install pdo pdo_mysql
RUN pecl install mongodb \
&& docker-php-ext-enable mongodb
Result of command docker ps:
When I tried open in browser address http://localhost:8004 then get error:
502 Bad Gateway nginx/1.18.0
Ngnix config file:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Env vars:
HOST_PORT=8004
HOST_SSL_PORT=3004
# Nginx
NGINX_HOST=localhost
As you're using nginx as your web-server, you don't need to install PHP Apache (FROM php:7.4-apache) in your Dockerfile.
Instead try php:7.4-fpm and make sure nginx accesses that php-fpm correctly (line fastcgi_pass php:9000; in your default.conf file. In your case everything seems configured correctly).
One more thing to note that it's not necessary to expose php-fpm port 9000 to the host machine. As both containers (in your docker-compose.yml) will use the same network, nginx container can directly access php container. You can remove the lines
ports:
- "9000:9000"

Resources