Docker Community Forums

Share and learn in the Docker community.

Docker-Compose+ PostgreSQL: Name or service not known

Hi! I’m new in Docker/Docker-Compose and try to build my first app. I use docker-compose up for my project, and all the container was successfully built except the postgres container. How can I fix it? Thanks in advance!

INFO     | __main__:before_log:21 - Retrying __main__.wait_postgres in 2.0 seconds as it raised [Errno -2] Name or service not known

Dockerfile:

FROM python:3.9 as production
LABEL maintainer="Alex Root Junior <jroot.junior@gmail.com>" \
      description="Telegram Bot"

ENV PYTHONPATH "${PYTHONPATH}:/app"
ENV PATH "/app/scripts:${PATH}"

EXPOSE 80
WORKDIR /app

COPY Pipfile* /app/
RUN pip install pipenv && \
    pipenv install --system --deploy
ADD . /app/
RUN chmod +x scripts/* && \
    pybabel compile -d locales -D bot

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["run-webhook"]

docker-compose.yml:

version: '3.7'

services:
  bot:
    container_name: bot
    build:
      context: .
    env_file:
      - .env
    restart: on-failure
    stop_signal: SIGINT
    depends_on:
      redis:
        condition: service_started
      postgres:
        condition: service_healthy
      api:
        condition: service_started
      nginx:
        condition: service_started

  redis:
    env_file:
      - .env
    container_name: bot-redis
    image: redis:5-alpine
    restart: on-failure
    volumes:
      - redis-data:/data

  postgres:
    env_file:
      - .env
    container_name: bot-postgres
    image: postgres:latest
    volumes:
      - postgres-data:/var/lib/postgresql/data/
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres" ]
      interval: 5s
      timeout: 5s
      retries: 5
    ports:
    - 5432:5432

  api:
    env_file:
      - .env
    image: aiogram/telegram-bot-api:latest
    container_name: bot-api
    restart: always
    volumes:
      - telegram-bot-api-data:/var/lib/telegram-bot-api

  nginx:
    container_name: bot-nginx
    image: nginx:1.19-alpine
    restart: always
    depends_on:
      - api
    volumes:
      - telegram-bot-api-data:/var/lib/telegram-bot-api
      - ./docker/nginx:/etc/nginx/conf.d/
    ports:
      - 8084:8084

volumes:
  redis-data:
  postgres-data:
  telegram-bot-api-data:

.env

COMPOSE_PROJECT_NAME=aiogram_bot
COMPOSE_FILE=docker-compose.yml

TELEGRAM_TOKEN=token
BOT_PUBLIC_PORT=8084
DOMAIN=localhost
WEBHOOK_BASE_PATH=/webhook

REDIS_HOST=redis
REDIS_PORT=6379
REDIS_DB_FSM=0
REDIS_DB_JOBSTORE=1

POSTGRES_HOST=postgres
POSTGRES_PORT=5423
POSTGRES_PASSWORD=postgres
POSTGRES_USER=postgres
POSTGRES_DB=postgres

TELEGRAM_API_ID=id
TELEGRAM_API_HASH=hash
TELEGRAM_STAT=1
TELEGRAM_LOCAL=1

Full log:

bot-postgres | The files belonging to this database system will be owned by user "postgres".
bot-postgres | This user must also own the server process.
bot-postgres |
bot-postgres | The database cluster will be initialized with locale "en_US.utf8".
bot-postgres | The default database encoding has accordingly been set to "UTF8".
bot-postgres | The default text search configuration will be set to "english".
bot-postgres |
bot-postgres | Data page checksums are disabled.
bot-postgres |
bot-postgres | fixing permissions on existing directory /var/lib/postgresql/data ... ok
bot-postgres | creating subdirectories ... ok
bot-postgres | selecting dynamic shared memory implementation ... posix
bot-nginx   | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
bot-nginx   | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
bot-postgres | selecting default max_connections ... 100
bot-redis   | 1:M 11 May 2021 16:11:44.418 * Running mode=standalone, port=6379.
bot-redis   | 1:M 11 May 2021 16:11:44.418 # Server initialized
bot-redis   | 1:M 11 May 2021 16:11:44.418 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
bot-redis   | 1:M 11 May 2021 16:11:44.418 * Ready to accept connections
bot-nginx   | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
bot-postgres | selecting default shared_buffers ... 128MB
bot-postgres | selecting default time zone ... Etc/UTC
bot-nginx   | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
bot-nginx   | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
bot-postgres | creating configuration files ... ok
bot-nginx   | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
bot-nginx   | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
bot-postgres | running bootstrap script ... ok
bot-nginx   | /docker-entrypoint.sh: Configuration complete; ready for start up
bot-postgres | performing post-bootstrap initialization ... ok
bot-postgres | syncing data to disk ... initdb: warning: enabling "trust" authentication for local connections
bot-postgres | You can change this by editing pg_hba.conf or using the option -A, or
bot-postgres | --auth-local and --auth-host, the next time you run initdb.
bot-postgres | ok
bot-postgres |
bot-postgres |
bot-postgres | Success. You can now start the database server using:
bot-postgres |
bot-postgres |     pg_ctl -D /var/lib/postgresql/data -l logfile start
bot-postgres |
bot-postgres | waiting for server to start....2021-05-11 16:11:46.627 UTC [47] LOG:  starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
bot-postgres | 2021-05-11 16:11:46.628 UTC [47] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
bot-postgres | 2021-05-11 16:11:46.632 UTC [48] LOG:  database system was shut down at 2021-05-11 16:11:46 UTC
bot-postgres | 2021-05-11 16:11:46.636 UTC [47] LOG:  database system is ready to accept connections
bot-postgres |  done
bot-postgres | server started
bot-postgres |
bot-postgres | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
bot-postgres |
bot-postgres | 2021-05-11 16:11:46.756 UTC [47] LOG:  received fast shutdown request
bot-postgres | waiting for server to shut down....2021-05-11 16:11:46.758 UTC [47] LOG:  aborting any active transactions
bot-postgres | 2021-05-11 16:11:46.760 UTC [47] LOG:  background worker "logical replication launcher" (PID 54) exited with exit code 1
bot-postgres | 2021-05-11 16:11:46.761 UTC [49] LOG:  shutting down
bot-postgres | 2021-05-11 16:11:46.773 UTC [47] LOG:  database system is shut down
bot-postgres |  done
bot-postgres | server stopped
bot-postgres |
bot-postgres | PostgreSQL init process complete; ready for start up.
bot-postgres |
bot-postgres | 2021-05-11 16:11:46.877 UTC [1] LOG:  starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
bot-postgres | 2021-05-11 16:11:46.878 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
bot-postgres | 2021-05-11 16:11:46.878 UTC [1] LOG:  listening on IPv6 address "::", port 5432
bot-postgres | 2021-05-11 16:11:46.880 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
bot-postgres | 2021-05-11 16:11:46.883 UTC [66] LOG:  database system was shut down at 2021-05-11 16:11:46 UTC
bot-postgres | 2021-05-11 16:11:46.886 UTC [1] LOG:  database system is ready to accept connections

your application cannot resolve the service dns name

I see that you have a postgres_host = postgres variable in your env file and your database container is called bot-postgres.

try to change the postgres_host variable and enter the name of the container, i.e. bot-postgres.

probably your application has this variable entered, but there is no container under the name postgres, so in the log you see a problem with dns.