Connection refused when running docker-compose exec app php artisan migrate - laravel

I'm getting PDOException::("SQLSTATE[HY000] [2002] Connection refused") when running docker-compose exec app php artisan migrate on the terminal
here's my db config on .env file
DB_CONNECTION=mysql
DB_HOST=172.20.0.4
DB_PORT=3306
DB_DATABASE=queue_db
DB_USERNAME=root
DB_PASSWORD=root
I also inspect the container ip
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' worker-db
Output: 172.20.0.4
here's my docker-compose.yml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: ./docker/Dockerfile
image: digitalocean.com/php
container_name: worker-app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/config/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: worker-webserver
restart: unless-stopped
tty: true
ports:
- "8090:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker/config/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql
container_name: worker-db
# restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: queue_db
MYSQL_ROOT_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
There's a weird error on the stack trace where ip is different i'm not sure why it's getting a different ip, but in my .env file i'm using the container ip which is "172.20.0.4"
Exception trace:
1 PDOException::("SQLSTATE[HY000] [2002] Connection refused")
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
2 PDO::__construct("mysql:host=172.20.0.3;port=3306;dbname=queue_db", "root", "root", [])
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
Thanks in advance.

ok, after i tried to run php artisan config:clear it's working now.

Related

How to solve [Undefined index: name] in Laravel-docker

I am trying to execute Laravel with docker on Ubuntu 20.04
At first, I installed docker and created docker-compose.yml so on.
version: "3.7"
services:
app:
build:
args:
user: sammy
uid: 1000
context: ./
dockerfile: Dockerfile
image: travellist
container_name: travellist-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- travellist
db:
image: mysql:5.7
container_name: travellist-db
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker-compose/mysql:/docker-entrypoint-initdb.d
networks:
- travellist
nginx:
image: nginx:alpine
container_name: travellist-nginx
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d/
networks:
- travellist
networks:
travellist:
driver: bridge
docker-compose exec app composer install
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> #php artisan package:discover --ansi
In PackageManifest.php line 122:
Undefined index: name
Script #php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
when I entered this command, I have got some error "undefined index: name"
If you can solve this error, please let me know.
Thanks.

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

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

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 .

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 ENTRYPOINT bash script is executed over and over again

Everything in my docker container initialisation goes well except when I run an ENTRYPOINT script at the end of my Dockerfile with
# ...
ENTRYPOINT ["bash", "./shell_scripts/init.sh"]
which consists of
#!/bin/bash
echo "Init app..."
composer update
composer dump-autoload
php artisan migrate
and when I run docker-compose up --build it keeps running the script over and over again....
docker-compose.yml
version: '3.7'
services:
mysql_db:
image: mysql:8.0.13
container_name: mysql_8.0.13
command: --default-authentication-plugin=mysql_native_password
restart: unless-stopped
tty: true
ports:
- 3307:3306
environment:
SERVICE_TAGS: dev
SERVICE_NAME: mysql
MYSQL_ROOT_PASSWORD: mypass
networks:
- app-network
app_n_php:
build:
context: .
dockerfile: Dockerfile
container_name: app_php_7.3-rc-fpm
volumes:
- type: bind
source: ./app
target: /var/www/app
restart: unless-stopped
tty: true
ports:
- 8001:8000
depends_on:
- mysql_db
environment:
SERVICE_NAME: app_n_php
SERVICE_TAGS: dev
networks:
app-network:
driver: bridge
Any idea what is going on?

Resources