Laravel with docker on RPI - Failed to open stream: Permission denied - laravel

I am testing deployment with docker on RPI but getting "Failed to open stream: Permission denied"
I tried few solutions but none of them worked for me
Permission Denied Error using Laravel & Docker
Laravel & Docker: The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied
my Dockerfile:
FROM php:8.0-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql
RUN chown -R www-data:www-data /var/www
RUN chmod -R 755 .
and docker-compose.yml
version: "3.7"
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
restart: unless-stopped
container_name: nginx
ports:
- "80:80"
volumes:
- ./:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mariadb
networks:
- laravel
mariadb:
image: yobasystems/alpine-mariadb:latest
restart: unless-stopped
container_name: mariadb
tty: true
ports:
- "4306:3306"
volumes:
- ./mariadb:/var/lib/mariadb
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: laravel_dev
MYSQL_USER: laravel_dev
MYSQL_PASSWORD: laravel_dev
SERVICE_TAGS: dev
SERVICE_NAME: mariadb
networks:
- laravel
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin
restart: unless-stopped
ports:
- 4380:80
environment:
PMA_ARBITRARY: 1
PMA_PORT: 3306
PMA_HOST: mariadb
PMA_USER: laravel_dev
PMA_PASSWORD: laravel_dev
depends_on:
- mariadb
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./:/var/www/html
ports:
- "9000:9000"
networks:
- laravel
composer:
image: composer:latest
container_name: composer
volumes:
- ./:/var/www/html
working_dir: /var/www/html
networks:
- laravel
npm:
image: node:current-alpine3.14
container_name: npm
volumes:
- ./:/var/www/html
working_dir: /var/www/html
entrypoint: ['npm']
networks:
- laravel
artisan:
build:
context: .
dockerfile: Dockerfile
container_name: artisan
volumes:
- ./:/var/www/html
depends_on:
- mariadb
working_dir: /var/www/html
entrypoint: ['php', '/var/www/html/artisan']
networks:
- laravel
Only thing that worked was chmod 777 on project root folder but that is not the solution i would be comfortable with using.

So i found a solution on reddit
https://www.reddit.com/r/docker/comments/hjsipd/permission_denied_with_volumes/fwoixqe/
I had to sign project folder to container user, which i found with
less /etc/passwd. In this case name of user was www-data with UID 82. And command chown -R 82:82 .

Related

Trying to run Vite inside Docker container

I'm new in Docker and JS technologies so don't judje too hard. I'm trying to run npm run dev from node.js container (called npm). It goes right and shows that css and scripts are available at localhost:3009, but they're not.
Am I right that the problem is with node.js container, which is trying to restart all the time and doesn't exposes any ports? If yes, how can I fix it? Thank you in advance.
Here is my docker-compose.yml and vite.config.js
version: '3'
networks:
laravel:
services:
site:
build:
context: ./dockerfiles
dockerfile: nginx.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: nginx
restart: unless-stopped
ports:
- "80:80"
volumes:
- ./src:/var/www/html:delegated
depends_on:
- php
- redis
- mysql
- mailhog
networks:
- laravel
mysql:
image: mariadb:10.6
container_name: mysql
restart: unless-stopped
tty: true
volumes:
- ./db-data:/var/lib/mysql
ports:
- 3306:3306
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
php:
build:
context: ./dockerfiles
dockerfile: php.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: php
restart: unless-stopped
volumes:
- ./src:/var/www/html:delegated
networks:
- laravel
redis:
image: redis:alpine
container_name: redis
restart: unless-stopped
ports:
- 6379:6379
networks:
- laravel
composer:
build:
context: ./dockerfiles
dockerfile: composer.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- php
user: laravel
entrypoint: ['composer', '--ignore-platform-reqs']
networks:
- laravel
npm:
image: node:latest
container_name: npm
restart: always
volumes:
- ./src:/var/www/html
ports:
- 3000:3000
- 3001:3001
- 3009:3009
working_dir: /var/www/html
entrypoint: ['npm']
networks:
- laravel
artisan:
build:
context: ./dockerfiles
dockerfile: php.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: artisan
volumes:
- ./src:/var/www/html:delegated
depends_on:
- mysql
working_dir: /var/www/html
entrypoint: ['php', '/var/www/html/artisan']
networks:
- laravel
mailhog:
image: mailhog/mailhog:latest
container_name: mailhog
ports:
- 1025:1025
- 8025:8025
networks:
- laravel
vite.config.js
import {defineConfig} from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
server: {
port: 3009
},
plugins: [
laravel({
input: ['resources/css/app.css', 'resources/js/app.js'],
refresh: true,
}),
],
});
You should use the "--host" key starting your Vite

