phpmyadmin in docker-compose

Viewed 914

I dockerized a Laravel project and I want to run PhpMyAdmin while running beside MySQL.

My problem is I can't login PhpMyAdmin in PhpMyAdmin page.

Here is the error: mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known.

This is the content of my docker-compose.yml file:

version: '3'
networks:
  laravel:

services:
  mysql:
    image: mysql:5.7.22
    container_name: mysql
    tty: true
    restart: unless-stopped
    ports:
      - "4506:3306"
    volumes:
      - ./mysql:/var/lib/mysql
    environment:
      MYSQL_DATABASE: homestead
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_TAGS: dev
      SERVICE_NAME: mysql
    networks:
      - laravel
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    depends_on:
      - mysql
    ports:
      - 8181:80
    environment:
      MYSQL_USERNAME: homestead
      MYSQL_ROOT_PASSWORD: secret
    networks:
      - laravel

What am I missing?

1 Answers

You need to specify the PMA_HOST environment variable to tell phpmyadmin where to find the mysql server (in this case, the hostname is simply the service name, mysql):

version: '3'
networks:
  laravel:

services:
  mysql:
    image: mysql:5.7.22
    container_name: mysql
    tty: true
    restart: unless-stopped
    ports:
      - "4506:3306"
    volumes:
      - ./mysql:/var/lib/mysql
    environment:
      MYSQL_DATABASE: homestead
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_TAGS: dev
      SERVICE_NAME: mysql
    networks:
      - laravel
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    depends_on:
      - mysql
    ports:
      - 8181:80
    environment:
      PMA_HOST: mysql
      MYSQL_USERNAME: homestead
      MYSQL_ROOT_PASSWORD: secret
    networks:
      - laravel
Related