简体   繁体   中英

MySQL with docker-compose ports doesn't work with network

I am not sure if this is a bug, or a feature. But I have been running this docker-compose setup for my local development databases several months now and since I have updated Docker and Lando it stopped working.

As you can see I use it for some php sites managed with Lando. Since the last update either the ports or the network directive is not working on the MySQL container.

  • If I use it with the snippet below the ports directive is not working. Meaning I can not connect from QueryPie on localhost:3306 , but can connect from another lando docker container with mysql_db as host.
  • If I comment out the networks directive, the ports directive is working. But the networks directive obviously not. Meaning I can connect from QueryPie on localhost:3306 , but I can not connect from another lando docker container with mysql_db as host.

Before the update they worked nice together. So I could open MySQL in QueryPie and access the databases from the Lando containers.

version: '3'
services:
  mysql:
    container_name: mysql_db
    restart: always
    image: mysql:latest
    ports: 
        - "3306:3306" 
    environment:
       - MYSQL_ROOT_PASSWORD=secret
    volumes:
      - ./mysql-data:/var/lib/mysql
    networks:
      - lando_bridge_network
networks:
  lando_bridge_network:
    external: true

My docker engine is 19.03.8 and my docker-compose version is 1.25.5.

So I solved it by removing the networks directive and only exposing port 3306 with ports . The PHP sites running on lando/docker I the internal docker IP to connect:

define('DB_HOST', 'host.docker.internal');

In QueryPie (running on the host) I can connect with localhost:3306

version: '3'
services:
  mysql:
    container_name: mysql_db
    restart: always
    image: mysql:latest
    ports: 
        - "3306:3306" 
    environment:
       - MYSQL_ROOT_PASSWORD=secret
    volumes:
      - ./mysql-data:/var/lib/mysql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM