I am working on spring boot application. I want to dockerize my spring boot application but while I’m trying to make jar file from eclipse
I get error java.net.UnknownHostException
. I’m a new guy in docker technology.
Here is a docker images which is stored in my docker:
Here down is my code:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.flightbook</groupId>
<artifactId>flight_management_system</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>flight_management_system</name>
<description>Demo project for Spring Boot</description>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
spring.datasource.url=jdbc:mysql://mysqldb:3306/mysql_database?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=
server.port=8040
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
docker-compose.yml
version: "3.8"
services:
server:
build: .
restart: always
ports:
- 8090:8090
depends_on:
- mysqldb
mysqldb:
image: "mysql:8.0"
restart: always
ports:
- 3306:3306
environment:
MYSQL_DATABASE: mysql_database
MYSQL_USER: root
MYSQL_PASSWORD:
MYSQL_ROOT_PASSWORD:
Stack trace
Caused by: java.net.UnknownHostException: mysqldb
at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:801) ~[na:na]
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1509) ~[na:na]
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1367) ~[na:na]
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1301) ~[na:na]
at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:130) ~[mysql-connector-java-8.0.29.jar:8.0.29]
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-java-8.0.29.jar:8.0.29]
... 118 common frames omitted
2
Answers
I think that the container is not actually called
mysqldb
, but{appname}_mysqldb
, where the{appname}
is the name of the parent directory, where you invokedocker-compose up
.To check if this is the case, run
docker ps
after starting your containers.you can use in spring boot properties as database source localhost , and when you run your container use: sudo docker run –net=host -p image name