Error: creating session files when running laravel in docker - php

I have a problem when building laravel with Docker.
Here is my docker-compose.yml file:
services:
php:
build:
context: ./
dockerfile: php.Dockerfile
container_name: php-80
volumes:
- ./:/var/www/html
webserver:
image: nginx:stable-alpine
container_name: nginx-webserver
links:
- php:fpm
environment:
- FPM_HOST=fpm
- FPM_PORT=9000
volumes:
- ./:/var/www/html
- ./docker/nginx_conf:/etc/nginx/conf.d/
ports:
- "8000:80"
db:
image: mysql:5.7
container_name: mysql-db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sampleDB
MYSQL_USER: sampleUser
MYSQL_PASSWORD: password
ports:
- "3306:3306"
My Dockerfile:
FROM php:8.0-fpm-alpine
# install required extension
RUN apk add libxml2-dev
RUN docker-php-ext-install pdo_mysql
# composer
ENV COMPOSER_ALLOW_SUPERUSER 1
ENV COMPOSER_HOME /composer
RUN curl -sS https://getcomposer.org/installer \
| php -- --install-dir=/usr/bin --filename=composer
Configuration for nginx:
server {
listen 80; ## listen for ipv4; this line is default and implied
listen [::]:80 default ipv6only=on; ## listen for ipv6
server_name localhost;
root /var/www/html/public;
index index.html index.php;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
location / {
try_files $uri $uri /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass fpm:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
# deny access to . files, for security
location ~ /\. {
log_not_found off;
deny all;
}
}
I run docker-compose up --build. After that, ssh to PHP container and using composer to create laravel project. Configuration permission:
chmod -R 777 storage
chmod -R 775 bootstrap/cache
Pull vendor by
composer install
But when I access url http://localhost:8000/index.php, I get an error: file_put_contents(/var/www/html/storage/framework/sessions/Amv2Kart2HNfbvigAV0WiTE7eVlSb9ghS1kG3V61): Failed to open stream: Invalid argument
[1]: https://i.stack.imgur.com/0dNh4.png
Anyone can help me to solve this problem? Thanks so much.

Related

Why doesn't work the nginx container in docker-compose vuejs, php, nginx?

I made the vuejs and php to work, but the nginx won't to start. If I click on the "start" button in the docker app, it immediately stopps.
In the docker app, I have this message by the nginx container (maybe it helps):
nginx: [emerg] host not found in upstream "app" in /etc/nginx/conf.d/nginx.conf:10
Here are the files:
vuejs Dockerfile:
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 80
CMD ["npm", "run", "dev", "--", "--host"]
php Dockerfile
FROM php:8.0.2-fpm
WORKDIR /var/www
docker-compose.yml
services:
vue:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- '3001:3001'
container_name: vue_c
volumes:
- ./frontend:/app
- /app/node_modules
php:
build:
context: ./backend
dockerfile: Dockerfile
container_name: php_c
volumes:
- ./backend:/var/www
nginx:
image: nginx:1.19-alpine
container_name: nginx_c
expose:
- '8000'
ports:
- '80:80'
volumes:
- ./backend:/var/www
- ./backend/nginx:/etc/nginx/conf.d
nginx.conf:
server {
listen 80;
index index.php;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
error_page 404 /index.php;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}

docker nginx 404 not found

Why I get this error when visit localhost:8080!
Any suggestion to solve this issue is welcome because I spend many time for it and I changed PHP docker file and nginx file many times, similar version with laravel framework is ok for me.
*1 open() "/var/www/html/myapp404" failed (2: No such file or directory)
Note that myapp folder contains index.php and other files but it is not a Laravel framework.
My docker compose:
version: '3.8'
services:
nginx:
build:
context: .
dockerfile: nginx.dockerfile
ports:
- 8080:80
depends_on:
- php
- mysql
php:
build:
context: .
dockerfile: php.dockerfile
container_name: php
volumes:
- ${PWD}/myapp:/var/www/html/myapp
working_dir: /var/www/html/myapp
ports:
- "9000:9000"
mysql:
image: yobasystems/alpine-mariadb
ports:
- "3308:3308"
volumes:
- ./mariadb/data:/var/lib/mysql
- ./mariadb/my.conf:/etc/my.cnf
environment:
MYSQL_ROOT_PASSWORD: "111"
MYSQL_DATABASE: myapp
MYSQL_USER: root
MYSQL_PASSWORD: "111"
restart: always
My default.conf => 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/myapp;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# pass the PHP scripts to FastCGI server listening on php:9000
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;
}
}
nginx:
FROM nginx:stable-alpine
ADD nginx/default.conf /etc/nginx/conf.d/default.conf
php :
FROM dwchiang/nginx-php-fpm:latest
Thanks.

configure shadowd with nginx for laravel project

