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:

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:[email protected]:5432/users_dev
- DATABASE_TEST_URL=postgres://postgres:[email protected]: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?

WordPress in Docker. Part 1: Dockerization

WordPress in Docker. Part 1: Dockerization

This entry-level guide will tell you why and how to Dockerize your WordPress projects.

This entry-level guide will tell you why and how to Dockerize your WordPress projects.

Linking python app docker and postgress docker

I have two docker containers running by the following commands:

I have two docker containers running by the following commands:

  • docker run --name postgres -v "/Users/xxx/Desktop/Coding/DockerMounting":/home/ -e POSTGRES_PASSWORD=xyz -d postgres
  • docker run -it -v "/Users/xxx/Desktop/Coding/DockerMounting":/home/t -p 5000:5000 --name some-app --link postgres:postgres -d xxx/ubuntu:latest

I have created the necessary user, database and table in my postgres (psql) container.

I am trying to run a python script:

import os

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))

def main():
flights = db.execute("SELECT origin, destination, duration FROM flights").fetchall()
for flight in flights:
print(f"{flight.origin} to {flight.destination}, {flight.duration} minutes.")

if name == "main":
main()

I get the following error:

  File "list.py", line 6, in <module>
engine = create_engine(os.getenv("DATABASE_URL"))
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/init.py", line 435, in create_engine
return strategy.create(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/strategies.py", line 56, in create
plugins = u._instantiate_plugins(kwargs)

I know one issue is that I need to set DATABASE_URL env - but I am not sure what should be that value

What is Docker | Docker Tutorial for Beginners

What is Docker | Docker Tutorial for Beginners

This DevOps Docker Tutorial on what is docker will help you understand how to use Docker Hub, Docker Images, Docker Container & Docker Compose. This tutorial explains Docker's working Architecture and Docker Engine in detail.

This Docker tutorial also includes a Hands-On session around Docker by the end of which you will learn to pull a centos Docker Image and spin your own Docker Container. You will also see how to launch multiple docker containers using Docker Compose. Finally, it will also tell you the role Docker plays in the DevOps life-cycle.

The Hands-On session is performed on an Ubuntu-64bit machine in which Docker is installed.