8

I am attempting to build a "service" consisting of a Spring Boot application and PostgreSQL database. I have been able to access the database (running in a container) from the Spring Boot app while the Spring Boot application was running on my local machine. Now, when I attempt to move the Spring Boot application to a container, I am received the following error:

inventory_1  | 2018-01-20 18:43:06.108 ERROR 1 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection] with root cause
inventory_1  |
inventory_1  | java.net.ConnectException: Connection refused (Connection refused)

However, I am able to connect to DB from my local machine: psql -h localhost -p 5000 -U kelly_psql -d leisurely_diversion

My application.properties file:

spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.url=jdbc:postgresql://localhost:5432/leisurely_diversion
spring.datasource.username=kelly_psql
spring.datasource.password=pass
spring.datasource.driver-class-name=org.postgresql.Driver

My docker-compose file:

    # Use postgres/example user/password credentials
version: '3.2'

services:
  db:
    image: postgres
    ports:
      - 5000:5432
    environment:
      POSTGRES_PASSWORD: example
    volumes:
      - type: volume
        source: psql_data
        target: /var/lib/postgresql/data
    networks: 
      - app
    restart: always
  inventory:
    image: kellymarchewa/inventory_api
    depends_on:
        - db
    ports:
      - 8080:8080
    networks:
      - app
    restart: always
volumes:
  psql_data:
networks: 
  app:

My Dockerfile (from the Spring website)

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

I suspect the issue lies in a misunderstanding (on my part) of Docker or containers, but I am not sure. Any advice would be appreciated.

3 Answers 3

9

You are pointing your application towards localhost, but this is not shared between containers.

To access another container you have to refer to its hostname.

In your case, I understand that you want the inventory service to access the db service. So you should use the following datasource url:

spring.datasource.url=jdbc:postgresql://db:5432/leisurely_diversion

See this simple tutorial about connecting to a container from another container with docker compose: https://docs.docker.com/compose/gettingstarted/

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

Comments

2

Like in my case if you are using Docker Toolbox for windows 8.1 then you cannot use "localhost",

Instead you have to use docker machine ip;

 host> docker-machine ip default

 192.168.99.100

After that your url will look like;

 spring.datasource.url=jdbc:postgresql://192.168.99.100:5432/bankdb

This will successfully connect to docker Postgres DB.

Cheers!!

Comments

0

Elaborating a little upon the answer given by ESala:

I agree, it is a networking issue and the given solution works fine, but you can also use localhost (e.g. if you really really want to), by switching to network host mode when running your containers (cf here). Like done here for nginx.

I'd say you won't want this most of the time, since it messes with the sandbox you gain. But the option exists.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.