hi everyone i'm working on deploy laravel project on docker and use shadowd as waf, laravel project work probly on docker with nginx but when i try to configure show i find this output:
nginx configuration:
server {
listen 80;
listen [::]:80;
server_name localhost;
root /var/www/html/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param PHP_ADMIN_VALUE "auto_prepend_file=/usr/share/shadowd/shadowd.php";
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
to deploy my laravel project i create docker compose file and dockefile
docker compose:
version: "3.9" # optional since v1.27.0
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: laravel_nginx
ports:
- "8088:80"
volumes:
- "./:/var/www/html"
- "./nginx/nginx.conf:/etc/nginx/conf.d/default.conf"
# depends_on:
# - php
# - mysql
networks:
- laravel
mysql:
image: mysql:8
container_name: laravel_mysql
ports:
- "3366:3306"
volumes:
- "./mysql:/var/lib/mysql"
restart: unless-stopped
tty: true
environment:
- MYSQL_DATABASE=ttDataBase
- MYSQL_USER=root
- MYSQL_ROOT_PASSWORD=root
- SERVICE_TAGS=dev
- MYSQL_SERVICE_NAME=mysql
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: laravel_php
ports:
- "9000:9000"
volumes:
- "./:/var/www/html"
networks:
- laravel
dockerfile
FROM php:8.0.3-fpm-alpine3.12
RUN docker-php-ext-install pdo pdo_mysql
RUN apk add libzip-dev
RUN docker-php-ext-install zip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
two networks are on docker :
please someone help me.

Permission Denied Nginx Docker

I'm using docker compose to boot up a development workspace, consisting of php, nginx and mysql. Everything boots, static html get's served, but when trying to start a laravel app, i get the following error:
The stream or file "/home/html/storage/logs/laravel-2019-06-10.log" could not be opened: failed to open stream: Permission denied
I searched around and it looked like a permissions issue? Do note, that the docker with just the database and the build in php server does seem to work.
My docker-compose.yml
version: "3"
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: "root"
ports:
- 3306:3306
php-fpm:
image: php:7.3-fpm-alpine
links:
- db
volumes:
- "./:/home/html/"
nginx:
image: nginx:1-alpine
ports:
- "8080:80"
links:
- php-fpm
volumes:
- "./site.conf:/etc/nginx/conf.d/default.conf"
- "./:/home/html/"
My nginx config:
server {
index index.php index.html;
listen 80 default_server;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /home/html/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Kind regards :)
Enter the php-fpm container:
docker-compose -i -t exec php-fpm /bin/sh
Then change access rights of storage folder:
chmod -r 777 /home/html/storage
Cause it's local development environment, correct rights doesn't matter.

Craft CMS With Php/Nginx on Docker can't write outside Document Root

I'm trying to set up a containerized version of a basic Craft CMS set up using Docker with Php and Nginx. Craft can't seem to write to folders outside of my Nginx Document Root. (See bottom of post for more details) I'm using Docker-Compose and this is my current Docker-compose.yml:
nginx:
build: ./compose/nginx/
ports:
- 80:80
links:
- php
volumes_from:
- app
php:
build: ./compose/php/
expose:
- 9000
links:
- mysql
volumes_from:
- app
app:
image: php:7.0-fpm
volumes:
- ./app:/var/www/html
- ./vendors:/var/www
command: "true"
mysql:
image: mysql:latest
volumes_from:
- data
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: project
MYSQL_USER: project
MYSQL_PASSWORD: project
data:
image: mysql:latest
volumes:
- /var/lib/mysql
command: "true"
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 8080:8080
links:
- mysql
environment:
PMA_HOST: mysql
This set up works perfectly, here are my two build files for Nginx
- DockerFile:
FROM nginx:latest
COPY ./default.conf /etc/nginx/conf.d/default.conf
Default.conf
server {
listen 80 default_server;
root /var/www/html;
index index.html index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
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_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
}
Everything works near flawlessly, until I try to deploy Craft CMS, I understand that I don't have all the php-extensions, but I don't believe that affects my problem. I keep getting the Error, /var/www/craft/config isn't writable by PHP. Please fix that. Thus I am assuming that this is an error with permissions with Nginx and PHP. Although, I have tried opening a bash shell inside both of my nginx and php containers and chmod 777 craft/config folder, and I still get this error, thus I am a bit confused as to how to fix it.
EDIT 1 ------
I was able to fix this problem by implementing the following into my Dockerfile within my php build profile.
RUN usermod -u 1000 www-data
RUN usermod -G staff www-data
RUN chown -R www-data:www-data /var/www
I'm not sure if this is the safest way.
In your docker-compose.yml app container definition the volume declaration ./vendors:/var/www effectively overwrites ./app:/var/www/html. /var/www/craft/ therefore very likely does not exist.
Volume declarations for app should look as follows instead:
app:
image: php:7.0-fpm
volumes:
- ./app:/var/www/html
- ./vendors:/var/www/vendors
command: "true"

Categories