Laravel cannot connect to dockerise database - php

I created three containers (PhP-NGINX-MySql) to support default laravel project located on host machine.
When I try to connect to DB from laravel I get error:
Route::get('/', function () {
dd(App\User::all());
return view('welcome');
});
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed:
Name or service not known (SQL: select * from users)
Here is my .env in laravel-5.3.16
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=33061
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
and ansible-playbook:
---
- hosts: localhost
environment:
PYTHONPATH: /usr/local/lib/python2.7/site-packages/
tasks:
- name: get currernt location
command: pwd
register: my_way
- set_fact: host_dir="{{my_way.stdout}}"
- name: create image with nginx
docker_image:
path: /home/demaunt/Jun/dock_click/engie
dockerfile: engie.dockerfile
name: engie_image
- name: create image with php
docker_image:
path: /home/demaunt/Jun/dock_click/piha
dockerfile: piha.dockerfile
name: piha_image
- name: run piha container
docker_container:
name: piha_cont
image: piha_image
volumes:
- "/home/demaunt/Dockjun/laravel-5.3.16:/var/www/wapclick"
links:
- diba_cont:db
env:
DB_PORT: 3306
DB_HOST: database
- name: run engie container
docker_container:
name: engie_cont
image: engie_image
volumes_from:
- piha_cont
ports:
- "8080:80"
links:
- piha_cont:app
- name: run diba container
docker_container:
name: diba_cont
image: mysql:5.6
env:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
ports:
- 33061:3306
What is more strange is that when I run php artisan migrate I get succesfull mesage:
Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table
Here are containers running:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e926382db347 engie_image "nginx -g 'daemon ..." 19 seconds ago Up 18 seconds 443/tcp, 0.0.0.0:8080->80/tcp engie_cont
c9563c839e45 piha_image "docker-php-entryp..." 19 seconds ago Up 18 seconds 9000/tcp piha_cont
5de541049da2 mysql:5.6 "docker-entrypoint..." 20 seconds ago Up 19 seconds 0.0.0.0:33061->3306/tcp diba_cont

Try "diba_cont" as DB_HOST that should do the job. Docker declare automatically a DNS rule with the name of the container to contact the container

Related

Laravel env configuration on Docker using MySQL

I don't know the technical terms of the LAMP stack very well so I will try to explain myself as much as I can. I have my project in my local Ubuntu (running on Windows 10 pro). I ran docker from WSL and I edited the default Laravel Sail docker-compose file.
my env file is like this:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=new_cms_system_db
DB_USERNAME=root
DB_PASSWORD=
and this is my docker-compose MySQL part:
db:
image: 'mysql:5.7'
container_name: db
ports:
- '3306:3306'
environment:
MYSQL_DATABASE: mysql
MYSQL_ALLOW_EMPTY_PASSWORD: 1
MYSQL_USER: sail
MYSQL_PASSWORD: 123
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
if I use "DB_HOST" as "db," then I have got no problems reaching the database from the website but then I can't use "PHP artisan migrate" as I get SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known error.
So I change "DB_HOST" to 0.0.0.0, then I can migrate but I can't reach the database from the website.
What should I do to solve this problem so I can both reach the database from the terminal and the website?
I solved my problem with the following command:
docker exec -it <name of container> php artisan migrate
In this case, I used my main container's name and it worked. I kept DB_HOST as "db" as I think it should be.
I can reach my database from PHPMyAdmin. I added it to docker-compose like this:
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin
ports:
- 8200:80
environment:
PMA_HOST: db
MYSQL_USER: root
networks:
- sail

Database connection refused for Laravel artisan command with Docker

