Cannot access postgresql container from another container


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
COPY psql_dump.sql /docker-entrypoint-initdb.d/

Spring Boot Dockerfile :

FROM frolvlad/alpine-oraclejdk8:slim
ADD myPackagedJar.jar app.jar
RUN sh -c ‘touch /app.jar’
ENTRYPOINT [ “sh”, “-c”, “java $JAVA_OPTS -jar /app.jar” ]

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

docker build -t my_postgresql .
docker run -i --publish -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).

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.

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.

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

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:

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

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) :-/

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

If I get it right it will not work as stated here:

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

Sorry, but I can’t provide a solution.