1

my application.properties file

server.port=8085
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://init-postgres:5432/dbname
spring.datasource.username=username
spring.datasource.password=password

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

and for dockerizing postgres I'm using command

docker run -d -p 5432:5432 --name init-postgres -e POSTGRES_DB=dbname -e POSTGRES_USER=username -e POSTGRES_PASSWORD=password postgres

but it gives java.net.UnknownHostException: init-postgres I'm beginner with Docker and learning it from a tutorial. to dockerized Postgresql & Spring boot app communication.

3
  • The url should use localhost... The container name will only work if you are also dockerizing your spring boot application and run them in the same network. Commented Oct 10, 2018 at 12:23
  • @M. Denium how I'll be able to generate docker image using mvn install because whenever I fire this command it gives an error java.net.UnknownHostException & build is failed Commented Oct 10, 2018 at 13:46
  • As stated use localhost... Commented Oct 10, 2018 at 13:49

2 Answers 2

1

If you need to dockerize both of them without docker-compose

application.config

spring.datasource.url=jdbc:postgresql://init-postgres:5432/dbname

  1. Create network

docker network create mynet

  1. Run postgres container with created network

docker run --net mynet --name init-postgres -d -e POSTGRES_DB=dbname -e POSTGRES_USER=username -e POSTGRES_PASSWORD=password -p 5432:5432 postgres

  1. Create jar archive
mvn clean
mvn compile
mvn package 
  1. Create dockerfile
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/<HERE_IS_NAME_OF_YOUR_JAR_FILE>.jar
COPY ${JAR_FILE} myapp.jar
EXPOSE 8085
ENTRYPOINT ["java","-jar" , "/myapp.jar"]
  1. Build spring boot image myapp

docker build -t myapp .

  1. Run spring boot container

docker run --name myapp-container --net mynet -p 8080:8080 myapp

Sign up to request clarification or add additional context in comments.

Comments

0

If your application runs on the host without docker and your database lives inside a docker container, you need to change this line:

spring.datasource.url=jdbc:postgresql://init-postgres:5432/dbname

with

spring.datasource.url=jdbc:postgresql://localhost:5432/dbname

instead, if both the application and the database are running on docker you need to refer with the docker container name, as you stated above in the snippet you posted.

I suggest to use docker-compose, it's a handy tool that can ease the difficulties of deployment and it's useful when developing too since it allows to bring up & down your application without too much hassle. In the official docker website there is a nice introduction to the tool with examples.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.