1

I have a docker-compose file that looks like this

version: "3.7"

services:
  app:
    stdin_open: true  
    tty: true 
    build:
      context: .
      dockerfile: app.Dockerfile
    volumes:
      - ${HOST_SAVE_DIRC}:${CONTAINER_SAVE_DIRC}
    depends_on:
      - postgres

  postgres:
    image: 'postgres'
    environment:
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_HOST_AUTH_METHOD=trust
    restart: always
    expose:
      - "5432"

where variables like POSTGRES_USER are entries from a env file. app.Dockerfile looks like

FROM python:3.8.3-slim-buster
COPY src /src/
COPY init.sql .
COPY .env .
COPY run.sh run.sh
COPY requirements.txt .
RUN ls -a
RUN pip install --no-cache-dir -r requirements.txt

The containers are created, then the user is logged into the app container w/ the main function of the program being called - this is when the database calls

From the app container I am attempting to connect to the postgres container via psycopg2. However when I attempt to do so, I receive the following error:

psycopg2.OperationalError: could not connect to server: No route to host
    Is the server running on host "postgres" (172.22.0.2) and accepting
    TCP/IP connections on port 5432?

using a psycopg2 call that looks like

with psy.connect(host='postgres', port=5432, user='postgres', password='postgres') as conn:
    ...

the entries of this psycopg2 call match the env file given to the docker-compose file.

My understanding is that Postgres uses port 5432 by default. Also that when docker-compose creates the two containers - it creates a docker network for those containers name DIR_default where DIR is the name of the directory the docker-compose file lives in, where each container can be accessed with using the name listed in the docker-compose file ('postgres' and 'app' in these cases).

Among various tries:

  • I've checked and the database isn't going down between the container being created and the user being exec'd in.

  • I've tried various little changes like changing the container names, postgres login info, etc.

  • I've tried linking the postgres container name explicitly with link: "postgres:postgres".

  • Other solutions suggested here

Any help would be greatly appreciated! I see no reason why something as simple as this should be occurring, but also here I am.


Edit:

Pinging the Postgres container from the app container appears to be working when running docker exec app ping postgres_container_name. Is this a sign that the Docker network is set up correctly and the issue is something of mine?


Edit 2:

Tried clearing all images and containers, then restarting the Docker daemon and afterwards my PC. No change in either case.

For reference, the ping command looked like

docker exec python-app ping name_given_to_postgres_container

returning various statements which looked like

64 bytes from name_given_to_postgres_container.project_name_default (172.18.0.3): icmp_seq=1 ttl=64 time=0.090 ms

which unless I am mistaken, I believe is signalling a succesful ping.


The top level .env file provided to docker-compose

HOST_SAVE_DIRC=~/python_projects/project_directory/directory_in_project
CONTAINER_SAVE_DIRC=/pdfs

POSTGRES_DB=project_name  # same as project_directory
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_PORT=5432

Here is the requirements.txt file for the Python app as well

certifi==2020.4.5.1
chardet==3.0.4
idna==2.9
psycopg2-binary==2.8.5
read-env==1.1.0
requests==2.23.0
urllib3==1.25.9

Exec-ing into the Postgres container with docker exec -it container_id bash and running psql -U postgres appears to be successful - even with restart: always removed. I can also see the database named in the docker-compose file is also created. I feel confident in saying this container isn't dying spontaneously.

However, hitting the 5432 port on the Postgres container with netcat via nc name_given_to_postgres_container 5432-5433 returns an error similar to the one returned by psycopg2

arxivist_postgres_1 [172.22.0.3] 5433 (?) : No route to host
arxivist_postgres_1 [172.22.0.3] 5432 (postgresql) : No route to host

The same error is also returned with curl. So my guess the issue isn't with the Postgres container directly, psycopg2, or the host-name - but something with the port?


Edit 3:

As a last attempt to fix this project, the full project this post is referring to is posted at this link. If anyone would like to download the repo and try building the docker containers themselves via ./start.sh - that might be just what is needed to find a solution!

J. Auon
  • 189
  • 2
  • 12
  • Please share the env file provided to docker-compose. If ping is successful then probably the issue lies in `psycopg2`. – leopal Jun 19 '20 at 07:30
  • Can you connect to the container that is running postgres (`sudo docker exec -it container-id bash`) and check if you can connect to the database from inside? It could be that the postgres settings don't allow outside connections – borisdonchev Jun 19 '20 at 10:09
  • It's worth noting that DNS resolution works – the Python container knows what the `postgres` host name is, which suggests the Docker wiring is correct. When you run this, is the PostgreSQL container fully started up? If you're repeatedly running `docker-compose down; docker-compose up` everything will start from scratch and it's likely the database just isn't ready yet. – David Maze Jun 19 '20 at 11:54
  • @leopal added the .env file. Believe the issue is simply in trying to connect to the 5432 port on the Postgres container? – J. Auon Jun 19 '20 at 13:44
  • @J.Auon Check David's comment. Ensure that the postgres container is ready when you try to connect through python. – leopal Jun 19 '20 at 13:52
  • @leopal I believe it is. The build process shows the Postgres container is ready to accept connections, and attempts to ping from the app container are successful. – J. Auon Jun 19 '20 at 15:22

1 Answers1

1

I thought I had Docker setup on my machine, which runs Fedora 32. However as I came to realize from this article, setting up Docker on Fedora 32 requires some extra steps I was not previously aware of.

Specifically for this issue, the command listed in the article to add Docker to whitelist Docker on the local network's firewall with the command

sudo firewall-cmd --permanent --zone=FedoraWorkstation --add-masquerade

So I believe the root cause of my issue was simply my app container being blocked from accessing the postgres container by the firewall. Making the above change made the program work finally!

J. Auon
  • 189
  • 2
  • 12