phpMyAdmin error "No address associated with hostname" in Docker? - mysql

I am trying to containerize my Laravel project. I have the following services in my docker-compose.yml
laravel_app
database
nginx
phpmyadmin
All of them are running okay except PHPMyAdmin. It gets built and does serve the phpMyAdmin login page but I get the following errors when I enter the credentials.
Cannot log in to the MySQL server
mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: No address associated with hostname
mysqli::real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No address associated with hostname
Here is my docker-compose.yml snippet that includes my database service and phpmyadmin service
#The Database
database:
container_name: mysql_database
image: mysql:5.7
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=dev-db"
- "MYSQL_USER=phpmyadmin"
- "MYSQL_PASSWORD=phpmyadmin"
- "MYSQL_ROOT_PASSWORD=123456"
ports:
- 8991:3306
#PHPMyAdmin Service
phpmyadmin:
container_name: phpmyadmin
image: phpmyadmin/phpmyadmin
ports:
- "8992:80"
depends_on:
- database
environment:
- "PMA_HOST:database"
- "MYSQL_USER:phpmyadmin"
- "MYSQL_PASSWORD:phpmyadmin"
- "MYSQL_ROOT_PASSWORD:123456"
- "UPLOAD_LIMIT:3000000000"
# redis
cache:
image: redis:3.0-alpine
volumes:
dbdata:

For that to work, you need to at least create a bridge network and connect phpmyadmin and your db to it.
Something like this should work:
version: "3"
volumes:
dbdata:
networks:
backend:
driver: bridge
services:
database:
container_name: mysql_database
image: mysql:5.7
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=dev-db"
- "MYSQL_USER=phpmyadmin"
- "MYSQL_PASSWORD=phpmyadmin"
- "MYSQL_ROOT_PASSWORD=123456"
ports:
- 8991:3306
networks:
- backend
phpmyadmin:
container_name: phpmyadmin
image: phpmyadmin/phpmyadmin
ports:
- "8992:80"
depends_on:
- database
environment:
- "PMA_HOST=database"
- "MYSQL_USER=phpmyadmin"
- "MYSQL_PASSWORD=phpmyadmin"
- "MYSQL_ROOT_PASSWORD=123456"
- "UPLOAD_LIMIT=3000000000"
networks:
- backend
Also I do not recommend exposing DB port
EDITED: Fixed docker-compose file

Like Fominykh Maxim mentioned, my error was caused by syntax error while assigning environment value at phpmyadmin service. I had used PMA_HOST:database instead of PMA_HOST=database

Related

Docker / MySQL - Doctrine Migration - Access Database outside container

