Home > Back-end >  Docker / MySQL - Doctrine Migration - Access Database outside container
Docker / MySQL - Doctrine Migration - Access Database outside container

Time:10-21

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

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

@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