How To Fix SQLSTATE[HY000] [2002] Connection refused Laravel Docker

When I try to run the command php artisan migrate, an error occurs:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = docker and table_name = migrations and table_type = 'BASE TABLE')
Dockerfile:
FROM php:8.1-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql mysqli
docker-compose.yml:
version: '3'
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
ports:
- "8088:80"
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:8.0
container_name: db
ports:
- "3307:3306"
expose:
- "3306"
volumes:
- ./cache/mysql:/var/lib/mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: docker
MYSQL_USER: root
MYSQL_PASSWORD: root
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src:/var/www/html
ports:
- "9000:9000"
networks:
- laravel
composer:
image: composer:latest
container_name: composer
depends_on:
- nginx
- php
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
networks:
- laravel
.env:
DB_CONNECTION=mysql
DB_HOST="db"
DB_PORT=3306
DB_DATABASE=docker
DB_USERNAME=root
DB_PASSWORD=root
I try too:
DB_HOST=127.0.0.1
DB_HOST=localhost
thank you very good
In docker-compose.yml
services:
## -----------------------------------------------
## MySql database
## -----------------------------------------------
mysql:
image: mysql:8.0
restart: always
volumes:
- db_mysql:/var/lib/mysql
- ./mysql:/docker-entrypoint-initdb.d
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- app-network
deploy:
mode: global
ports:
- "3306:3306"
php:
..
..
environment:
DB_HOST: mysql
DB_NAME: db_name
DB_USERNAME: root
DB_PASSWORD: root
networks:
- laravel
// map volume
volumes:
db_mysql:
In MySQL Dockerfile
FROM mysql:8.0
please chnage the ports in docker-compose.yml File , it works for me
ports:
- 8889:3306
----My-> docker-compose.yml File ---
version: '3.8'
services:
Web Server Service
nginx:
image: nginx:alpine
container_name: nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
depends_on:
- app
- db
#Database Service
db:
image : mysql
container_name : mysql
restart: unless-stopped
tty: true
ports:
- 8889:3306
volumes:
# - ./mysql/data:/var/lib/mysql
environment:
# MYSQL_DATABASE: laravel_blogs
## MYSQL_ROOT_PASSWORD: password
# MYSQL_ROOT_HOST: localhost
# SERVICE_NAME: mysql
# SERVICE_TAGS: dev
environment:
- MYSQL_DATABASE=${DB_DATABASE}
- MYSQL_USER=${DB_USERNAME}
- MYSQL_PASSWORD=${DB_PASSWORD}
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
volumes:
- ~/mysql:/var/lib/mysql
app:
container_name: app
build:
context: ./php
dockerfile: Dockerfile
volumes:
- ./src:/var/www
ports:
- "9000:9000"
working_dir: /var/www
phpMyAdmin:
# phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
ports:
- "3400:80"
depends_on:
- db
links:
- db
#pgadmin:
#image: dpage/pgadmin4
#container_name: pgAdmin
#ports:
# - "5050:80"
#depends_on:
# - db
#environment:
# PGADMIN_DEFAULT_EMAIL: hanieasemi76#gmail.com
PGADMIN_DEFAULT_PASSWORD: password

Problems dockerizing Laravel application - database hosts array is empty