Hello :) here is my docker compose :
version: "3.8"
services:
web-server:
build: .
volumes:
- ./:/var/www/html/
ports:
- "8000:80"
links:
- "db:db"
db:
image: mysql:5.6
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "8001:80"
links:
- "db:db"
environment:
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: root
As you can see, when i go to phpmyadmin with http://localhost:8001/ i have :
PhpMyAdmin Dashboard
Doctrine Migation is installed in my project, so when i try :
./vendor/bin/doctrine-migrations status
Errors are :
In AbstractMySQLDriver.php line 112:
An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
In Exception.php line 18:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
In PDOConnection.php line 39:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
In PDOConnection.php line 39:
PDO::__construct(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
Connexion in my env file is :
BDD = pdo-mysql://root:root#db:3306/playlist_maker_multi
I expose port 3306 on my docker-compose so i don't understand why outside my container i can't access to my Database ?
Thanks everybody, if you have explanations.
Have a nice day.
Camille
Could you try running it with this config
I renamed the db service to mysql, explicitly added a network and made your php service depend on mysql, so it will restart untill it establishes a connection, just in case the php container starts running before the mysql container
version: "3.8"
services:
web-server:
build: .
volumes:
- ./:/var/www/html/
ports:
- "8000:80"
links:
- "mysql:db"
mysql:
image: mysql:5.6
container_name: db
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- net
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "8001:80"
links:
- "mysql:db"
depends_on:
- mysql
restart: on-failure
environment:
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: root
networks:
- net
networks:
net:
driver: bridge
For applications running on your host machine but outside of your Docker network (i.e. not containerised) that need to connect to a container, you can use localhost instead of db, as the DNS name db is only valid inside the Docker Compose network.
With:
mysql:
image: mysql:5.6
container_name: db
ports:
- "3306:3306"
you are creating a path from locahost:3306 (or MACHINEIP:3306 if on another machine) to the service on mysql:3306 inside the network.
#clarj, do you mean like that ?
version: "3.8"
services:
web-server:
build: .
volumes:
- ./:/var/www/html/
ports:
- "8000:80"
links:
- "localhost:db"
mysql:
image: mysql:5.7
container_name: db
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "8001:80"
links:
- "localhost:db"
environment:
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: root

Prisma 2 docker container can't connect MySQL Database container

I'm developing Backend server with prisma 2, MySQL, Docker-compose, GraphQL-Yoga server
My OS is Windows 10, docker compose use debian-openssl-1.1.x
problem is prisma 2 container can't connect MySQL container
docker-compose.yml
version: "3.7"
services:
mysql:
image: mysql:8.0.19
container_name: mysql
ports:
- 3306:3306
restart: always
environment:
MYSQL_DATABASE: $${MYSQL_DATABASE}
MYSQL_ROOT_PASSWORD: $${MYSQL_ROOT_PASSWORD}
volumes:
- /var/lib/mysql
prisma:
links:
- mysql
depends_on:
- mysql
container_name: prisma
ports:
- "5555:5555"
build:
context: back/prisma
dockerfile: Dockerfile
environment:
MYSQL_URL: $${MYSQL_URL}
MYSQL_ROOT_PASSWORD: $${MYSQL_ROOT_PASSWORD}
volumes:
- /app/prisma
backend:
links:
- mysql
depends_on:
- mysql
container_name: backend
ports:
- "4000:4000"
build:
context: back
dockerfile: Dockerfile
environment:
MYSQL_URL: $${MYSQL_URL}
FRONTEND_URL: $${FRONTEND_URL}
volumes:
- ./back:/app
- ./back/node_modules:/app/node_modules
- ./back/prisma:/app/prisma
frontend:
container_name: frontend
ports:
- "3000:3000"
build:
context: front
dockerfile: Dockerfile
environment:
BACKEND_URL: $${BACKEND_URL}
volumes:
- ./front:/app
- ./front/node_modules:/app/node_modules
.env
MYSQL_URL="mysql://root:prisma#mysql:3306/prisma"
BACKEND_URL=http://localhost:4000
FRONTEND_URL=http://localhost:3000
MYSQL_DATABASE:"prisma"
MYSQL_ROOT_PASSWORD:"prisma"
-schema.prisma
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "debian-openssl-1.1.x"]
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
.env in prisma folder
DATABASE_URL="mysql://root:prisma#mysql:3306/prisma"
when i run docker-compose (docker-compose up), prisma container (prisma 2 studio) show this logs
Error in Prisma Client request:
Error:
Invalid `prisma.user.count()` invocation:
Authentication failed against database server at `mysql`, the provided database credentials for `root` are not valid.
Please make sure to provide valid database credentials for the database server at `mysql`.
at PrismaClientFetcher.request (/root/.cache/prisma/studio/app/runtime/index.js:1:52273)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
enter image description here
I think this error occurred because MySQL container has a password different from MYSQL_ROOT_PASSWORD which I wrote in the doctor-compose.yml.
But, i think there seems to be no problem with the setup, why is this error happening?
And if i did not set up MYSQL_ROOT_PASSWORD, what is the default password value generated by MySQL container?
I searched for days to find the answer to this problem, but I could not get the answer. please give me some advices. thank you

Docker Compose phpmyadmin not connecting to MySQL

I have a docker compose file which is basically trying to build a WAMP/LAMP style environment.
Basically, PHPMyAdmin can't seem to connect to MySQL. Looking through answers here, it appeared that it was an issue with legacy auth using MySQL Image 8 so I added:
command: --default-authentication-plugin=mysql_native_password
but that didn't work, so I dropped down to mysql image5.7 and the issue is still present. For some reason, I can't connect to MySQL and I get the error:
mysqli_real_connect(): php_network_getaddresses: getaddrinfo failed: Try again
and
mysqli_real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Try again
Here is a copy of my Docker Compose which I don't think is doing anything weird.
Hoping someone can clarify that for me.
version: "3.1"
services:
site:
build: .
ports :
- "80:80"
volumes:
- ./www:/var/www/html/
links:
- database
networks:
- php-network
#######################################
# PHP MY ADMIN
#######################################
phpmyadmin:
build:
context: .
dockerfile: PHPMYADMIN.Dockerfile
restart: always
links:
- database:mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- "8080:80"
environment:
- MYSQL_USERNAME=admin
- MYSQL_PASSWORD=root
networks:
- php-network
#######################################
# MySQL server
#######################################
database:
image: mysql:5.7.25
ports:
- "3306:3306"
container_name: db-mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=test_db
- MYSQL_USER=admin
- MYSQL_PASSWORD=root
networks:
- php-network
networks:
php-network:
driver: bridge
I also saw mention of a "depends on" flag which I tried too but that also failed to allow me to connect. I have had the same error throughout.
It's about the container name.
database:
image: mysql:5.7.25
ports:
- "3306:3306"
- **container_name: db-mysql**
and in your phpcontainer:
phpmyadmin:
build:
context: .
dockerfile: PHPMYADMIN.Dockerfile
restart: always
links:
- database:mysql
environment:
**PMA_HOST: mysql**
PMA_PORT: 3306
you define host as mysql, which in docker network will be unrecognizable.
try switching PMA_HOST to db-mysql.
Sorry for bad formatting.
Also use docker ps to see docker container names and to figure out which hosts do you need to connect.
I've got this working now with the following:
version: "3.1"
services:
www:
build: .
ports:
- "8081:80"
volumes:
- ./www:/var/www/html/
links:
- db
networks:
- default
#######################################
# MySQL server
#######################################
db:
image: mysql:5.7.25
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: test_db
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump:/docker-entrypoint-initdb.d
- persistent:/var/lib/mysql
networks:
- default
#######################################
# PHP MY ADMIN
#######################################
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8080:80
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
persistent:
According to your phpmyadmin service configuration, you are trying to reach mysql container by mysql address:
environment:
PMA_HOST: mysql
PMA_PORT: 3306
But container with mysql server is accessible by database or db-mysql addresses.
So, you need to change phpmyadmin service configuration to:
environment:
PMA_HOST: database
PMA_PORT: 3306

