dockerfile:
FROM anapsix/alpine-java:8_server-jre_unlimited
# copy application
COPY server.jar /opt/test/
COPY application.yml /opt/test/
# expose server ports
EXPOSE 8080 18080
# Run via dumb-init
WORKDIR /opt/test
ENTRYPOINT ["java"]
CMD ["-jar", "server.jar"]
docker-compose files:
services:
backend-server:
image: test.com/server:latest
build: .
depends_on:
- database-server
ports:
- "127.0.0.1:8080:8080"
- "127.0.0.1:18080:18080"
database-server:
image: postgres:9.6
ports:
- "127.0.0.1:5432:5432"
environment:
- POSTGRES_PASSWORD testtest
application property file:
spring:
datasource:
# use default user/database created by PostgreSQL Docker image upon startup
url: jdbc:postgresql://localhost/postgres
username: postgres
password: testtest
driver-class-name: org.postgresql.Driver
When I run the docker-compose up, it will create a database container and an application container which connects to the database container.
But the spring application container encounters some problem during bootup with error:
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.
docker container ls
shows the database is running, and I can access it if I run the spring application directly.
a295bfd16e2c postgres:9.6 "docker-entrypoint.s…" About a minute ago Up About a minute 127.0.0.1:5432->5432/tcp server_database-server_1
So what would be the problem that I messed up?
Solution I found
services:
backend-server:
image: test.com/server:latest
build: .
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://database-server/postgres
depends_on:
- database-server
ports:
- 8080:8080
- 18080:18080
database-server:
image: postgres:9.6
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD testtest
But I still cannot access the application from localhost:8080
of my computer, even the log of the container says
2018-10-31 15:02:40.881 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-10-31 15:02:40.881 INFO 1 --- [ main] com.test.server.Application : Started Application in 12.823 seconds (JVM running for 13.235)
I suggest to modify your docker-compose.yml file as given below and test it