简体   繁体   中英

Spring boot, postgres inside docker compose cannot reach localhost

I have Spring boot web application with a Postgres database. I created docker-compose file

version: '3.1'
services:
  app:
    container_name: springboot-postgresql
    image: springboot-postgresql
    build: ./
    restart: always
    #Environment variables for Spring Boot Application.
    environment:
      - DB_SERVER=postgresqldb
      - POSTGRES_DB=my_database
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "8080:8080"
    depends_on:
      - postgresqldb

  postgresqldb:
    image: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=my_database

and Dockerfile

FROM adoptopenjdk/openjdk15:alpine-jre
VOLUME /tmp
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
EXPOSE 8080

and also application.properties file:

spring.datasource.url=jdbc:postgresql://${DB_SERVER}/${POSTGRES_DB}
spring.datasource.username=${POSTGRES_USER}
spring.datasource.password=${POSTGRES_PASSWORD}
spring.jpa.hibernate.ddl-auto=create

spring.freemarker.expose-request-attributes=true
spring.freemarker.suffix= .ftl

address=http://localhost:8080/

server.error.whitelabel.enabled=false

# =========================== MAIL ===========================
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

spring.mail.host=***
spring.mail.username=***
spring.mail.password=***
spring.mail.port=465
spring.mail.protocol=smtps
mail.smtp.auth=true
mail.debug=true

# =========================== SESSIONS ===========================
spring.session.jdbc.initialize-schema=always
spring.session.jdbc.table-name=SPRING_SESSION

# =========================== THYMELEAF ===========================
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html # ;charset=<encoding> is added
spring.thymeleaf.cache=true


After I run docker-compose up all the services started and everything is working well:

...
springboot-postgresql | 2021-01-11 13:20:20.703  INFO 1 --- [           main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
springboot-postgresql | 2021-01-11 13:20:21.268  INFO 1 --- [           main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
springboot-postgresql | 2021-01-11 13:20:21.306  INFO 1 --- [           main] com.example.semestral_work.Application   : Started Application in 15.753 seconds (JVM running for 17.887)

But I can't visit http://localhost:8080/ link because of ERR_CONNECTION_REFUSED . The server looks like it is running, but I can't visit any page of this app. What I'm doing wrong, please?

because your tomcat of spring did not start yet. for that, you need to start docker or the spring application in a different terminal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM