简体   繁体   中英

Spring Boot Docker - connection to localhost:5432 refused

I am new to Docker. I am trying to dockerize my Spring Boot application but getting the error connection to localhost:5432 refused . The Spring Boot application has a postgresql db connection and uses random port to run on Tomcat. Please find the details below, requesting to please check and help in resolving the issue. Please give a detailed solution as I am new to Docker.

Dockerfile

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/SpringConversionFactor-0.0.1-SNAPSHOT.jar
WORKDIR /opt/app
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","app.jar"]

Docker run

docker run springconversionfactor

Exception

Connection to localost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections

When you are running any application in docker it gets isolated from your existing environment.Here you are running spring boot application in docker and provided postgresql url as localhost:5432 ,because of that your spring boot application is finding postgres db inside your spring boot docker container.Please make sure where your postgres db is running .If it is running on your server give url as server_ip:5432 or if it is running in other conatiner then give it as container_name:5432 (here you can do port mapping with your server also).

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