0

I'm running a composition of docker containers of a preexisting project on Ubuntu 20. One container for apache2, one for postresql. I can connect to the webserver through my browser and the webserver container establishes a connection to the postgresql container - so far everything works.

The problem is: I can't establish a connection from my host machine to the postgresql database using php. Whenever I try, I get an password authentication failed for user .. error.

I know that the php code, the username and the password are correct, as all three work when I execute them from within the webserver container.

$db_connection = pg_connect("host=db dbname=MYDB user=MYUSER password=MYPASS");

I configured /etc/hosts on my host to resolve "db" to 127.0.0.1 and also tried using host=localhost. Both didn't help.

Am I getting something wrong about how docker works? I found a couple of OSX solutions which were talking about docker containers running in virtual machines and therefore not being accessible from the host... Which is confusing, as apache2 is reachable from my host.

I did my research on stackoverflow tried solutions from e.g. Connecting to Postgresql in a docker container from outside but they didn't work for me.

  • 1
    Did you expose the port ? in docker you should expose the port so that it can be accessible from the outside, check "-p" argument when using docker run. For example: "docker run -p 8000:8000" will make the port 8000 of your container accessible on port 8000 of your host machine. – thibaultbl Sep 15 '20 at 15:00
  • You wrote "I can't establish connection using php" - can you connect using `psql` or any other way? How do you `docker run `? What you see with `docker container inspect [your container name` ? If you run `docker run ` with `--network=host` - can you access db? – Alex Yu Sep 15 '20 at 15:11
  • @AlexYu: I tried psql as well as php. I use docker-composer to start my containers. When I inspect the container then I see `"Ports": {"5432/tcp": null}`, so the port seems to be not exposed (but is reachable from the webserver container). That seems to be the root of the problem. I tried adding `ports: - "5432:5432"` to my docker-composer.yml, but then the container doesn't start up complaining that `Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use'"`. I understand now, that docker has an internal network, so now it's about how to reach into it. – Florian Metzger-Noel Sep 15 '20 at 16:05
  • @thibaultbl I'm using docker-composer to start my postgresql container, so I'm dependent on the yml file to configure it. I used the `ports: - "5432:5432"` option, which should be equivalent to `-p 5432:5432`, but it causes an `Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use'".` error. – Florian Metzger-Noel Sep 15 '20 at 16:11
  • I learned now, that a docker-compose creates it's own network, so the question is now really about how to reach into that network to postgresql. Sounds to me like a routine job, I hope someone knows how to do that. – Florian Metzger-Noel Sep 15 '20 at 16:13
  • The network in docker compose refer to docker using container name. If you want to reach a postgres docker name "my_postgres" on port 8000, you can reach it using postgres:8000 – thibaultbl Sep 16 '20 at 06:58
  • the "Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use'" mean that port is already used, try another port like 5433, 5435, ... – thibaultbl Sep 16 '20 at 12:28

0 Answers0