Docker Community Forums

Share and learn in the Docker community.

Cannot access postgresql container from another container


(Jvervier) #1

Hello,

I’m encoutering a strange things on my Mac OS X. I’m quite new to Docker, so even with what I read, I could miss something…

I’ve two containers one with my Spring Boot application and another one with the Database (Postgresql).

Here is first the two dockerfile :
Postgresql Dockerfile :

FROM postgres
ENV POSTGRES_DB mydb
ENV POSTGRES_USER dbuser
ENV POSTGRES_PASSWORD mypassword
COPY psql_dump.sql /docker-entrypoint-initdb.d/

Spring Boot Dockerfile :

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD myPackagedJar.jar app.jar
RUN sh -c 'touch /app.jar’
ENV JAVA_OPTS=""
ENTRYPOINT [ “sh”, “-c”, “java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar” ]

And here are the command line I executed (with no errors) :

docker build -t my_postgresql .
docker run -i --publish 127.0.0.1:5433:5432 -t --name my_postgresql my_postgresql

docker build -t my_api .
docker run -p 8080:8080 -t --name my_api my_api

And I’m getting this error when a I’m trying part of rest call that lead to database connection and data :

Caused by: org.postgresql.util.PSQLException: Connection to localhost:5433 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

For information, I’m succeeding to connect to the postgresql DB in the container with a local pgAdmin :slight_smile:

Thanks in advance for your help because I don’t succeed to find reason/solution even with lot of research. I certainly forget one little detail… (I hope).


(Think) #2

docker run -p 8080:8080 -t --name my_api --net=host my_api could work

or if you replace localhost with your host ip / name.

Your API container has it’s own network. So its localhost is not your localhost.


(Jvervier) #3

Hello !

Thank you for your help :slight_smile:

I made the change (by looking in the documentation too) to get that command line :

docker run -p 8080:8080 -t --name my_api_name --net=host my_api

It’s running, but I cannot access anymore my application on http://localhost:8080/ :-/

When you’re speaking about replacing the hostname with host ip, do you mean my computer IP? Sorry I’m really quite new.


(Think) #4

sorry…-p cannot be mixed with --net=host.
but if you do --net=host and your app opens port 8080, then you should be able to access it


(Jvervier) #5

Ok, it’s confirming what I read. No problem ! But it’s strange a such incompatibilities does not give an error :confounded:

I tried this without the -p but I cannot access to my api on localhost:8080 :frowning:


(Think) #6

it will give you an error…doesn’t it? if not please try to double check that you call it correct.


(Jvervier) #7

I can ensure, I don’t get any error when I’m launching the run commands :-/

I tried with fixed IP address, but as soon as I’m using -net option (and so cannot use -p for 8080) I cannot access anymore my api with a postman to the address : http://localhot:8080/myapi/

And when I’m looking in Kitematic, I have indeed no configuration for Port section (but for my container postgresql, I have one) :-/


(Jvervier) #8

Hello, In fact I have another issue, it will be another topic if necessary, my initial problem received a respond :slight_smile:

-Considering to use the host IP
-using host for --network

thank you


#9

If I get it right it will not work as stated here: https://github.com/docker/for-mac/issues/2716

–net=host and -p port1:port2 will not work on mac on the same time.

Sorry, but I can’t provide a solution.