I am trying to run Laravel using Docker locally. Everything works fine expect the database. This is how my docker-compose looks like:
services:
app:
build:
context: ./
dockerfile: ./docker/Dockerfile
container_name: my-app
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- my-network
db:
image: mysql:8.0
container_name: my-db
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
ports:
- 3307:3306
volumes:
- ./docker/mysql:/docker-entrypoint-initdb.d
networks:
- my-network
nginx:
image: nginx:alpine
container_name: my-nginx
ports:
- 8001:80
volumes:
- ./:/var/www
- ./docker/nginx/nginx.conf:/etc/nginx/conf.d
networks:
- my-network
My .env file looks like this:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=admin
DB_PASSWORD=
When I run php artisan migrate:fresh within the app container, I am getting the following message:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
Can someone see what is the problem here?
UPDATE: When I run docker-compose ps I can see
NAME COMMAND SERVICE STATUS PORTS
my-app "docker-php-entrypoi…" app running 9000/tcp
my-db "docker-entrypoint.s…" db restarting
my-nginx "/docker-entrypoint.…" nginx running 0.0.0.0:8001->80/tcp, :::8001->80/tcp
This is the log when I run docker-compose logs db
my-db | 2021-07-29T20:18:37.340769Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.20) initializing of server in progress as process 104
my-db | 2021-07-29T20:18:37.354423Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
my-db | 2021-07-29T20:18:37.354605Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
my-db | 2021-07-29T20:18:37.356184Z 0 [ERROR] [MY-010119] [Server] Aborting
my-db | 2021-07-29T20:18:37.358746Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.20) MySQL Community Server - GPL.

Docker - Nginx, PHP, MySQL - Laravel artisan migrate connection refused

I have Laravel running in Docker with 3 containers - Nginx, MySQL 8 And PHP 8.
I have the following docker-compose.yaml
version: '3.8'
services:
server:
build:
context: .
dockerfile: dockerfiles/nginx.dockerfile
ports:
- '8000:80'
volumes:
- ./src:/var/www/html
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
container_name: server
depends_on:
- app
- db
app:
build:
context: .
dockerfile: dockerfiles/php.dockerfile
volumes:
- ./src:/var/www/html:delegated
container_name: app
db:
image: mysql:8.0
env_file:
- ./env/mysql.env
container_name: db
This command:
docker-compose up -d server
Launches 3 containers:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ff5cbefd12e toolkit_server "/docker-entrypoint.…" 9 seconds ago Up 3 seconds 0.0.0.0:8000->80/tcp server
c7f3c9753909 toolkit_app "docker-php-entrypoi…" 14 seconds ago Up 8 seconds 9000/tcp app
d7b421bd3c3b mysql:8.0 "docker-entrypoint.s…" 14 seconds ago Up 9 seconds 3306/tcp, 33060/tcp db
I want to run php artisan migrate, so I try it like so:
docker-compose exec app php artisan migrate
This gives me the following error:
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = toolkit and table_name = migrations and table_type = 'BASE TABLE')
My mysql.env file is like so:
MYSQL_DATABASE=toolkit
MYSQL_USER=root
MYSQL_PASSWORD=password
MYSQL_ROOT_PASSWORD=password
AND .env:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=toolkit
DB_USERNAME=root
DB_PASSWORD=password
I've tried changing ports, host, but I just can't get access - I must have messed something up somewhere.
please try this in the app Section of the docker-compose.yml
links:
- db
In my haste, I was trying to run commands before mysql was ready. It can take 90 seconds after a rebuild for the container to accept connections.
docker logs db
Once it was ready to connect, my commands would run.

Docker + Laravel issue [SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution]

