Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connect to mysql container from another container

I'm trying to connect to a mysql container from within another container on the same network specified by a compose file.

version: "2"
services:
  web:
    build:
      context: .
      dockerfile: nginx/Dockerfile
    ports:
      - "8080:80"
    volumes:
      - ./data:/srv
  php:
    build:
      context: .
      dockerfile: php-fpm/Dockerfile
    volumes:
      - ./data:/srv
  mysql:
    image: mysql
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
      - MYSQL_USER=dummy_user
      - MYSQL_PASSWORD=12345

I'm not really sure what the connection parameters would be if I'm trying to connect to the mysql container from the php container.

Specifically, what are the host and port for the mysql container from within another container of the same docker-compose network?

I've tried host: mysql port: 3306, but that doesn't seem to work.

like image 683
Confused Avatar asked Aug 17 '17 14:08

Confused


1 Answers

You should link the containers. Add ports section to mysql container and links section to php container.

version: "2"
services:
  web:
    build:
      context: .
      dockerfile: nginx/Dockerfile
    ports:
      - "8080:80"
    volumes:
      - ./data:/srv
  php:
    build:
      context: .
      dockerfile: php-fpm/Dockerfile
    links:
      - mysql:mysql
    volumes:
      - ./data:/srv
  mysql:
    image: mysql
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
      - MYSQL_USER=dummy_user
      - MYSQL_PASSWORD=12345

With that configuration you'll be able to access mysql container from php with mysql:3306

A bit of documentation: https://docs.docker.com/compose/networking/#updating-containers

like image 60
Taras Velykyy Avatar answered Sep 27 '22 20:09

Taras Velykyy