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
config.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()
config.py
import os
class BaseConfig:
"""Base configuration"""
TESTING = False
SQLALCHEMY_TRACK_MODIFICATIONS = False
class DevelopmentConfig(BaseConfig):
"""Development configuration"""
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
class TestingConfig(BaseConfig):
"""Testing configuration"""
TESTING = True
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_TEST_URL')
class ProductionConfig(BaseConfig):
"""Production configuration"""
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
I run and successfully build it with:
docker-compose -f docker-compose-dev.yml up -d --build
but I'm 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
(...)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ca5718c9867 dev2_users "/usr/src/app/entryp…" 2 minutes ago Up 2 minutes 0.0.0.0:5001->5000/tcp dev2_users_1
What am I missing in the code above?
You need to install bind-tools
in Dockerfile-dev
for Name does not resolve
problem:
apk add bind-tools
Test:
/usr/src/app # nslookup users-db
nslookup: can't resolve '(null)': Name does not resolve
Name: users-db
Address 1: 172.20.0.2 stc_users-db_1_f435854a8c88.stc_default
/usr/src/app # apk add bind-tools
(1/4) Installing json-c (0.13.1-r0)
(2/4) Installing libxml2 (2.9.8-r1)
(3/4) Installing bind-libs (9.12.3-r0)
(4/4) Installing bind-tools (9.12.3-r0)
Executing busybox-1.28.4-r3.trigger
OK: 188 MiB in 62 packages
/usr/src/app # nslookup users-db
Server: 127.0.0.11
Address: 127.0.0.11#53
Non-authoritative answer:
Name: users-db
Address: 172.20.0.2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With