4

Mysql is in it's own docker-compose.yml as I want a mysql server up and running that any other php application can connect to. So I do not have php and mysql in the same docker-compose.yml. From the php application, I can connect to mysql if I use the mysql container's gateway ip address by looking it up and then hard coding it into the php application. docker inspect mysql-db. But docker will change that 172... ip address each time mysql restarts so that is not ideal for development.

I can connect to mysql via mysql -h 127.0.0.1 no problem, but from the php application if I try to use 127.0.0.1 I get connection refused. I can only connect if I use the 172... gateway ip address.

How do I get the mysql container listening for connections from the host to 127.0.0.1?

docker-compose.yml for mysql

version: "3"

services:
  mysql:
    container_name: mysql-db
    image: mysql
    build:
      dockerfile: Dockerfile
      context: ./server/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=admin
    volumes:
      - ./data/mysql:/var/lib/mysql
    ports:
      - 3306:3306

docker-compose.yml for php

version: "3"

services:
  nginx:
    container_name: nginx_myapp
    image: nginx
    build:
      dockerfile: Dockerfile
      context: ./server/nginx
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./app:/var/www/html
    networks:
      - myapp
  php:
    container_name: php_myapp
    image: php:7.3-fpm
    build:
      dockerfile: Dockerfile
      context: ./server/php-fpm
    environment:
      CI_ENV: development
    volumes:
      - ./app:/var/www/html
    networks:
      - myapp

networks:
  myapp:
Zelf
  • 1,723
  • 2
  • 23
  • 40
  • Does [this](https://docs.docker.com/config/containers/container-networking/#ip-address-and-hostname) help at all? – segFault Feb 15 '20 at 15:06
  • @segFault I suppose the answer might be in there somewhere, but the answer I accepted here was a simple solution and I think the most correct one for docker-compose simplicity. – Zelf Feb 15 '20 at 18:29

2 Answers2

6

127.0.0.1 is the loopback address. It points to localhost. In the context of docker, localhost is the container itself. There is no db running on your php container so the connection will never succeed.

What you need to do is to configure the default network in you mysql compose file so that you will predictably control its name for later convenience (else it will be calculated from your compose project name which could change if you rename the containing folder...):

Important note: for the below to work, you need to use compose file version >= 3.5

---
version: '3.7'
#...
networks:
  default:
    name: shared_mysql

You can now use that shared_mysql network as external from any other compose project.

version: '3.7'

services:
  nginx:
    #...
    networks:
      - myapp
  php:
    #...
    networks:
      - myapp
      - database

networks:
  myapp:
  database:
    external: true
    name: shared_mysql

You can then connect to mysql from your php container using the service name mysql (e.g. mysql -h mysql -u user -p)

Reference: https://docs.docker.com/compose/networking/

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
-1

Few solutions for you

1) you can duplicate mysql section in each file using same volume path, in that case when you start project you will have same databases

project1
version: "3.2"
services:
  mysql:
    volumes:
      - /var/mysql:/var/lib/mysql
  php:
    build:
      context: './php/'

project2
version: "3.2"
services:
  mysql:
    volumes:
      - /var/mysql:/var/lib/mysql
  php:
    build:
      context: './php/'

2) you can connect using host.docker.internal or macos (docker.for.mac.localhost) directly to your host machine more information here From inside of a Docker container, how do I connect to the localhost of the machine?

  • I had considered volumes that lead to the same host directory, but the accepted answer was an easy solution. Regarding #2 thought, how would I pass in the host ip? Can you provide an example? That seems like a good solution as well. – Zelf Feb 15 '20 at 18:32
  • Are you suggesting to share the same bind mounted mysql data directory from the host to several mysql servers running in different containers and active at the same time ? I do hope you don't have any critical data in there (or a good backup). Running different servers on the same data dir is very risky, only supports a few db engines, and will end up in data corruption in nearly all cases unless you **perfectly** know what you are doing. For more info see notes in https://dev.mysql.com/doc/refman/8.0/en/multiple-data-directories.html (valid for version 5.6 as well). – Zeitounator Apr 26 '21 at 15:14
  • @Zeitounator no in example project1 never run with project2 (separate projects) but same db (volume) storage – Tomáš Mafi Vinduška May 01 '21 at 22:00
  • First sentence of above question: `Mysql is in it's own docker-compose.yml as I want a mysql server up and running that any other php application can connect to.` – Zeitounator May 02 '21 at 06:41