connect to mysql container from another container

86
May 14, 2022, at 7:20 PM

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.

Answer 1

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

Answer 2

You don't share port for mysql

add to your docker-compose.yml to mysql service:

ports:
  - "3306:3306"

UPD

try to set environments of your mysql container

MYSQL_ROOT_PASSWORD: 123456
MYSQL_USER: dev
MYSQL_PASSWORD: 123456
MYSQL_DATABASE: myapp

and

$link = mysqli_connect("mysql", "dev", "123456", "myapp");

should works fine

Rent Charter Buses Company
READ ALSO
Is there a way to Customize GridFieldEditor summary values based on Array?

Is there a way to Customize GridFieldEditor summary values based on Array?

We have a field which contains an array of data and we want to display it on GridField as Summary individually

88
Stripe Webhook times out half way through

Stripe Webhook times out half way through

I have a Stripe webhook that times out half way through my requestThe webhook itself is working, as I have it inserting data into my database

68
php json_decode function doesn't work with me

php json_decode function doesn't work with me

when decoding this json file 'https://2geekproarblogspot

146