I have docker installed on ubuntu machine and I'm trying to run a laravel app.
MySQL service has service_name: mysql in docker-compose.yml file and .env file has DB_HOST=mysql.
As I remember .env file should figure out that DB_HOST=mysql points to the mysql docker service IP. However this isn't happening and after running migrations I get:
Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
First I ran docker-compose build, after which I ran docker-compose up -d and all of my 3 services are up and running.
If I extract the IP of MySQL service and use it in .env file like this:
DB_HOST=172.18.0.2
I can then run migrations successfully and in this case everything works fine.
However, I consider this as bad practice since IP address could be changed if MySQL service is restarted. Am I missing something here, why using service_name in my .env file for DB_HOST fails resolving db host name?
docker-compose.yml:
version: '3'
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:5.7.22
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: laraone
MYSQL_USER: laraone_user
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src:/var/www/html
depends_on:
- mysql
ports:
- "9000:9000"
networks:
- laravel
.env:
APP_NAME=Laraone
APP_ENV="local"
APP_KEY=base64:PMwGrcSu2ioPEj75dv5gcdWAogESOtt8UCr/gs0nOtw=
APP_DEBUG=true
APP_URL=http://localhost:8080
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laraone
DB_USERNAME=laraone_user
DB_PASSWORD=secret
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=
MAIL_FROM_ADDRESS=noreply#example.com
MAIL_FROM_NAME="${APP_NAME}"
MAIL_SENDMAIL="/usr/sbin/sendmail -bs"
I think you try to run "php artisan migrate" command outside php docker container.
You should run php artisan migrate command inside php container.
Try following;
docker ps --> list running containers
docker exec -it <your_container_id> bash
and now you can run;
php artisan migrate
Edit:
You can also write artisan command without entering container bash as following;
docker exec -i <your_container_id> php artisan migrate
For anyone still wanting a dockerized Laravel enviroment, check out Laravel Sail. It lets you route commands to the docker instance easily with an alias sail. Like sail artisan migrate.
https://laravel.com/docs/8.x/sail
Laravel Sail is a light-weight command-line interface for interacting
with Laravel's default Docker development environment. Sail provides a
great starting point for building a Laravel application using PHP,
MySQL, and Redis without requiring prior Docker experience.
I resolved the issue by installing laravel app inside a php container. Simple 1-line command which helped me solve this problem: docker exec -it php php artisan app:install
Similar issue, with console command I created, with docker containers to mysql and php
(just entrypoint for dev runnig php artisan serve --host 0.0.0.0)
and I tried to run on host
php artisan my:console:command
which return :
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution (SQL: select * from `mytable` limit 5)
By running :
docker exec -it myPhpContainerName php artisan my:console:command
it works fine
SOLUTION 1:
Always run docker ps to check if your ports are well mapped. below you can see what I mean.
to fix this you need to check if you have another instance of MySQL running with this port number and stop it or try a different port
SOLUTION 2:
change your credentials to
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=
here is mysql yml

Laravel database migration failed using laradock

I am getting the following error
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = testdb and table_name = migrations)
when i run -
php artisan migrate
I am running this command on laradock workspace. I entered the workspace using the following command.
docker-compose exec workspace bash
I am using laravel 5.5. I have laradock inside my project folder in following way.
+testproject
-Laradock
My project .env (testproject/.env) file contains the following settings for mysql.
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=33060
DB_DATABASE=testdb
DB_USERNAME=root
DB_PASSWORD=root
My laradock .env(testproject/laradock/.env) file contains the following settings for mysql.
MYSQL_VERSION=8.0
MYSQL_DATABASE=testdb
MYSQL_USER=default
MYSQL_PASSWORD=secret
MYSQL_PORT=33060
MYSQL_ROOT_PASSWORD=root
MYSQL_ENTRYPOINT_INITDB=./mysql/docker-entrypoint-initdb.d
My docker-compose.yml contains the following settings for Mysql Container
mysql:
build:
context: ./mysql
args:
- MYSQL_VERSION=${MYSQL_VERSION}
environment:
- MYSQL_DATABASE={MYSQL_DATABASE}
- MYSQL_USER={MYSQL_USER}
- MYSQL_PASSWORD={MYSQL_PASSWORD}
- MYSQL_ROOT_PASSWORD={MYSQL_ROOT_PASSWORD}
- TZ=${WORKSPACE_TIMEZONE}
volumes:
- ${DATA_SAVE_PATH}/mysql:/var/lib/mysql
- ${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
ports:
- "${MYSQL_PORT}:3306"
networks:
- backend
Mysql in docker is installed in port 33060. I am trying laradock for the first time. After trying several configurations now i am calling out for Avengers !!!
solved the problem by having the following mysql settings.
testproject/.env:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=testdb
DB_USERNAME=root
DB_PASSWORD=root
testproject/laradock/.env:
MYSQL_VERSION=8.0
MYSQL_DATABASE=default
MYSQL_USER=default
MYSQL_PASSWORD=secret
MYSQL_PORT=33060
MYSQL_ROOT_PASSWORD=root
MYSQL_ENTRYPOINT_INITDB=./mysql/docker-entrypoint-initdb.d
testproject/laradock/docker-compose.yml
mysql:
build:
context: ./mysql
args:
- MYSQL_VERSION=${MYSQL_VERSION}
environment:
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- TZ=${WORKSPACE_TIMEZONE}
volumes:
- ${DATA_SAVE_PATH}/mysql:/var/lib/mysql
- ${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
ports:
- "${MYSQL_PORT}:3306"
networks:
- backend
I also ran the following two commands in laradock folder
docker-compose down
docker-compose up -d mysql

Categories