# Cannot access postgresql container from another container

**URL:** <https://forums.docker.com/t/cannot-access-postgresql-container-from-another-container/30971>\
**Category:** Docker Desktop\
**Tags:** macos\
**Created:** [April 12, 2017, 8:28pm UTC](https://forums.docker.com/t/cannot-access-postgresql-container-from-another-container/30971 "2017-04-12T20:28:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jvervier](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@jvervier](https://forums.docker.com/u/jvervier)\
**Post date:** [April 12, 2017, 8:28pm UTC](https://forums.docker.com/t/cannot-access-postgresql-container-from-another-container/30971/1 "2017-04-12T20:28:27Z")

</div>

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 🙂

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).

---

<div class="post-metadata">

**Author:** ![think](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/think/32/14641_2.png) [@think](https://forums.docker.com/u/think)\
**Post date:** [April 12, 2017, 10:56pm UTC](https://forums.docker.com/t/cannot-access-postgresql-container-from-another-container/30971/2 "2017-04-12T22:56:12Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![jvervier](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@jvervier](https://forums.docker.com/u/jvervier)\
**Post date:** [April 13, 2017, 5:34pm UTC](https://forums.docker.com/t/cannot-access-postgresql-container-from-another-container/30971/3 "2017-04-13T17:34:31Z")

</div>

Hello !

Thank you for your help 🙂

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/](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.

---

<div class="post-metadata">

**Author:** ![think](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/think/32/14641_2.png) [@think](https://forums.docker.com/u/think)\
**Post date:** [April 13, 2017, 8:37pm UTC](https://forums.docker.com/t/cannot-access-postgresql-container-from-another-container/30971/4 "2017-04-13T20:37:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jvervier](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@jvervier](https://forums.docker.com/u/jvervier)\
**Post date:** [April 13, 2017, 8:51pm UTC](https://forums.docker.com/t/cannot-access-postgresql-container-from-another-container/30971/5 "2017-04-13T20:51:39Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![think](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/think/32/14641_2.png) [@think](https://forums.docker.com/u/think)\
**Post date:** [April 14, 2017, 6:00am UTC](https://forums.docker.com/t/cannot-access-postgresql-container-from-another-container/30971/6 "2017-04-14T06:00:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jvervier](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@jvervier](https://forums.docker.com/u/jvervier)\
**Post date:** [April 14, 2017, 1:24pm UTC](https://forums.docker.com/t/cannot-access-postgresql-container-from-another-container/30971/7 "2017-04-14T13:24:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jvervier](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@jvervier](https://forums.docker.com/u/jvervier)\
**Post date:** [April 14, 2017, 8:18pm UTC](https://forums.docker.com/t/cannot-access-postgresql-container-from-another-container/30971/8 "2017-04-14T20:18:42Z")

</div>

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

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

thank you

---

<div class="post-metadata">

**Author:** ![greyshine](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/greyshine/32/22092_2.png) [@greyshine](https://forums.docker.com/u/greyshine)\
**Post date:** [December 17, 2018, 10:38am UTC](https://forums.docker.com/t/cannot-access-postgresql-container-from-another-container/30971/9 "2018-12-17T10:38:26Z")

</div>

If I get it right it will not work as stated here: [https://github.com/docker/for-mac/issues/2716](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.
