简体   繁体   中英

Docker Compose - Flyway - Unable to obtain Jdbc connection from DataSource

I'm traying to deploy a microserivice and a flyway service with Docker Compose in Ubuntu. The docker-compose.yml looks like this:

version: '2'

services:

mysqldb:
image: mysql:5.6.26
environment:
  MYSQL_USER: user
  MYSQL_PASSWORD: password
  MYSQL_ROOT_PASSWORD: password
  MYSQL_DATABASE: base
ports:
  - "3306:3306"

flyway-service-i:
image: mialk/flyway-service
volumes:
 - "../resources/db/migration:/migrations/ro"
depends_on:
 - mysqldb
links:
 - mysqldb
command: migrate -url=jdbc:mysql://mysqldb:3306/base -user=user -password=password -baselineOnMigrate=true -    locations='filesystem:/migrations'

  service1:
image: my/service
ports:
  - "8080:8080"
links:
 - mysqldb
environment:
  - SPRING_DATASOURCE_URL=jdbc:mysql://mysqldb:3306/base
  - SPRING_DATASOURCE_USERNAME=user
  - SPRING_DATASOURCE_PASSWORD=password

But when I run the command: sudo docker-compose up, I have this message:

flyway-service-i_1 | ERROR: Unable to obtain Jdbc connection from DataSource (jdbc:mysql://mysqldb:3306/base) for user 'user': Host '172.18.0.4' is not allowed to connect to this MySQL server

Each time that I run the command, I get a different host, eg:

ERROR: Unable to obtain Jdbc connection from DataSource (jdbc:mysql://mysqldb:3306/base) for user 'user': Host '172.18.0.6' is not allowed to connect to this MySQL server

The data base was created in that way:

CREATE DATABASE base CHARACTER SET utf8 COLLATE utf8_bin;
GRANT ALL PRIVILEGES ON base.* To 'user'@'localhost';

The ifconfig command shows that Docker has the IP 172.17.0.1.

I don't know why flyway can't connect to the data base, and why the host changes en each call,can you help me please?

Thanks! :)

Issue

In this line
GRANT ALL PRIVILEGES ON base.* To 'user'@'localhost';
you grant priviledges to user on localhost but your service1 and flyaway-service-1 services are running in their own separate containers and therefore are not running on localhost (with respect to the mysqldb container). If these services were running inside the mysqldb container then maybe they could be running on localhost .

Possible Resolution?

You may want to try using the hostname of the services/containers you're granting access to instead of localhost ?

ie : GRANT ALL PRIVILEGES ON base.* To 'user'@'service1';

Docker Networks instead of Container Links

Also, I would suggest using Docker's new networking feature instead of the legacy container linking method. See rationale behind docker compose "links" order

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