7

Here my DockerFile :-

FROM openjdk:10
ENV AQUILA_HOME /data/config
#USER root
#VOLUME /tmp
ADD a2i-web-1.0.0-SNAPSHOT.jar app.jar
#RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-jar","app.jar"]

My jar is spring boot application which requires postgres. I installed portgres locally and it is successfully runnning on my localhost. I am building DockerFile successfully by command

sudo docker build -t djtijare/a2i-web:v1 .

But while running it by command

sudo docker run -p 8080:8080 -t djtijare/a2i-web:v1

giving exception as :

Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

I am running this docker command from directory containing DockerFile and my jar Do I need to set any configuration to run postgres?

Dhanraj
  • 1,162
  • 4
  • 18
  • 45
  • 1
    Your app want to access postgres? Where is it? – atline May 30 '19 at 07:59
  • 1
    I installed postgres locally and I am building and running spring boot app using above docker commands – Dhanraj May 30 '19 at 08:01
  • 1
    Possible duplicate of [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – David Maze May 30 '19 at 09:57

2 Answers2

6

You can't access service which on the host using localhost from container, you had to use the ip address of your host to access.

This because: default docker will use bridge which will setup a internal network for your container, so when container use localhost, it doesn't mean the host, it mean the container self's network.

If insist on, a ugly solution is use --net=host.

Something like next:

sudo docker run --net=host -p 8080:8080 -t djtijare/a2i-web:v1

Then you can use localhost to visit host's service from container.

atline
  • 28,355
  • 16
  • 77
  • 113
  • It is helpful as it solved my problem but as you say it is really a ugly solution. – Dhanraj May 30 '19 at 08:55
  • What will other way of accessing it using IP – Dhanraj May 30 '19 at 08:56
  • As I said, you need to get the ip of the host, and directly use host ip to access the postgres. When you not use container, you also use this way if postgres not on the same machine of your app. – atline May 30 '19 at 08:57
  • https://stackoverflow.com/questions/56429466/ioexception-occurred-while-connecting-to-host-ip5432 – Dhanraj Jun 03 '19 at 14:38
  • no experience on k8s, just know it do not use docker bridge driver, customize a network which could across machines – atline Jun 03 '19 at 22:55
  • What's your host ip, also what's your docker0 ip? I use `python -m SimpleHTTPServer` to new a http server on host, and use `docker run --rm tutum/curl curl http://10.192.244.188:8000` to fetch, it's ok. Please confirm you use the correct host ip(that's your physical machine's ip, not related with docker), not the docker0 ip. – atline Jun 04 '19 at 06:05
1

What does the error mean?

For those who came here for the error, and not for the docker problem:

You get this error when psql is not installed (or cannot be reached).

Since you did install it in your localhost, the docker container is unable to reach the localhost with the settings. This is nothing new, the other answer is years old, just making clearer what the error itself means.

PS

I had the same error in DBeaver when I thought that DBeaver would install the PostgreSQL database driver as soon as I make a connection. Turns out that you have to install psql yourself, make a host in pgAdmin 4 or in the console, and then, you can reach it from DBeaver.

questionto42
  • 7,175
  • 4
  • 57
  • 90