I started a new Laravel 9.x + JsonApi project on docker-compose with 2 containters, 'web' and 'mysql'. Artisan migrations, seeding and db:show works fine. But when I'm trying to load a page from browser or Postman, it returns DB error
`SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mariadb failed: Temporary failure in name resolution
select * from redirects where redirects.id = 1 limit 1 `
The weird part is that my DB host is mysql and 'mariadb' string is never mentioned in my code (except some vendor files), nor in docker files, nor in .env file. I also checked container environment variables - nothing there.
Artisan scripts work fine.
Here are my settings, if it helps. Mariadb was never mentioned, it was mysql from the very beginning.
docker-compose.yml
version: "3.4"
services:
mysql:
image: mysql
volumes:
- mysql_db:/var/lib/mysql
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=***
- MYSQL_DATABASE=url_shortener
web:
build:
dockerfile: Dockerfile.web.dev
context: .
#image: php:alpine
volumes:
- "./:/app"
- "/app/src/vendor"
- web_root_home:/root
working_dir: /app/src/
command: "php artisan serve --host=0.0.0.0 --port=7999"
ports:
- 8000:7999
depends_on:
- mysql
volumes:
mysql_db:
web_root_home:
Dockerfile.web.dev
#FROM php
FROM bitnami/laravel
WORKDIR /app/src/
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
ENV COMPOSER_ALLOW_SUPERUSER=1
RUN apt update
RUN apt install -y git build-essential vim mc mlocate
RUN pecl install xdebug
COPY ./src /app/src
COPY ./src/composer.json ./
COPY ./src/.env ./
RUN composer install
RUN php artisan key:generate
CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8000"]
.env (DB part)
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=url_shortener
DB_USERNAME=root
DB_PASSWORD=***
app/config/database.php wasn't changed
artisan db:show
root#a3c873713ee1:/app/src# php artisan db:show
MySQL 8 ...................................................................
Database .................................................... url_shortener
Host ................................................................ mysql
Port ................................................................. 3306
Username ............................................................. root
URL .......................................................................
Open Connections ........................................................ 3
Tables .................................................................. 6
Total Size ........................................................ 0.09MiB
Table .......................................................... Size (MiB)
redirects ............................................................ 0.02
failed_jobs .......................................................... 0.02
migrations ........................................................... 0.02
personal_access_tokens ............................................... 0.02
password_resets ...................................................... 0.02
users ................................................................ 0.02
migrations and seeding works fine, I can connect to mysql from the host laptop. All seeds data are there
I tried to clear config cache, didn't help
root#a3c873713ee1:/app/src# php artisan config:clear
INFO Configuration cache cleared successfully.
I also reloaded containers, no luck as well
Stack trace isn't useful error screenshot
I'm out of ideas what to try and where it takes this mariadb host at all.
I could solve the problem by defining the DATABASE_URL=mysql://root:PASSWORD#mysql/url_shortener in my .env file. I still have no idea where it got mariaDB host from.
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
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
may be somebody know how to resolve my issue?
If I use my existing app with wp-content and existing db, every time I've get redirected to the localhost:80 port. How to launch it at another port, maybe 8000 for example?
I have a Wordpress app Dockerfile with next lines:
FROM wordpress:latest
COPY ./src /var/www/html
ENV WORDPRESS_DB_PASSWORD mypass
ENV WORDPRESS_DB_NAME mydb
ENV WORDPRESS_DB_HOST mysql:3306
MySQL Dockerfile with existing db dump:
FROM mariadb:10.1.20
COPY dump/dump.sql /docker-entrypoint-initdb.d
ENV MYSQL_ROOT_PASSWORD mypass
CMD ["mysqld"]
And docker-compose.yml with that:
version: '2'
services:
mysql:
build: mysql/
restart: always
volumes:
- db_data:/var/lib/mysql
wordpress:
depends_on:
- mysql
build: wpapp/
ports:
- 8000:80
restart: always
volumes:
db_data:
Thanks for help everyone!
Override the database siteurl and homeurl vales with these lines in you wp-config.php:
define('WP_HOME', 'http://localhost:8000/');
define('WP_SITEURL', 'http://localhost:8000/');
After many tests I can recap how to solve this issue.
We need to change option_value of 2 rows in db table wp_options with option_names siteurl and home to http://localhost:8000 in this case.
We need to trigger dockers build to mysql container with volume.
docker-compose down -v
optional for clean docker cache
docker rm $(docker ps -aq)
docker volume rm $(docker volume ls -q)
docker rmi $(docker images -q)
this is not optional ofcourse
docker-compose up -d --build
The main thing is to clean browser cache manually, because it invokes redirect faster then cache clean if we use CMD + R for example in Safari and it seems like nothing is working after changes.
I was trying out Docker for the first time. Got a LEMP stack up and running, but I can't connect to the MYSQL Database. Not on my Symfony application, not on PHPMyAdmin. The applications are returning the following error code:
An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused
This is my docker-compose.yml:
nginx:
image: tutum/nginx
ports:
- "80:80"
links:
- phpfpm
volumes:
- ./nginx/default:/etc/nginx/sites-available/default
- ./nginx/default:/etc/nginx/sites-enabled/default
- ./logs/nginx-error.log:/var/log/nginx/error.log
- ./logs/nginx-access.log:/var/log/nginx/access.log
phpfpm:
build: phpfpm/
ports:
- "9000:9000"
volumes:
- ./public:/usr/share/nginx/html
mysql:
image: mariadb
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: admin
phpmyadmin:
image: phpmyadmin/phpmyadmin
restart: always
links:
- mysql
ports:
- 8183:80
environment:
MYSQL_USERNAME: admin
MYSQL_ROOT_PASSWORD: admin
PMA_ARBITRARY: 1
Dockerfile PHPFPM:
FROM php:fpm
RUN docker-php-ext-enable opcache
RUN apt-get update \
&& apt-get install -y --no-install-recommends libpq-dev \
&& docker-php-ext-install mysqli pdo_pgsql pdo_mysql
GitHub URL: https://github.com/MolengraafFrank/DockerSymfony
Could someone help me out? Thank you for your time.
The '[2002] Connection refused' means you can reach the database server, but you don't have right access for the user (in your case admin). By default mariadb have a root user with the password given by MYSQL_ROOT_PASSWORD and this user can connect from any server (%).
If you want use an over login to your databases, you have to create it in the databases server with the right granting on databases from chosen locations.
The problem here is that you have named your database server as 'mysql' (service name in the docker-compose file). But by default phpmyadmin tries to connect to a database server named 'db'. Adding PMA_HOST: mysql under the environment section of the phpmyadmin service will resolve this problem.
I think that MYSQL_USERNAME and PMA_ARBITRARY are useless if you work with default configuration (connection with root to your databases server)
I had this challenge because I am running 3 different containers with different IP Addresses
db - 172.18.0.3 - container for MYSQL
app - 172.18.0.2 - container for Laravel app
so I fixed it by editing my Laravel .env file
DB_CONNECTION=mysql
DB_HOST=172.18.0.3
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=username
...
To get your containers Ip addresses. From your docker host
docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
UPDATED:
For latest docker versions and based on the services name, "mysql", in your docker-compose.yml
mysql:
image: mariadb
ports:
- 3306:3306
You can try this:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=username
DB_HOST is the name of MySQL service name defined in docker-compose.yml
I've managed to connect to the mysql instance using mysql command line tool, this is the command I used - mysql -u root -p -h 127.0.0.1, and the entering the admin password. Is that a sufficient solution for you?
In my case I was running mysql in a docker container whose port was mapped to the host mac (3306:3306). I tried connecting to this database from a phpmyadmin docker container using 127.0.0.1 . But it won't work because the localhost on the phpmyadmin docker container does not have the required mysql running.
To connect to the host from docker network
docker.for.mac.host.internal
Docker Networking Docker Compose Networking
For my instance, the issue was with port mapping somehow.
In my case it was 3307:3306, as soon as I changed it to 3307 on the right side as well, I could connect to the DB instance.
Adding DB_READ_HOST=db solved this problem for me
If you want to know why your connexion failed, you can use in your terminal php artisan tinker and then
like
DB::connection()->getPdo();
Unfortunately this will only give you a part of the error.
Quit tinker and then use this command php artisan db will give you more information like database type, host, port, ad user about the issue.
Like this one
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (61)
Symfony\Component\Process\Exception\ProcessFailedException
The command "'mysql' '--host=127.0.0.1' '--port=3306' '--user=root' '--default-character-set=utf8mb4' 'laravel'" failed.
Exit Code: 1(General error)
Working directory: /Users/Dev/Documents/www/laravel_docker/src/addressAPI
Output:
================
Error Output:
================
at vendor/symfony/process/Process.php:270
266▕ */
267▕ public function mustRun(callable $callback = null, array $env = []): self
268▕ {
269▕ if (0 !== $this->run($callback, $env)) {
➜ 270▕ throw new ProcessFailedException($this);
271▕ }
272▕
273▕ return $this;
274▕ }
+14 vendor frames
15 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
This will not give you the answer on what is wrong, but at least you can understand why your config is wrong.
you need to link the phpfpm container to mysql.