0

I'm a new user of docker I have build my app and now I want to test the image of my app with postgres since it using a postgres Database. I'm using a postgres image too.

This is my image:

docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

my_app 2.5-SNAPSHOT 6416734dcf5a 53 years ago 518MB

This is how i launch postgres:

docker run --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres

I tried to run my_app like this:

 docker run --name my-app --link my-postgres:postgres -p 8080:8080 -d 6416734dcf5a

But i got this error:

org.flywaydb.core.internal.exception.FlywaySqlException: Unable to obtain connection from database:

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

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

SQL State : 08001

Error Code : 0

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

I was wondering if there is a way to launch my_app in a network, so that I can avoid this error.

Lam
  • 23
  • 1
  • 6
  • The problem is the connection string you provided in your application container. You provided `localhost`, but `localhost` means this container, so in your case your application looks for a database in my_app container, and you need to change it. Also, if the database container is a basic database container that you have build from scratch, then you might need to configure `listen_addresses` and `pg_hba` settings. – Umut TEKİN Jul 27 '23 at 09:04
  • [This answer](https://stackoverflow.com/questions/30545023/how-to-communicate-between-docker-containers-via-hostname/35184695#35184695) in particular walks through the steps of creating a network and attaching both containers to it. You should not use `docker run --link` on modern Docker, and the other container will not be `localhost`. – David Maze Jul 27 '23 at 10:19

0 Answers0