Incorrect docker compose on phpmyadmin

According to the docker compose yaml,
version: '3'
services:
db:
image: mariadb:latest
container_name: mariadb
restart: always
volumes:
- ./mysql/initdb/:/docker-entrypoint-initdb.d
- ./mysql/data/:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=root-pwd
- MYSQL_DATABASE=appdb
- MYSQL_USER=appuser
- MYSQL_PASSWORD=user-pwd
php:
image: php:fpm-alpine
container_name: php
restart: always
volumes:
- ./www/:/var/www/html
expose:
- "9000"
nginx:
image: nginx:alpine
container_name: nginx
restart: always
volumes:
- ./nginx/conf/nginx.conf:/etc/nginx/conf/nginx.conf:ro
- ./nginx/conf.d:/etc/nginx/conf.d:ro
- ./www:/var/www/html
ports:
- "80:80"
pma:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
restart: always
ports:
- "8080:80"
it produces an error:
#2002 - php_network_getaddresses: getaddrinfo failed: Try again — The server is not responding (or the local server's socket is not correctly configured).
when logging in to phpMyadmin (http://127.0.0.1:8080).
I have tried to fix this problem, but nothing works.
Could you please find a solution?
Any recommendations/comments are welcomed here.
I had a similar problem try this in the links directive - container-name:db as below
links:
- mariadb:db
you should links your phpmyadmin to database services in order to use the db service
pma:
image: phpmyadmin/phpmyadmin
links:
- db
container_name: phpmyadmin
restart: always
ports:
- "8080:80"
environment:
- PMA_HOST: maria_db
here is more about docker compose services links
If you set the following environment variable in docker compose:
pma:
environment:
- PMA_ARBITRARY=1
then you will be given the opportunity to enter the host server manually on the login screen. Enter the name of the database service there. In your case the name of the service is db

Wordpress not work with docker compose

This is my docker-compose.yml
version: '2'
services:
wordpress:
image: wordpress:4.6.1-php5.6-apache
container_name: wordpress
volumes:
- ./projects/:/home/docker/
working_dir: /home/docker/
ports:
- "8000:80"
environment:
WORDPRESS_DB_PASSWORD: secret
links:
- database-mysql
database-mysql:
image: mysql:5.7
container_name: mysqldb
ports:
- "3306:3306"
volumes:
- ./backups/mysqldb/:/var/lib/mysql/
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: wordpress
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: phpmyadmin
ports:
- "8080:80"
environment:
PMA_USER: root
PMA_PASSWORD: secret
PMA_HOST: database-mysql
links:
- database-mysql
When I run: docker-compose up, the log error shows:
MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo failed: Name or service not know
Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 19
Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 19
2016-11-11 04:14:33,648 INFO success: php-fpm entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2016-11-11 04:14:33,648 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
What am I doing wrong?
is's too long to comment, therefore i create another answer
try this and wait for 2 minutes, after that access localhost:8000
version: '2'
services:
db:
image: mysql:5.7
volumes:
- "./.data/db:/var/lib/mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
links:
- db
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: wordpress
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: phpmyadmin
ports:
- "8080:80"
environment:
PMA_USER: root
PMA_PASSWORD: secret
PMA_HOST: database-mysql
links:
- db
On Windows 10 Home edition (so using docker-toolbox) I also ran into this problem.
It seems that the wordpress image now required the environment setting:
WORDPRESS_DB_HOST: db:3306.
After adding this, the wordpress container can connect to the database.
Also with Windows 10 Home edition I can't get the port mapping to work for localhost. I need to lookup the ip-adress via docker-machine ip and connect to the port there.
On Linux Mint (based on Ubuntu) this does not seem required.
So I expect this to be an installation problem with docker-toolbox or Windows 10 Home Edition.