Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#259): docker-compose error on building sample project. #260

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
FROM python:3.8-alpine
FROM python:3.10-slim-bullseye
NaGaii1994 marked this conversation as resolved.
Show resolved Hide resolved

RUN apk update \
&& apk add --virtual build-deps gcc python3-dev musl-dev \
&& apk add bash \
&& apk add postgresql \
&& apk add postgresql-dev \
&& pip install psycopg2 \
&& apk add jpeg-dev zlib-dev libjpeg \
&& pip install Pillow \
&& apk del build-deps
# Install apt packages
RUN apt-get update && apt-get install --no-install-recommends -y \
# dependencies for building Python packages
build-essential \
# psycopg2 dependencies
libpq-dev \
# Translations dependencies
gettext \
# cleaning up unused files
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /code/
COPY . /code/
WORKDIR /code/

RUN pip install -r /code/test/example/requirements.txt

RUN apk add --no-cache postgresql-libs

ENTRYPOINT ["/code/docker-entrypoint.sh"]
RUN pip install -r /code/test/example/requirements.txt \
&& python -m pip install django-comments-dab[markdown]
6 changes: 4 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ services:
build:
context: .
dockerfile: Dockerfile
command: python manage.py runserver 0.0.0.0:8000
command: >
bash -c "python wait_for_postgres.py &&
/code/docker-entrypoint.sh"
ports:
- "8000:8000"
volumes:
Expand All @@ -26,7 +28,7 @@ services:
DB_PORT: 5432
DB_USER: postgres
db:
image: postgres:9.6
image: postgres:14
ports:
- "5432:5432"
environment:
Expand Down
2 changes: 2 additions & 0 deletions test/example/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ cssselect
coverage
flake8
tox
# used for postgresql connection
psycopg2
NaGaii1994 marked this conversation as resolved.
Show resolved Hide resolved
36 changes: 36 additions & 0 deletions wait_for_postgres.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
import logging
from time import time, sleep
import psycopg2
check_timeout = os.getenv("POSTGRES_CHECK_TIMEOUT", 30)
check_interval = os.getenv("POSTGRES_CHECK_INTERVAL", 1)
interval_unit = "second" if check_interval == 1 else "seconds"
config = {
"dbname": os.getenv("DB_NAME", "postgres"),
"user": os.getenv("DB_USER", "postgres"),
"password": os.getenv("DB_PASSWORD", "django-comments-dab"),
"host": os.getenv("DB_HOST", "db")
}

start_time = time()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())


def pg_isready(host, user, password, dbname):
while time() - start_time < check_timeout:
try:
conn = psycopg2.connect(**vars())
logger.info("Postgres is ready! ✨ 💅")
conn.close()
return True
except psycopg2.OperationalError:
logger.info(f"Postgres isn't ready. Waiting for {check_interval} {interval_unit}...")
sleep(check_interval)

logger.error(f"We could not connect to Postgres within {check_timeout} seconds.")
return False


pg_isready(**config)