简体   繁体   中英

Can't connect to Windows 10 Docker mysql

Win 10 Composer version 1.4.1 2017-03-10 09:29:45 PHP 7 npm/Node Docker CE Apache 2.4 Powershell git BASH shell drush (installed via composer)

Noob Composer/Docker skills

I have a docker config yml specifying how mysql service can start:

version: "2"
services:
  mysql:
    image: mysql:5.6
    ports:
      - 3306:3306
    volumes:
      - /data/nbif_mysql:/var/lib/mysql
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"

And when I call

#!/bin/bash
docker-compose up -d mysql

I see that the container is running:

PS C:\dev\appname> docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
    f1a0ecab8af6        mysql:5.6           "docker-entrypoint..."   2 hours ago         Up 5 seconds        0.0.0.0:3306->3306/tcp   appname_mysql_1

But, notice that the reported IP is 0.0.0.0:3306->3306/tcp

So when I try to connect with the expected IP, it fails:

ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.0.1' (10060)

How to I tell docker-compose to use that expected IP for docker? Is this a setup issue, or some config tweak I need to do?

When you bind a port to your host, you have to use localhost instead of the container's IP address, because you're not assigning any local IP address.

Every container always runs in a isolated network (bridge), the container in your compose file will be able to find the others by their hostname, but inside of those containers, they are isolated from the local network so that's why you can't reach them.

In your compose file you only have a mysql container and you're binding that port in your host, so the only way to reach that container is by using localhost:3306

Remember, when you run a docker container it isn't like a server with an IP in your host network, it's more like a virtual machine with an isolated network configuration.

Take a look on the docker-compose docs in this specific topic: https://docs.docker.com/compose/networking

UPDATE:

The link that finally answered the question was: https://docs.docker.com/engine/userguide/networking/default_network/custom-docker0/

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