Docker-compose and Postgres : Name does not resolve

In docker, I am trying to configure Postgres, get it up and running in another container, and link it to my users service, set up with the following structure:

docker-compose-dev.yml
services/
        users/
             manage.py
             Dockerfile-dev
             entrypoint.sh
             project/
                    __init__.py
                    db/
                      create.sql
                      Dockerfile

docker-compose-dev.yml

version: '3.7'

services:

users:
build:
context: ./services/users
dockerfile: Dockerfile-dev
volumes:
- ‘./services/users:/usr/src/app’
ports:
- 5001:5000
environment:
- FLASK_APP=project/init.py
- 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
depends_on:
- users-db

users-db:
build:
context: ./services/users/project/db
dockerfile: Dockerfile
ports:
- 5435:5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres

Dockerfile-dev

# base image
FROM python:3.7.2-alpine

install dependencies

RUN apk update &&
apk add --virtual build-deps gcc python-dev musl-dev &&
apk add postgresql-dev &&
apk add netcat-openbsd

set working directory

WORKDIR /usr/src/app

add and install requirements

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

add entrypoint.sh

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

add app

COPY . /usr/src/app

run server

CMD [“/usr/src/app/entrypoint.sh”]

Here I try to extend the official Postgres image by adding a SQL file to the “docker-entrypoint-initdb.d” directory in the container.

Dockerfile

# base image
FROM postgres:11.1-alpine

run create.sql on init

ADD create.sql /docker-entrypoint-initdb.d

create.sql

CREATE DATABASE users_prod;
CREATE DATABASE users_dev;
CREATE DATABASE users_test;

Since the “users” service is dependent not only on the container being up and running but also the actual Postgres instance being up and healthy, I added an entrypoint.sh file to “users”:

entrypoint.sh

#!/bin/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

manage.py

from flask.cli import FlaskGroup
from project import app, db

cli = FlaskGroup(app)
@cli.command(‘recreate_db’)
def recreate_db():
db.drop_all()
db.create_all()
db.session.commit()
if name == ‘main’:
cli()

I run and sucessfully build it with:

docker-compose -f docker-compose-dev.yml up -d --build

but Im having some network issue. If i run:

docker-compose -f docker-compose-dev.yml logs

I get:

Attaching to dev2_users_1
users_1 | Waiting for postgres…
users_1 | nc: getaddrinfo: Name does not resolve
users_1 | nc: getaddrinfo: Name does not resolve
users_1 | nc: getaddrinfo: Name does not resolve
users_1 | nc: getaddrinfo: Name does not resolve
users_1 | nc: getaddrinfo: Name does not resolve
(…)

Can somebody please point me in the right direction here? What am I missing in the code above?

#docker #postgresql

4 Likes38.55 GEEK