2

I have installed docker on Windows machine using docker tool box. I also have mysql installed on my windows machine and the server is running on port 3306 (localhost - 127.0.0.1 - outside the docker machine) I am running a sprint boot application inside a docker container with name as 'micra-workq-svc' inside the docker network called 'micra-network' I want application running inside the docker to connect to this local host. I spent hours searching on google and none of the links help me to resolve this issue. I tried running mysql image with this command:

docker run  --name micra-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=<pass> -e MYSQL_DATABASE=<db name> -e MYSQL_USER=<roo user> -e MYSQL_PASSWORD=<root password >--net=micra-network mysql:latest

I am using following command to run my container:

docker run -e "SPRING_PROFILES_ACTIVE=dev" --network micra-network --env "eureka.client.enabled=true"  --env "eureka.host=micra-eureka-server"  --env "eureka.instance.preferIpAddress: true"  --env "eureka.client.serviceUrl.defaultZone=http://micra-eureka-server:8761/eureka"  --env DATABASE_HOST=127.0.0.1 --env DATABASE_PORT=3306 --env DATABASE_NAME=<db name> --env DATABASE_USER=<db user> --env DATABASE_PASSWORD=<db password> --expose 8083 -p 8083:8083 --name micra-workq-svc --link micra-mysql  -t  <my docker service image>

This is how application.properties look like in spring boot:

spring:
 datasource:
  password: ${DATABASE_PASSWORD}
  url: jdbc:mysql://${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}
  username: ${DATABASE_USER}

When I run my service I get following error:

Failed to obtain JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

My docker machine ip is 192.168.99.100.

adwived
  • 63
  • 5
  • Try with your machine's ip something like (192.168.xxx.xxx), also you will need to update your mysql conf, bind address 0.0.0.0 – BugHunter Oct 28 '18 at 18:15
  • @Bughunter - No it doesn't work. I changed environment property DATABASE_HOST to 192.168.99.100 (My docker machine ip). – adwived Oct 28 '18 at 18:19
  • If your running both your Java and DB in containers, you can access them by their name. Localhost won't work as it's relative to the container. – Arman Oct 28 '18 at 18:23
  • @Arman - I tried running mysql inside the container using the above command and changed DATABASE_HOST to 'micra-mysql' which is the container name but I get unknown host exception. Also note application is running inside the docker network 'micra-network'. – adwived Oct 28 '18 at 18:26
  • On second view, on your first run command you used "--net=micra-network" and on the second one "--network micra-network". Sure they where on the same network? Using docker-compose get rid of these issues in the first place, but doesn't seem to solve your use-case. – Arman Oct 28 '18 at 19:15
  • Possible duplicate of [Access host database from a docker container](https://stackoverflow.com/questions/28056522/access-host-database-from-a-docker-container) – David Maze Oct 28 '18 at 21:06

1 Answers1

1

Just clean your containers and images as administrator execute this command docker prune - a , put DATABASE_HOST=micra-mysql and run again.

Jonathan JOhx
  • 5,784
  • 2
  • 17
  • 33
  • Man, does this work for localhost? DATABASE_HOST=localhost – Miltex Oct 24 '19 at 17:36
  • does it work on docker-compose? On an internal network in the docker environment. – Miltex Oct 24 '19 at 17:37
  • @Miltex Since you use `docker compose` means you need to specif `name container` then this will work on its containers context on `docker`. On your environment will be taken `localhost` internally. – Jonathan JOhx Oct 24 '19 at 17:48
  • I have mysql and an application running on the docker. I want the application to connect to mysql localhost inside the docker. – Miltex Oct 24 '19 at 18:07
  • You're telling me that if I put in docker-compose the container_name: localhost attribute will work. – Miltex Oct 24 '19 at 18:09
  • Nope, however you can achievement it creating several services in `docker-compose.yml`, one of them will be `database image` in your case `mysql`, please open a question and let me know so I can help you :) @Miltex – Jonathan JOhx Oct 24 '19 at 18:25