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
Related
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
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
In my container when I run php artisan migrate I keep getting this error
In Connector.php line 67:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
In Connector.php line 67:
PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known
Here's my .ENV file
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:etukbJpSLgbRsdf5uOEGtLT5Qw+XB6y06Q38HPr
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://127.0.0.1:8000/
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=project
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
I tried to change DB_HOST to mysql but still same result as well as changed it to mariadb
Here's my docker-compose file
version: '3'
services:
# The Application
app:
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ../.:/var/www
- ./custom.ini:/usr/local/etc/php/conf.d/custom.ini
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
# The Web Server
web:
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www
volumes:
- ../.:/var/www
- ./vhost.conf:/etc/nginx/conf.d/default.conf
ports:
- 8082:80
# The Database
database:
image: mysql:5.7
volumes:
- dbdata:/var/lib/mysql
environment:
MYSQL_USER: ${DB_USERNAME}
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
ports:
- "33061:3306"
volumes:
dbdata:
I searched for solution online but none of them helped. Does anyone have a suggestion as to why I keep getting that error?
You should add a container_name to your database service
database:
image: mysql:5.7
container_name: database
And reference that name inside your .env MYSQL_HOST as:
MYSQL_CONNECTION=mysql
MYSQL_HOST=database
MYSQL_PORT=3306
rather than using localhost
you should also connect the database service and app using a network
add a networks block to the end of the docker-compose
networks:
app-network:
driver: bridge
and add this block to the end of your database serice and app servce
demo_db:
.
.
.
networks:
- app-network
app:
.
.
.
networks:
- app-network
here is a working demo for dockerizing a simple laravel app
https://bitbucket.org/RamiAmro/hello-laravel-docker/
It happens when the laravel application running in docker is not able to connect to database. I hope this could be helpful for those who have gone through the solution and not yet succeeded. I had similar problem and tried to change the DB_HOST localhost to 127.0.0.1 also with the name of the container/db host (as mentioned in docker-compose), yet I could not get this error resolved. So I updated my .env file like this:
DB_CONNECTION=mysql
DB_HOST=host.docker.internal
DB_PORT=3308 or any other port
DB_DATABASE=your_database_name
DB_USERNAME=your_user_name
DB_PASSWORD=your_password
which started working. I am not assuring it works 100% but you could give a try for this in case other option are not working.
Cheers!
I solved it by DB_HOST=0.0.0.0 in ENV file, because Docker works on that port.
If you are connecting to mysql as an external docker container, make sure that you implemented this solution https://stackoverflow.com/a/38089080/1770571
I had the same problem. In my case I found out that it was visible in the sail log that there was a problem with recovering the InnoDB database
!! CAUTIOUS (THIS WILL DELETE THE WHOLE DATABASE) !!
As it was my development environment, I tried deleting the existing volume
./vendor/bin/sail down -v
And then bringing up a new container again
./vendor/bin/sail up
There's now Laravel Sail for anyone still interested.
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 had the same issue and I found in the doc that when using Sail you should run all commands using sail instead of php. for example "php artisan migrate" => "sail artisan migrate".
for more insight about this:
https://laravel.com/docs/9.x/sail#executing-sail-commands
I'm running Laravel 5.4 in Docker. This is my docker-compose.yml file:
version: '2'
services:
app:
container_name: laravel_app
image: webdevops/php-apache-dev:ubuntu-16.04
links:
- mysql
depends_on:
- mysql
ports:
- 8888:80
volumes:
- .:/app
environment:
docker: 'true'
WEB_DOCUMENT_ROOT: '/app/public'
WEB_NO_CACHE_PATTERN: '\.(.*)$$'
working_dir: '/app'
mysql:
image: mariadb:latest
ports:
- 8889:80
environment:
MYSQL_ROOT_PASSWORD: 'dev'
MYSQL_DATABASE: 'dev'
MYSQL_USER: 'dev'
MYSQL_PASSWORD: 'dev'
This is the relevant part of my .env file:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=8889
DB_DATABASE=dev
DB_USERNAME=dev
DB_PASSWORD=dev
I am able to see the Laravel welcome page - that side of things works. But when I run php artisan migrate I get this error:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = dev and table_name = migrations)
I have tried fiddling with the host and port parameters in the .env file.
this worked for me:
put the name of my mysql container instead of 127.0.0.1
NAME CONTAINERS
project-db --> container mysql
project-app --> container laravel
DB_CONNECTION=mysql
DB_HOST=project-db
DB_PORT=3306
DB_DATABASE=project
DB_USERNAME=root
DB_PASSWORD=root
First edit your docker-compose.yml.
mysql:
image: mariadb:latest
ports:
- 8889:3306
After that set the correct DB port in .env.
Your DB port is wrong. You are trying to connect the exposed port inside the docker. In this case you should use DB_PORT=3306 in your .env.
Try change port to 3306 and use DB_HOST=localhost to yourdomain.com(your IP)
And don't forget Sudo Clear cache and config cache
DB_HOST=My_ip_for_virtual_machine (yourdomain.com)
sudo docker-compose exec app php artisan config:clear
sudo docker-compose exec app php artisan cache:clear
After 5 hours researching I found this quote in comments:
Remove port-exposing, mariadb:latest sets it in its Dockerfile: This image exposes the standard MySQL port (3306)...
This mean you MUST use port 3306 in .env of each Laravel projects, either you define port for Mysql(or mariaDB).
This mean in host only port 3306 connect your Laravel to database.
If you don't want to set the DB port like in #kotapeter's answer, you can instead invoke artisan through docker by running:
$ docker exec -it <name of container> php artisan migrate
Looking at your docker-compose.yml, your container could be laravel_app_app_1 but you can check this by running docker ps
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