Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgres container throws "forward host lookup failed: Unknown host"

I'm working with docker image that uses Python3.6 as its base. All of the sudden it started crashing (exiting right after start up). So I bashed into the container and found out that it crashes because connection to containerized postgres database fails all of the sudden. The only error output I managed to get is forward host lookup failed: Unknown host which isn't telling me much.

entrypoint.sh:

echo "Waiting for postgres..."

while ! nc -z users-db 5432; do
  sleep 0.1
done

echo "PostgreSQL started"

python manage.py run -h 0.0.0.0

error output:

Waiting for postgres...
users-db: forward host lookup failed: Unknown host
users-db: forward host lookup failed: Unknown host
users-db: forward host lookup failed: Unknown host
...
...

Dockerfile:

FROM python:3.6.9-slim

LABEL maintainer="abc"

RUN apt-get update && \
    apt-get install -y netcat && \
    apt-get clean

WORKDIR /usr/src/app

COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

COPY ./entrypoint.sh /usr/src/app/entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh


COPY . /usr/src/app

CMD ["/usr/src/app/entrypoint.sh"]

What strikes me about this is that it worked wonderfully until now and without me making any changes to the database container the connection failed.

What can I do to troubleshoot this ? If you need to review any files just ask and I'll share it here.

docker ps:

72d344cc61bf        tdd_nginx           "nginx -g 'daemon of…"   25 minutes ago      Restarting (1) 55 seconds ago                            tdd_nginx_1
8ee2f8082e69        tdd_client          "npm start"              26 minutes ago      Up 25 minutes                   0.0.0.0:3007->3000/tcp   tdd_client_1
1ccfc3ca5600        tdd_users-db        "docker-entrypoint.s…"   26 minutes ago      Up 26 minutes                   0.0.0.0:5435->5432/tcp   tdd_users-db_1
-->  62af29277b78        tdd_users           "/bin/bash -s"           22 minutes ago      Exited (130) 2 minutes ago   # <-- keeps crashing

docker-compose file:

version: '3.7'

services:
  users:
    build:
      context: ./services/users
      dockerfile: Dockerfile
    volumes:
      - './services/users:/usr/src/app'
    ports:
      - 5001:5000
    environment:
      - FLASK_ENV=development
      - APP_SETTINGS=project.config.DevelopmentConfig
      - DATABASE_URL=postgres://postgres:postgres@users-db:5432/users_dev
      - DATABASE_TEST_URL=postgres://postgres:postgres@users-db:5432/users_test
      - SECRET_KEY=bart_simpson
    depends_on:
      - users-db

  client:
    build:
      context: ./services/client
      dockerfile: Dockerfile
    volumes:
      - './services/client:/usr/src/app'
      - '/usr/src/app/node_modules'
    ports:
      - 3007:3000
    environment:
      - NODE_ENV=development
      - REACT_APP_USERS_SERVICE_URL=${REACT_APP_USERS_SERVICE_URL}
    depends_on:
      - users

  nginx:
    build:
      context: ./services/nginx
      dockerfile: Dockerfile
    restart: always
    ports:
      - 80:80
    depends_on:
      - users
      - client

  users-db:
    build:
      context: './services/users/project/db'
      dockerfile: Dockerfile
    ports:
      - 5435:5432
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
like image 357
mr_incredible Avatar asked Feb 21 '26 17:02

mr_incredible


1 Answers

Problem was that I've got pyjwt library installed for generating web tokens and I used pyjwt.encode() instead of jwt.encode() in my code. That made a major difference to the functionality of the connection between containers. Still don't know why though. Containers are now running again. If somebody will vote to close this topic I'll understand as nobody would've guessed this.

like image 93
mr_incredible Avatar answered Feb 24 '26 06:02

mr_incredible