3

Docker version 17.03.1-ce, build c6d412e OS: Ubuntu

I am trying to connect to host mysql from the docker container. but i am getting this error.

Error: connect ECONNREFUSED 0.0.0.0:3306

I am getting same for mysql if i use mysql container. Tried 127.0.0.1 and localhost also.

version: '2'

services:

### Applications Code Container 
#############################

  applications:
    image: tianon/true
    volumes:
      - ${APPLICATION}:/var/www/html

  apache2:
    build:
      context: ./apache2
    volumes_from:
      - applications
    volumes:
      - ${APACHE_HOST_LOG_PATH}:/var/log/apache2
      - ./apache2/sites:/etc/apache2/sites-available
    ports:
      - "${APACHE_HOST_HTTP_PORT}:80"
      - "${APACHE_HOST_HTTPS_PORT}:443"
    networks:
      - frontend
      - backend

  node:
    build:
      context: ./node
    volumes_from:
      - applications
    ports:
      - "4000:30001"
    networks:
      - frontend
      - backend    


### MySQL Container  #########################################

  mysql:
    build:
      context: ./mysql
    environment:
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
    volumes_from:
      - applications  
    volumes:
      - ${DATA_SAVE_PATH}/mysql:/var/lib/mysql
      - ./mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
    ports:
      - "${MYSQL_PORT}:3306"
    networks:
      - backend


### Networks Setup ############################################

networks:
  frontend:
    driver: "bridge"
  backend:
    driver: "bridge"

### Volumes Setup #############################################

volumes:
  mysql:
    driver: "local"
  mongo:
    driver: "local"
  node:
    driver: "local"
  apache2:
    driver: "local"
Robert
  • 33,429
  • 8
  • 90
  • 94
Sandhu
  • 348
  • 5
  • 23
  • What port binding you see with `docker-compose ps` for mysql? – Robert Jun 01 '17 at 16:35
  • 0.0.0.0:3306->3306/tcp for mysql container. – Sandhu Jun 01 '17 at 16:37
  • I am more concerned about connecting with docker host mysql, creating a mysql container was just a try. – Sandhu Jun 01 '17 at 16:38
  • Oh.. I understand. Use this: `172.17.0.1:3306`. That IP is fixed for your host docker, visible from container. Give a try – Robert Jun 01 '17 at 16:40
  • Nope. not working. I am also connecting to the mongo db. Which is on another server. That is working. – Sandhu Jun 01 '17 at 16:44
  • Does your host mysql listen to all interfaces? `netstat -na|grep LISTEN|grep 3306` – Robert Jun 01 '17 at 16:51
  • tcp6 0 0 :::3306 :::* LISTEN – Sandhu Jun 01 '17 at 16:53
  • I see that you have a custom network. So identify the container ID, then `docker inspect ID`, then find the container IP. There you have the network. Point to your mysql in that network, but host `.1`. Post it if you don't get me – Robert Jun 01 '17 at 16:56
  • I tried changing the bind-address in mysqld.cnf of the host. Didn't work. – Sandhu Jun 01 '17 at 17:19
  • I have mounted the code with ADD command. If i run npm start from outside the container it works – Sandhu Jun 01 '17 at 17:20
  • Because the localhost of the container is not your host (nor 0.0.0.0). To see what is the ip of the host as container can see, do `docker network ls | grep backend`, Use that network id in `docker network inspect THE_ID -f "{{(index .IPAM.Config 0).Gateway}}"` – Robert Jun 01 '17 at 17:29
  • this is ip that i got 172.20.0.1 – Sandhu Jun 01 '17 at 17:32
  • so point as this!!! `172.20.0.1:3306` – Robert Jun 01 '17 at 17:35
  • Thanks for your help, issue resolved with https://stackoverflow.com/a/1559992/3994193 – Sandhu Jun 01 '17 at 17:39
  • Always welcome. What is the ip of mysql finally? – Robert Jun 01 '17 at 17:41
  • In bind address i updated to bind-address = 0.0.0.0 Is this possible i dont have to change the bind address to 0.0.0.0? – Sandhu Jun 01 '17 at 17:45
  • That is required because your container is not accessing to its `localhost`, it needs to access to the host IP other than localhost, so mysql need to listen in all interfaces (`0.0.0.0`). Does it make sense? – Robert Jun 01 '17 at 17:58

1 Answers1

0

Instead of using 0.0.0.0, 127.0.0.1 or localhost, you should use your host machine's IP. This is because each container is a individual node in the network.

Or if you can inspect your MySQL container, and get the IP of it, you can use the IP as well, since they are on the same network.

benjah1
  • 1,440
  • 1
  • 13
  • 17
  • @Sandhu I see you are building the mysql. Can you provide the mysql log? `docker-compose logs mysql`. I suspect if the mysql is running, or bind to an ip. – benjah1 Jun 02 '17 at 01:45