I'm trying to dockerize existing Laravel + Vue.js application, however, when trying to access database getting an error:
Database hosts array is empty.
It looks like that connection fails from PHP to MYSQL (but MySQL image is running and I can connect to it from terminal). Not sure how to proceed with this. It doesn't look to me a misconfiguration. Is there a way to debug it?
Here is what I have:
docker-compose.yml:
version: '3'
networks:
backoffice:
services:
backoffice_nginx:
image: nginx:stable
container_name: backoffice_nginx
restart: unless-stopped
ports:
- "8088:80"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- backoffice_php
networks:
- backoffice
backoffice_mysql:
image: mysql:8.0.13
container_name: backoffice_mysql
restart: unless-stopped
tty: true
ports:
- "33067:3306"
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci', '--default-authentication-plugin=mysql_native_password']
environment:
MYSQL_DATABASE: my_db
MYSQL_USER: dbuser
MYSQL_PASSWORD: mypass
MYSQL_ROOT_PASSWORD: myrootpass
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./mysql/db:/var/lib/mysql
- ./mysql/conf.d:/etc/mysql/conf.d
networks:
- backoffice
backoffice_php:
build:
context: .
dockerfile: php.dockerfile
container_name: backoffice_php
restart: unless-stopped
volumes:
- ./src:/var/www/html
networks:
- backoffice
backoffice_composer:
image: composer:latest
container_name: backoffice_composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- backoffice_php
networks:
- backoffice
backoffice_artisan:
build:
context: .
dockerfile: php.dockerfile
container_name: backoffice_artisan
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
entrypoint: ['php', '/var/www/html/artisan']
networks:
- backoffice
php.dockerfile:
FROM php:7.4-fpm
WORKDIR /var/www/html
RUN docker-php-ext-install bcmath pdo pdo_mysql
RUN pecl install -o -f redis \
&& rm -rf /tmp/pear \
&& docker-php-ext-enable redis
DB configs part in the .env:
DB_CONNECTION=mysql
DB_HOST=backoffice_mysql
DB_PORT=3306
DB_DATABASE=my_db
DB_USERNAME=root
DB_PASSWORD=myrootpass
Here are running containers:
Any ideas?
Thanks.
You are not sharing your .env file with your container. Map your whole . Volume to the container instead of only your ./src directory.

Docker Redis unable to connect with laravel and predis

I'm using docker with laravel project but im struggling to it to connect to the laravel container
###############################################################################
# Generated on phpdocker.io #
###############################################################################
version: "3.1"
services:
redis:
image: redis:alpine
container_name: my-asset-management-redis
mysql:
image: mysql:8.0
container_name: my-asset-management-mysql
working_dir: /application
volumes:
- .:/application
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=app
- MYSQL_USER=user
- MYSQL_PASSWORD=pass
ports:
- "8085:3306"
webserver:
image: nginx:alpine
container_name: my-asset-management-webserver
working_dir: /application
volumes:
- .:/application
- ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8083:80"
php-fpm:
build: phpdocker/php-fpm
container_name: my-asset-management-php-fpm
working_dir: /application
volumes:
- .:/application
- ./phpdocker/php-fpm/php-ini-extras.ini:/etc/php/7.4/fpm/conf.d/99-extras.ini
And I have this as my REDIS_HOST=my-asset-management-redis in my env
But i keep getting this: Predis\Connection\ConnectionException : php_network_getaddresses: getaddrinfo failed: No such host is known. [tcp://my-asset-management-redis:6379]
I have the redis password set as NULL for redis in env as well.

Docker + Windows 10

I try work with docker on Windows 10 and I have 2 problems.
First:
if i try do
Dockerfile
FROM nginx
MAINTAINER Nikita rassamakhin "nrj.tomsk#gmail.com"
COPY /nginx-conf/nginx.conf /etc/nginx/nginx.conf
COPY /nginx-conf/vhost.conf /etc/nginx/sites-available/default
RUN mkdir -p /var/www/
RUN chown -R www-data:www-data /var/www/
RUN chmod -R 755 /var/www/
and this docker-compose
fpm:
container_name: php7
image: php:7.0.2-fpm
ports:
- "9000:9000"
links:
- mysql
nginx:
container_name: nginx
build: .
restart: always
ports:
- "80:80"
- "443:443"
links:
- fpm
mysql:
container_name: mysql
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root
i have 403 error on try open localhost.
if i try add app section in docker compose like this
app:
container_name: app
image: ubuntu:trusty
volumes:
- www/:/var/www
fpm:
container_name: php7
image: php:7.0.2-fpm
working_dir: "/var/www"
ports:
- "9000:9000"
links:
- mysql
volumes_from:
- app
nginx:
container_name: nginx
build: .
restart: always
ports:
- "80:80"
- "443:443"
links:
- fpm
volumes_from:
- fpm
mysql:
container_name: mysql
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root
app conteiner start and exit with code 0, without logs about error.
How i can fix this? What i doing wrong?
app conteiner start and exit with code 0, without logs about error.
You need to set a command or entrypoint for the app container. The default is probably bash which exits immediately. Exit code 0 means "exited with no error".

Resources