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
Related
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
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
Losing my mind on this one.
I've got a Lumen and MySQL setup in a Docker container. Most everything is good to go. I can run the container and access Lumen through a browser. I can access MySQL through Sequel Pro, no problem. And I can run php artisan migrate and it works fine.
But if I try to do anything through Lumen in the browser, it won't connect to the database, and it gives me the Connection refused error.
I'm using Lumen 5.7.7 and .env file looks like this:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=root
docker-compose.yml looks like this:
version: '3'
services:
api:
build:
context: .
dockerfile: .docker/Dockerfile
image: laravel-docker
ports:
- 8080:80
depends_on:
- mysqldb
volumes:
- .:/srv/app
# container_name:
mysqldb:
image: mysql:5.7
container_name: mysqldb
command: mysqld --user=root --verbose
volumes:
- ./schemadump.sql:/docker-entrypoint-initdb.d/schemadump.sql
ports:
- 3306:3306
environment:
MYSQL_DATABASE: test
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: root
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
EDIT: Ran phpinfo() on the site and on the command line and realized that they aren't even the same version, let alone the same exact details. Could that be the problem here? Looking at it more...
Well, it is working now, sort of. I've got it now working from the browser and not from the command line, which I can work with. From a comment above, it looks like a missing "link" section connecting the api to the database was the issue. .env file is unchanged, but the docker-compose.yml now looks like this:
version: '3'
services:
api:
build:
context: .
dockerfile: .docker/Dockerfile
image: laravel-docker
ports:
- 8080:80
links:
- mysql
volumes:
- .:/srv/app
environment:
DB_HOST: mysql
DB_DATABASE: test
DB_USERNAME: test
DB_PASSWORD: test
mysql:
image: mysql:5.7
ports:
- 13306:3306
environment:
MYSQL_DATABASE: test
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: root
Added the "environment" section to the api section of the yml file. That helped out. And changed the port so it has a different external port compared to the internal port. Not 100% which part made it work right, but it is working OK for now and I'm not about to change it more.
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