Docker / MySQL - Doctrine Migration - Access Database outside container - mysql

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

Related

Docker MariaDB, phpmyadmin mysqli::real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name does not resolve

So I wanted to update my stack with phpmyadmin today and I cam constantly getting these errors:
mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: Name does not resolve
mysqli::real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name does not resolve
docker-compose.yml:
version: "3.8"
services:
loadbalancer:
image: nginx
ports:
- "80:80"
- "443:443"
networks:
- frontend
- backend
deploy:
mode: global
volumes:
- ./config/nginx.conf:/etc/nginx/nginx.conf
- ./cert/:/home/pi/cert/
web-dotdotprint:
image: obdotking/dot_php_apache:latest
ports:
- "8080:80"
networks:
- backend
deploy:
mode: global
volumes:
- ./code/DOTDOTPRINT:/var/www/html/
- ./config/DOTDOTPRINT/php.ini:/usr/local/etc/php/conf.d/custom.ini
- ./config/apache2.conf:/etc/apache2/apache2.conf
- ./sessions_php_nfs/DOTDOTPRINT:/home/pi/sessions_php_nfs/DOTDOTPRINT/
phpmyadmin:
image: jackgruber/phpmyadmin
ports:
- "8082:80"
networks:
- backend
environment:
- PMA_HOST = mariadb
- PMA_PORT = 8081
depends_on:
- mariadb
deploy:
placement:
constraints:
- node.hostname==dot1
mariadb:
image: linuxserver/mariadb:arm32v7-latest
environment:
MYSQL_ROOT_PASSWORD: Cre8M8B42L
ports:
- "8081:80"
networks:
- backend
volumes:
- ./data:/config/databases
deploy:
placement:
constraints:
- node.hostname==dot5
networks:
frontend:
backend:
Note that this is a raspberry pi system so arm32v7 versions of dockerhub images are used.
phpmyadmin is not from the official image but one compiled from a 3rd party for support of arm devices
TIA

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

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

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

WordPress docker compose won't start mysql

I am trying to set up a docker container for WordPress development using docker-compose.yml which looks like this:
version: '3'
services:
db:
image: mysql:5.7
volumes:
- data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: wordpress
MYSQL_USER: root
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- 8080:80
- 443:443
volumes:
- ./data:/data # Required if importing an existing database
- ./:/var/www/html # Theme development
environment:
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: root
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:80
volumes:
data: {}
And I'm getting errors like this:
wordpress_1 | Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 22
wordpress_1 |
wordpress_1 | Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 22
wordpress_1 |
wordpress_1 | MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo failed: Name or service not known
I tried clearing my images and containers but no luck. Any idea why this happens?
Try providing links option for both wordpress and phpmyadmin.
links:
- db:mysql
Edit: Check this question. Add mysql to the db. I don't if this solves, I have to try out once I go home later.

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.