简体   繁体   中英

Openshift Spring Boot Docker Image & Postgres Pod

I have a spring boot web API (simple with database connection) and I would like to put it on a openshift. I can generate the image locally with docker, then use oc port-forward to port forward the postgres pod's connection to my localhost:3432 resulting in my application properties to be as follows:

spring.datasource.url=jdbc:postgresql://localhost:5432/{dbname}
spring.datasource.username={dbuser}
spring.datasource.password={dbpassword}

where the names & passwords are substituted appropriately. Effectively, this connects my local build to the remote database.

My question is, when pushing the docker container to OSE, what do I set these connection properties to in order to connect the remote application to the remote database? I initially thought I would expose the Postgres service w/ a route and set the datasource url to that; however, I still receive connection refused when running my docker image. My dockerfile is simple and as follows:

FROM openjdk:8-jre-alpine
COPY ./zeus-service-api/target/zeus-service-api-1.0-SNAPSHOT-exec.jar /usr/src/zeus-service/
WORKDIR /usr/src/zeus-service
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "zeus-service-api-1.0-SNAPSHOT-exec.jar"]

When deploying your springboot app to OpenShift as opposed to local you can use the environment variables or the connection information. To find this information (env variables), click on a progres deployment and then click environment and it will display the environment variables to use. I see a lot of developers setting up these environment variables locally so when they move their deployment to the platform, they don't have to change code.

Another way to find all of this information is to open a terminal session via the web console to postgres pod and run the following command:

h-4.2$ env |grep POSTGRES
POSTGRESQL_PORT_5432_TCP_ADDR=172.30.248.18
POSTGRESQL_PORT=tcp://172.30.248.18:5432
POSTGRESQL_SERVICE_PORT_POSTGRESQL=5432
POSTGRESQL_PORT_5432_TCP=tcp://172.30.248.18:5432
POSTGRESQL_SERVICE_HOST=172.30.248.18
POSTGRESQL_DATABASE=sampledb
POSTGRESQL_PASSWORD=xcUmSHIJudu421v0
POSTGRESQL_PORT_5432_TCP_PORT=5432
POSTGRESQL_VERSION=9.6
POSTGRESQL_PREV_VERSION=9.5
POSTGRESQL_SERVICE_PORT=5432
POSTGRESQL_USER=user455
POSTGRESQL_PORT_5432_TCP_PROTO=tcp
sh-4.2$

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