当使用Docker Compose并尝试将spring引导Web应用程序容器与mysql容器链接时,我收到“java.net.UnkownHostException:mysql:unknown error” .
docker-compose.yml 更新#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 更新#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)
更新#2 Nguyen Sy Thanh Son建议的方法是正确的 . 我的解决方案使用public wait-for-it.sh脚本 . 在这里查看我的解决方案:https://github.com/thomptr/DockerDemo