0
votes

I'm getting a "java.net.UnkownHostException: mysql: unknown error" when using Docker Compose and trying to link a spring boot web application container with a mysql container.

docker-compose.yml update #1

version: '2'

services:
web:
depends_on:
- db
image: thomptr/rest-services-ui:latest
ports:
- "9000:9000"
links:
- "db:database"
restart: always
environment:
SPRING_PROFILES_ACTIVE: dev
envTarget: dev

db:
image: mysql:5.7
ports:
- "3307:3306"
volumes:
- "/home/trevor/softwareDev/mySql/dump:/docker-entrypoint-initdb.d"
environment:
MYSQL_ROOT_PASSWORD: legion03
MYSQL_DATABASE: dev
MYSQL_USER: dbuser
MYSQL_PASSWORD: legion03

application-dev.properties update #1

spring.profiles.active=dev
server.contextPath=/restservices
server.port: 9000

spring.datasource.initialize=false
spring.datasource.platform=mysql
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://database:3306/dev

spring.datasource.username=dbuser
spring.datasource.password=legion03;
hibernate.level.logging=debug

spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect

logs from /dockercompose_web_1 container

 2016-07-01 04:17:24.119 ERROR 1 --- [ost-startStop-1] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.
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.
Caused by: java.net.UnknownHostException: mysql: unknown error
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)

UPDATE #2 The approach suggested by Nguyen Sy Thanh Son is correct. My solution uses the public wait-for-it.sh script. See my solution here : https://github.com/thomptr/DockerDemo

2

2 Answers

0
votes

This error will occur when web container is up before mysql container being up.

To solve this issue, I think that you should add a script into your entrypont.sh of web image as below

host="db"
password="$MYSQL_ROOT_PASSWORD" #mysql root password

until mysql -h "$host" -uroot -p$password; do
  >&2 echo "MYSQL is unavailable - sleeping"
  sleep 1
done

>&2 echo "MYSQL is up - executing command"

# start your app here

The script above will wait until mysql being up before running the application

And your Dockerfile of the application should contains:

# run app
COPY entrypoint.sh /
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
0
votes

i believe this error is due to the first container not knowing who is db. links from container A to container B will not modify /etc/hosts in both containers.

Another way to achieve this is with using aliases. you should find all info you need over here

https://docs.docker.com/compose/compose-file/#aliases

and modify the spring param:

spring.datasource.url=jdbc:mysql://<whatever alias you choose for db>:3306/dev

Please let me know if this worked.