Home  >  Article  >  Docker Compose + Spring Boot + Postgres connection

Docker Compose + Spring Boot + Postgres connection

WBOY
WBOYforward
2024-02-11 15:39:09932browse

php editor Xigua today brings you a tutorial on how to use Docker Compose to connect Spring Boot and Postgres. Docker Compose is a tool for defining and running multi-container Docker applications, while Spring Boot is a framework for building Java applications, and Postgres is a powerful relational database. By using these three together, we can easily build a development environment that includes a Spring Boot application and a Postgres database. This tutorial will take you step-by-step to learn how to configure and connect all three, so you can quickly start developing your applications.

Question content

I have a Java Spring Boot application that works with a Postgres database. I want to use Docker for both of them. I initially just put Postgres into Docker and had a docker-compose.yml file defined like this:

version: '2'
services:
    db:
        container_name: sample_db
        image: postgres:9.5
        volumes:
            - sample_db:/var/lib/postgresql/data
        environment:
            - POSTGRES_PASSWORD=sample
            - POSTGRES_USER=sample
            - POSTGRES_DB=sample
            - PGDATA=/var/lib/postgresql/data/pgdata
        ports:
            - 5432:5432

volumes:
    sample_db: {}

Then when I issue the commands sudo dockerd and sudo docker-compose -f docker-compose.yml up it is starting the database. For example, I can connect using pgAdmin, using localhost as the server and port 5432. Then, in my Spring Boot application, the following properties are defined in the application.properties file.

spring.datasource.url=jdbc:postgresql://localhost:5432/sample
spring.datasource.username=sample
spring.datasource.password=sample
spring.jpa.generate-ddl=true

At this point, I can run my Spring Boot application locally through Spring Suite and everything works fine. Then, I also want to add my Spring Boot application as a Docker image. I first created a Dockerfile in the project directory as follows:

FROM java:8
EXPOSE 8080
ADD /target/manager.jar manager.jar
ENTRYPOINT ["java","-jar","manager.jar"]

Then I went into the project directory where mvn clean was released, then mvn install. Next, issue docker build -f Dockerfile -t manager . followed by docker tag 9c6b1e3f1d5e myuser/manager:latest (the id is correct). Finally, I edited the existing docker-compose.yml file to look like this:

version: '2'
services:
    web:
      image: myuser/manager:latest
      ports: 
          - 8080:8080
      depends_on:
          - db
    db:
        container_name: sample_db
        image: postgres:9.5
        volumes:
            - sample_db:/var/lib/postgresql/data
        environment:
            - POSTGRES_PASSWORD=sample
            - POSTGRES_USER=sample
            - POSTGRES_DB=sample
            - PGDATA=/var/lib/postgresql/data/pgdata
        ports:
            - 5432:5432

volumes:
    sample_db: {}

However, now if I issue the sudo docker-compose -f docker-compose.yml up command, the database starts correctly again but I get an error and exit with code 1 of the web application section . The problem is the connection string. I believe I have to change it to something else but I don't know what it should be. I receive the following error message:

web_1  | 2017-06-27 22:11:54.418 ERROR 1 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.
web_1  | 
web_1  | org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections

Any ideas?

Solution

Each container has its own network interface and its own local host. So change the way Java points to Postgres:

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

To:

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

db will resolve to the correct Postgres IP.

bonus. With docker-compose, you don't need to build images manually. So change:

web:
  image: myuser/manager:latest

To:

web:
  build: .

I had the same problem and it took me some time to understand and solve it:

org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

I show all properties so everyone can understand. application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL82Dialect
spring.jpa.hibernate.ddl-auto=update

docker-compose.yml:

version: "3"
  services:
    springapp:
      build: .
      container_name: springapp
      environment:
        SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/testdb
      ports:
        - 8000:8080
      restart: always
      depends_on:
        - db
    db:
      image: postgres
      container_name: db
      environment:
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=postgres
        - POSTGRES_DB=testdb
        - PGDATA=/var/lib/postgresql/data/pgdata
      ports:
        - 5000:5432
      volumes:
        - pgdata:/var/lib/postgresql/data
      restart: always
  volumes:
    pgdata:

In order to start the Spring application using a local database, we use the url localhost. In order to connect to the container using the database we need to change the "localhost" on the database service, in my case "localhost" to "db". Solution: Add the SPRING_DATASOURCE_URL environment in docker-compose.yml and rewrite the spring.datasource.url connection value:

environment:
    SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/testdb

I hope this helps people save time.

The above is the detailed content of Docker Compose + Spring Boot + Postgres connection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Need @Bind?Next article:Need @Bind?