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

Viewed 2305

I am trying to containerize my Laravel project. I have the following services in my docker-compose.yml

  1. laravel_app
  2. database
  3. nginx
  4. 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

Screennshot of the error

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:
2 Answers

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