Pascal Pascal - 22 days ago 11
MySQL Question

[Docker]: Connecting PHPMyAdmin to MySQL doesnt work

I'm trying to connect a PHPMyAdmin-Container to a MySQL-Container to view the databases.

I have started the MySQL container via

$ docker run --name databaseContainer -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql


and the PHPMyAdmin-Container via
$ docker run --name myadmin -d --link databaseContainer:mysql -p 8080:8080 phpmyadmin/phpmyadmin


When trying to login on PHPMyAdmin, I get:
mysqli_real_connect(): php_network_getaddresses: getaddrinfo failed: Name does not resolve

and

mysqli_real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name does not resolve

By the way, I have also started a wordpress container and also linked it to mysql, there it works...

Answer

Instead of starting them one by one, use docker compose.

Create a docker-compose.yml file

version: '2'
services:
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD:my-secret-pw
    ports:
      # just if you also want to access it directly from you host
      # node neede for phpmyadmin
      - "3306:3306"
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    depends_on:
      - db
    ports:
      - "8080:8080"

Then start it using docker-compose up in the same folder your docker-compose.yml file is located. When phpmyadmin opens, type 'db' as the hostname, since that is the name of the service. All the links are setup for you and it will work out of the box.

Thats much sipler - docker run overcomplicates things and is not practical for those things - never.

Hint: if docker-compose is not installed for you, install it using this official howto https://docs.docker.com/compose/install/