Docker: Springboot container can not connect to PostgreSql Container Connection error
up vote
1
down vote
favorite
I am building my first Springboot 2.0 application. I am trying to put my Springboot application into one docker container and my PostgresDB into another container.
My Dockerfile
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD springboot-api-demo-0.1*.jar app.jar
RUN sh -c 'touch /app.jar'
EXPOSE 9443
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/urandom -jar /app.jar" ]
My docker-compose.yml file
version: "2.1"
services:
springboot-api-demo:
image: "fw/springboot-api-demo"
mem_limit: 1024m
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=local
- AWS_REGION=local
- ENVIRONMENT=local
- AUTH_ENABLED=false
postgres:
container_name: pgdb
image: postgres:9.6-alpine
environment:
- 'POSTGRES_ROOT_PASSWORD=postgres'
- 'POSTGRES_USER=postgres'
- 'POSTGRES_PASSWORD=postgres'
ports:
- "54321:5432"
I am using Springboot JPA Data 2.0 with below config data in my application.properties
spring.datasource.url= jdbc:postgresql://localhost:54321/java_learning
spring.datasource.username=postgres
spring.datasource.password=postgres
I can test that Both of the Images are up. Also from docker log and docker events, I see that postgres Container is running fine, even I can access it and also created a DB too.
But springboot container started but i died because it could not connect to postgress and throwing error below.
Unable to obtain connection from database: The connection attempt
failed
Note that my host machine already has Postgres on port 5432 thats why I did a port mapping ofr 54321:5432 on my postgres container. Here is Proof :) -
➜ springboot-api-demo git:(master) ✗ lsof -i:54321
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
com.docke 44345 shailendra.singh 18u IPv4 0xf62897fbdd69e31d 0t0 TCP *:54321 (LISTEN)
com.docke 44345 shailendra.singh 21u IPv6 0xf62897fbdd119975 0t0 TCP localhost:54321 (LISTEN)
➜ springboot-api-demo git:(master) ✗ lsof -i:5432
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 715 shailendra.singh 5u IPv6 0xf62897fbb43e03b5 0t0 TCP localhost:postgresql (LISTEN)
postgres 715 shailendra.singh 6u IPv4 0xf62897fbbaeea9bd 0t0 TCP localhost:postgresql (LISTEN)
I am not sure what is the problem. But my Springboot application is not able to connect my postgres container which is running fine with proper creadentials.
postgresql spring-boot docker docker-compose
add a comment |
up vote
1
down vote
favorite
I am building my first Springboot 2.0 application. I am trying to put my Springboot application into one docker container and my PostgresDB into another container.
My Dockerfile
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD springboot-api-demo-0.1*.jar app.jar
RUN sh -c 'touch /app.jar'
EXPOSE 9443
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/urandom -jar /app.jar" ]
My docker-compose.yml file
version: "2.1"
services:
springboot-api-demo:
image: "fw/springboot-api-demo"
mem_limit: 1024m
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=local
- AWS_REGION=local
- ENVIRONMENT=local
- AUTH_ENABLED=false
postgres:
container_name: pgdb
image: postgres:9.6-alpine
environment:
- 'POSTGRES_ROOT_PASSWORD=postgres'
- 'POSTGRES_USER=postgres'
- 'POSTGRES_PASSWORD=postgres'
ports:
- "54321:5432"
I am using Springboot JPA Data 2.0 with below config data in my application.properties
spring.datasource.url= jdbc:postgresql://localhost:54321/java_learning
spring.datasource.username=postgres
spring.datasource.password=postgres
I can test that Both of the Images are up. Also from docker log and docker events, I see that postgres Container is running fine, even I can access it and also created a DB too.
But springboot container started but i died because it could not connect to postgress and throwing error below.
Unable to obtain connection from database: The connection attempt
failed
Note that my host machine already has Postgres on port 5432 thats why I did a port mapping ofr 54321:5432 on my postgres container. Here is Proof :) -
➜ springboot-api-demo git:(master) ✗ lsof -i:54321
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
com.docke 44345 shailendra.singh 18u IPv4 0xf62897fbdd69e31d 0t0 TCP *:54321 (LISTEN)
com.docke 44345 shailendra.singh 21u IPv6 0xf62897fbdd119975 0t0 TCP localhost:54321 (LISTEN)
➜ springboot-api-demo git:(master) ✗ lsof -i:5432
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 715 shailendra.singh 5u IPv6 0xf62897fbb43e03b5 0t0 TCP localhost:postgresql (LISTEN)
postgres 715 shailendra.singh 6u IPv4 0xf62897fbbaeea9bd 0t0 TCP localhost:postgresql (LISTEN)
I am not sure what is the problem. But my Springboot application is not able to connect my postgres container which is running fine with proper creadentials.
postgresql spring-boot docker docker-compose
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am building my first Springboot 2.0 application. I am trying to put my Springboot application into one docker container and my PostgresDB into another container.
My Dockerfile
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD springboot-api-demo-0.1*.jar app.jar
RUN sh -c 'touch /app.jar'
EXPOSE 9443
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/urandom -jar /app.jar" ]
My docker-compose.yml file
version: "2.1"
services:
springboot-api-demo:
image: "fw/springboot-api-demo"
mem_limit: 1024m
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=local
- AWS_REGION=local
- ENVIRONMENT=local
- AUTH_ENABLED=false
postgres:
container_name: pgdb
image: postgres:9.6-alpine
environment:
- 'POSTGRES_ROOT_PASSWORD=postgres'
- 'POSTGRES_USER=postgres'
- 'POSTGRES_PASSWORD=postgres'
ports:
- "54321:5432"
I am using Springboot JPA Data 2.0 with below config data in my application.properties
spring.datasource.url= jdbc:postgresql://localhost:54321/java_learning
spring.datasource.username=postgres
spring.datasource.password=postgres
I can test that Both of the Images are up. Also from docker log and docker events, I see that postgres Container is running fine, even I can access it and also created a DB too.
But springboot container started but i died because it could not connect to postgress and throwing error below.
Unable to obtain connection from database: The connection attempt
failed
Note that my host machine already has Postgres on port 5432 thats why I did a port mapping ofr 54321:5432 on my postgres container. Here is Proof :) -
➜ springboot-api-demo git:(master) ✗ lsof -i:54321
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
com.docke 44345 shailendra.singh 18u IPv4 0xf62897fbdd69e31d 0t0 TCP *:54321 (LISTEN)
com.docke 44345 shailendra.singh 21u IPv6 0xf62897fbdd119975 0t0 TCP localhost:54321 (LISTEN)
➜ springboot-api-demo git:(master) ✗ lsof -i:5432
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 715 shailendra.singh 5u IPv6 0xf62897fbb43e03b5 0t0 TCP localhost:postgresql (LISTEN)
postgres 715 shailendra.singh 6u IPv4 0xf62897fbbaeea9bd 0t0 TCP localhost:postgresql (LISTEN)
I am not sure what is the problem. But my Springboot application is not able to connect my postgres container which is running fine with proper creadentials.
postgresql spring-boot docker docker-compose
I am building my first Springboot 2.0 application. I am trying to put my Springboot application into one docker container and my PostgresDB into another container.
My Dockerfile
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD springboot-api-demo-0.1*.jar app.jar
RUN sh -c 'touch /app.jar'
EXPOSE 9443
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/urandom -jar /app.jar" ]
My docker-compose.yml file
version: "2.1"
services:
springboot-api-demo:
image: "fw/springboot-api-demo"
mem_limit: 1024m
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=local
- AWS_REGION=local
- ENVIRONMENT=local
- AUTH_ENABLED=false
postgres:
container_name: pgdb
image: postgres:9.6-alpine
environment:
- 'POSTGRES_ROOT_PASSWORD=postgres'
- 'POSTGRES_USER=postgres'
- 'POSTGRES_PASSWORD=postgres'
ports:
- "54321:5432"
I am using Springboot JPA Data 2.0 with below config data in my application.properties
spring.datasource.url= jdbc:postgresql://localhost:54321/java_learning
spring.datasource.username=postgres
spring.datasource.password=postgres
I can test that Both of the Images are up. Also from docker log and docker events, I see that postgres Container is running fine, even I can access it and also created a DB too.
But springboot container started but i died because it could not connect to postgress and throwing error below.
Unable to obtain connection from database: The connection attempt
failed
Note that my host machine already has Postgres on port 5432 thats why I did a port mapping ofr 54321:5432 on my postgres container. Here is Proof :) -
➜ springboot-api-demo git:(master) ✗ lsof -i:54321
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
com.docke 44345 shailendra.singh 18u IPv4 0xf62897fbdd69e31d 0t0 TCP *:54321 (LISTEN)
com.docke 44345 shailendra.singh 21u IPv6 0xf62897fbdd119975 0t0 TCP localhost:54321 (LISTEN)
➜ springboot-api-demo git:(master) ✗ lsof -i:5432
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 715 shailendra.singh 5u IPv6 0xf62897fbb43e03b5 0t0 TCP localhost:postgresql (LISTEN)
postgres 715 shailendra.singh 6u IPv4 0xf62897fbbaeea9bd 0t0 TCP localhost:postgresql (LISTEN)
I am not sure what is the problem. But my Springboot application is not able to connect my postgres container which is running fine with proper creadentials.
postgresql spring-boot docker docker-compose
postgresql spring-boot docker docker-compose
edited Nov 22 at 10:52
Anthony Raymond
4,05232645
4,05232645
asked Nov 22 at 10:38
Shailendra2014
440316
440316
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
You are pointing your application towards localhost
, but this is not shared between containers.
To access another container you have to refer to its hostname
.
you should use the following datasource
url:
spring.datasource.url=jdbc:postgresql://pgdb:5432/java_learning
See this simple tutorial about connecting to a container from another container with docker compose: https://docs.docker.com/compose/gettingstarted/
No for that also It is not working. I tried with host as pgdb. It showing error Message : Connection to pgdb:54321 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. –
– Shailendra2014
Nov 22 at 11:07
try with 5432 instead of 54321
– Alien
Nov 22 at 11:07
add a comment |
up vote
2
down vote
Try with :
spring.datasource.url= jdbc:postgresql://pgdb:5432/java_learning
The postgres database is not running on localhost, it's running in the other container which has an other IP (yet unknown).
Thanksfully, docker-compose automatically create a network shared with all the other containers in this very docker-compose.yml, and you can magically use the service name as an hostname.
Also, you have a typo in the port, Postgres use 5432
by default, not 54321
No It is not working. I tried with host as pgdb. It showing error Message : Connection to pgdb:54321 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
– Shailendra2014
Nov 22 at 11:06
Woops, i missed an other error, the port is actually5432
not54321
. I've edited my answer
– Anthony Raymond
Nov 22 at 13:03
1
In addition to @AnthonyRaymond's answer, you actually don't need to expose the database port to be able to connect from thespringboot-api-demo
service, as the services would communicate via internal network anyway. You would need to expose it only if you will be connecting to the db port remotely from another machine.
– tftd
Nov 22 at 13:09
@tftd That's right !
– Anthony Raymond
Nov 22 at 13:49
add a comment |
up vote
0
down vote
You're missing networking configuration in your docker-compose.yml
specification. By using "networks" you can effectively communicate between containers by their service name (using dns, the service name as the hostname).
Here is an updated docker-compose.yml
:
version: "2.1"
services:
springboot-api-demo:
image: "fw/springboot-api-demo"
mem_limit: 1024m
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=local
- AWS_REGION=local
- ENVIRONMENT=local
- AUTH_ENABLED=false
networks:
- mynet
postgres:
container_name: pgdb
image: postgres:9.6-alpine
environment:
- 'POSTGRES_ROOT_PASSWORD=postgres'
- 'POSTGRES_USER=postgres'
- 'POSTGRES_PASSWORD=postgres'
ports:
- "54321:5432"
networks:
- mynet
networks:
mynet:
driver: bridge
Your database url should look like spring.datasource.url=jdbc:postgresql://postgres:5432/java_learning
(notice the hostname, postgres, is equal to that of the service name.
Also, I would highly advise upgradingdocker-compose
to >3.x
– yomateo
yesterday
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429062%2fdocker-springboot-container-can-not-connect-to-postgresql-container-connection%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You are pointing your application towards localhost
, but this is not shared between containers.
To access another container you have to refer to its hostname
.
you should use the following datasource
url:
spring.datasource.url=jdbc:postgresql://pgdb:5432/java_learning
See this simple tutorial about connecting to a container from another container with docker compose: https://docs.docker.com/compose/gettingstarted/
No for that also It is not working. I tried with host as pgdb. It showing error Message : Connection to pgdb:54321 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. –
– Shailendra2014
Nov 22 at 11:07
try with 5432 instead of 54321
– Alien
Nov 22 at 11:07
add a comment |
up vote
2
down vote
You are pointing your application towards localhost
, but this is not shared between containers.
To access another container you have to refer to its hostname
.
you should use the following datasource
url:
spring.datasource.url=jdbc:postgresql://pgdb:5432/java_learning
See this simple tutorial about connecting to a container from another container with docker compose: https://docs.docker.com/compose/gettingstarted/
No for that also It is not working. I tried with host as pgdb. It showing error Message : Connection to pgdb:54321 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. –
– Shailendra2014
Nov 22 at 11:07
try with 5432 instead of 54321
– Alien
Nov 22 at 11:07
add a comment |
up vote
2
down vote
up vote
2
down vote
You are pointing your application towards localhost
, but this is not shared between containers.
To access another container you have to refer to its hostname
.
you should use the following datasource
url:
spring.datasource.url=jdbc:postgresql://pgdb:5432/java_learning
See this simple tutorial about connecting to a container from another container with docker compose: https://docs.docker.com/compose/gettingstarted/
You are pointing your application towards localhost
, but this is not shared between containers.
To access another container you have to refer to its hostname
.
you should use the following datasource
url:
spring.datasource.url=jdbc:postgresql://pgdb:5432/java_learning
See this simple tutorial about connecting to a container from another container with docker compose: https://docs.docker.com/compose/gettingstarted/
edited Nov 22 at 11:08
answered Nov 22 at 11:04
Alien
4,51021024
4,51021024
No for that also It is not working. I tried with host as pgdb. It showing error Message : Connection to pgdb:54321 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. –
– Shailendra2014
Nov 22 at 11:07
try with 5432 instead of 54321
– Alien
Nov 22 at 11:07
add a comment |
No for that also It is not working. I tried with host as pgdb. It showing error Message : Connection to pgdb:54321 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. –
– Shailendra2014
Nov 22 at 11:07
try with 5432 instead of 54321
– Alien
Nov 22 at 11:07
No for that also It is not working. I tried with host as pgdb. It showing error Message : Connection to pgdb:54321 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. –
– Shailendra2014
Nov 22 at 11:07
No for that also It is not working. I tried with host as pgdb. It showing error Message : Connection to pgdb:54321 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. –
– Shailendra2014
Nov 22 at 11:07
try with 5432 instead of 54321
– Alien
Nov 22 at 11:07
try with 5432 instead of 54321
– Alien
Nov 22 at 11:07
add a comment |
up vote
2
down vote
Try with :
spring.datasource.url= jdbc:postgresql://pgdb:5432/java_learning
The postgres database is not running on localhost, it's running in the other container which has an other IP (yet unknown).
Thanksfully, docker-compose automatically create a network shared with all the other containers in this very docker-compose.yml, and you can magically use the service name as an hostname.
Also, you have a typo in the port, Postgres use 5432
by default, not 54321
No It is not working. I tried with host as pgdb. It showing error Message : Connection to pgdb:54321 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
– Shailendra2014
Nov 22 at 11:06
Woops, i missed an other error, the port is actually5432
not54321
. I've edited my answer
– Anthony Raymond
Nov 22 at 13:03
1
In addition to @AnthonyRaymond's answer, you actually don't need to expose the database port to be able to connect from thespringboot-api-demo
service, as the services would communicate via internal network anyway. You would need to expose it only if you will be connecting to the db port remotely from another machine.
– tftd
Nov 22 at 13:09
@tftd That's right !
– Anthony Raymond
Nov 22 at 13:49
add a comment |
up vote
2
down vote
Try with :
spring.datasource.url= jdbc:postgresql://pgdb:5432/java_learning
The postgres database is not running on localhost, it's running in the other container which has an other IP (yet unknown).
Thanksfully, docker-compose automatically create a network shared with all the other containers in this very docker-compose.yml, and you can magically use the service name as an hostname.
Also, you have a typo in the port, Postgres use 5432
by default, not 54321
No It is not working. I tried with host as pgdb. It showing error Message : Connection to pgdb:54321 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
– Shailendra2014
Nov 22 at 11:06
Woops, i missed an other error, the port is actually5432
not54321
. I've edited my answer
– Anthony Raymond
Nov 22 at 13:03
1
In addition to @AnthonyRaymond's answer, you actually don't need to expose the database port to be able to connect from thespringboot-api-demo
service, as the services would communicate via internal network anyway. You would need to expose it only if you will be connecting to the db port remotely from another machine.
– tftd
Nov 22 at 13:09
@tftd That's right !
– Anthony Raymond
Nov 22 at 13:49
add a comment |
up vote
2
down vote
up vote
2
down vote
Try with :
spring.datasource.url= jdbc:postgresql://pgdb:5432/java_learning
The postgres database is not running on localhost, it's running in the other container which has an other IP (yet unknown).
Thanksfully, docker-compose automatically create a network shared with all the other containers in this very docker-compose.yml, and you can magically use the service name as an hostname.
Also, you have a typo in the port, Postgres use 5432
by default, not 54321
Try with :
spring.datasource.url= jdbc:postgresql://pgdb:5432/java_learning
The postgres database is not running on localhost, it's running in the other container which has an other IP (yet unknown).
Thanksfully, docker-compose automatically create a network shared with all the other containers in this very docker-compose.yml, and you can magically use the service name as an hostname.
Also, you have a typo in the port, Postgres use 5432
by default, not 54321
edited Nov 22 at 13:50
answered Nov 22 at 10:52
Anthony Raymond
4,05232645
4,05232645
No It is not working. I tried with host as pgdb. It showing error Message : Connection to pgdb:54321 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
– Shailendra2014
Nov 22 at 11:06
Woops, i missed an other error, the port is actually5432
not54321
. I've edited my answer
– Anthony Raymond
Nov 22 at 13:03
1
In addition to @AnthonyRaymond's answer, you actually don't need to expose the database port to be able to connect from thespringboot-api-demo
service, as the services would communicate via internal network anyway. You would need to expose it only if you will be connecting to the db port remotely from another machine.
– tftd
Nov 22 at 13:09
@tftd That's right !
– Anthony Raymond
Nov 22 at 13:49
add a comment |
No It is not working. I tried with host as pgdb. It showing error Message : Connection to pgdb:54321 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
– Shailendra2014
Nov 22 at 11:06
Woops, i missed an other error, the port is actually5432
not54321
. I've edited my answer
– Anthony Raymond
Nov 22 at 13:03
1
In addition to @AnthonyRaymond's answer, you actually don't need to expose the database port to be able to connect from thespringboot-api-demo
service, as the services would communicate via internal network anyway. You would need to expose it only if you will be connecting to the db port remotely from another machine.
– tftd
Nov 22 at 13:09
@tftd That's right !
– Anthony Raymond
Nov 22 at 13:49
No It is not working. I tried with host as pgdb. It showing error Message : Connection to pgdb:54321 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
– Shailendra2014
Nov 22 at 11:06
No It is not working. I tried with host as pgdb. It showing error Message : Connection to pgdb:54321 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
– Shailendra2014
Nov 22 at 11:06
Woops, i missed an other error, the port is actually
5432
not 54321
. I've edited my answer– Anthony Raymond
Nov 22 at 13:03
Woops, i missed an other error, the port is actually
5432
not 54321
. I've edited my answer– Anthony Raymond
Nov 22 at 13:03
1
1
In addition to @AnthonyRaymond's answer, you actually don't need to expose the database port to be able to connect from the
springboot-api-demo
service, as the services would communicate via internal network anyway. You would need to expose it only if you will be connecting to the db port remotely from another machine.– tftd
Nov 22 at 13:09
In addition to @AnthonyRaymond's answer, you actually don't need to expose the database port to be able to connect from the
springboot-api-demo
service, as the services would communicate via internal network anyway. You would need to expose it only if you will be connecting to the db port remotely from another machine.– tftd
Nov 22 at 13:09
@tftd That's right !
– Anthony Raymond
Nov 22 at 13:49
@tftd That's right !
– Anthony Raymond
Nov 22 at 13:49
add a comment |
up vote
0
down vote
You're missing networking configuration in your docker-compose.yml
specification. By using "networks" you can effectively communicate between containers by their service name (using dns, the service name as the hostname).
Here is an updated docker-compose.yml
:
version: "2.1"
services:
springboot-api-demo:
image: "fw/springboot-api-demo"
mem_limit: 1024m
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=local
- AWS_REGION=local
- ENVIRONMENT=local
- AUTH_ENABLED=false
networks:
- mynet
postgres:
container_name: pgdb
image: postgres:9.6-alpine
environment:
- 'POSTGRES_ROOT_PASSWORD=postgres'
- 'POSTGRES_USER=postgres'
- 'POSTGRES_PASSWORD=postgres'
ports:
- "54321:5432"
networks:
- mynet
networks:
mynet:
driver: bridge
Your database url should look like spring.datasource.url=jdbc:postgresql://postgres:5432/java_learning
(notice the hostname, postgres, is equal to that of the service name.
Also, I would highly advise upgradingdocker-compose
to >3.x
– yomateo
yesterday
add a comment |
up vote
0
down vote
You're missing networking configuration in your docker-compose.yml
specification. By using "networks" you can effectively communicate between containers by their service name (using dns, the service name as the hostname).
Here is an updated docker-compose.yml
:
version: "2.1"
services:
springboot-api-demo:
image: "fw/springboot-api-demo"
mem_limit: 1024m
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=local
- AWS_REGION=local
- ENVIRONMENT=local
- AUTH_ENABLED=false
networks:
- mynet
postgres:
container_name: pgdb
image: postgres:9.6-alpine
environment:
- 'POSTGRES_ROOT_PASSWORD=postgres'
- 'POSTGRES_USER=postgres'
- 'POSTGRES_PASSWORD=postgres'
ports:
- "54321:5432"
networks:
- mynet
networks:
mynet:
driver: bridge
Your database url should look like spring.datasource.url=jdbc:postgresql://postgres:5432/java_learning
(notice the hostname, postgres, is equal to that of the service name.
Also, I would highly advise upgradingdocker-compose
to >3.x
– yomateo
yesterday
add a comment |
up vote
0
down vote
up vote
0
down vote
You're missing networking configuration in your docker-compose.yml
specification. By using "networks" you can effectively communicate between containers by their service name (using dns, the service name as the hostname).
Here is an updated docker-compose.yml
:
version: "2.1"
services:
springboot-api-demo:
image: "fw/springboot-api-demo"
mem_limit: 1024m
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=local
- AWS_REGION=local
- ENVIRONMENT=local
- AUTH_ENABLED=false
networks:
- mynet
postgres:
container_name: pgdb
image: postgres:9.6-alpine
environment:
- 'POSTGRES_ROOT_PASSWORD=postgres'
- 'POSTGRES_USER=postgres'
- 'POSTGRES_PASSWORD=postgres'
ports:
- "54321:5432"
networks:
- mynet
networks:
mynet:
driver: bridge
Your database url should look like spring.datasource.url=jdbc:postgresql://postgres:5432/java_learning
(notice the hostname, postgres, is equal to that of the service name.
You're missing networking configuration in your docker-compose.yml
specification. By using "networks" you can effectively communicate between containers by their service name (using dns, the service name as the hostname).
Here is an updated docker-compose.yml
:
version: "2.1"
services:
springboot-api-demo:
image: "fw/springboot-api-demo"
mem_limit: 1024m
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=local
- AWS_REGION=local
- ENVIRONMENT=local
- AUTH_ENABLED=false
networks:
- mynet
postgres:
container_name: pgdb
image: postgres:9.6-alpine
environment:
- 'POSTGRES_ROOT_PASSWORD=postgres'
- 'POSTGRES_USER=postgres'
- 'POSTGRES_PASSWORD=postgres'
ports:
- "54321:5432"
networks:
- mynet
networks:
mynet:
driver: bridge
Your database url should look like spring.datasource.url=jdbc:postgresql://postgres:5432/java_learning
(notice the hostname, postgres, is equal to that of the service name.
answered yesterday
yomateo
39219
39219
Also, I would highly advise upgradingdocker-compose
to >3.x
– yomateo
yesterday
add a comment |
Also, I would highly advise upgradingdocker-compose
to >3.x
– yomateo
yesterday
Also, I would highly advise upgrading
docker-compose
to >3.x– yomateo
yesterday
Also, I would highly advise upgrading
docker-compose
to >3.x– yomateo
yesterday
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429062%2fdocker-springboot-container-can-not-connect-to-postgresql-container-connection%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown