Skip to content

Commit

Permalink
Feature/new dockerfile (#325)
Browse files Browse the repository at this point in the history
* Fix/dockerfile (#322)

* Test PR changes

* Modify dockerfile

* Fix dockerfile and docker-entrypoint

* Remove REQUESTS_VERSION from dockerfile

* Update docker-entrypoint name in dockerfile

* Adjust docker-entrypoint

* Adjust docker-entrypoint command in the dockerfile

* Update Dockerfile

* feat: update dockerfile

* feat: new entrypoint

---------

Co-authored-by: Natália de Assis <[email protected]>
  • Loading branch information
helllllllder and nataliaweni authored Jan 11, 2024
1 parent 8ef32ef commit 7d39cad
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 59 deletions.
36 changes: 15 additions & 21 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
#!/bin/bash

export GUNICORN_APP=${GUNICORN_APP:-"app.wsgi:application"}
export CELERY_APP=${CELERY_APP:-"app.wsgi:celery"}
export GUNICORN_LOG_CONF=${GUNICORN_LOG_CONF:-"${PROJECT_PATH}/docker/gunicorn/gunicorn-logging.conf"}
export GUNICORN_CONF=${GUNICORN_CONF:-"${PROJECT_PATH}/docker/gunicorn/gunicorn.conf.py"}
export GUNICORN_APP=${GUNICORN_APP:-"chats.asgi"}
export CELERY_APP=${CELERY_APP:-"chats"}
export GUNICORN_CONF=${GUNICORN_CONF:-"${PROJECT_PATH}/gunicorn.conf.py"}
export LOG_LEVEL=${LOG_LEVEL:-"INFO"}
#export GUNICORN_CONF=${GUNICORN_CONF:-"python:app.gunicorn"
export CELERY_MAX_WORKERS=${CELERY_MAX_WORKERS:-'4'}
export CELERY_BEAT_DATABASE_FILE=${CELERY_BEAT_DATABASE_FILE:-'/tmp/celery_beat_database'}
export CELERY_MAX_WORKERS=${CELERY_MAX_WORKERS:-'6'}
# export CELERY_BEAT_DATABASE_FILE=${CELERY_BEAT_DATABASE_FILE:-'/tmp/celery_beat_database'}
export HEALTHCHECK_TIMEOUT=${HEALTHCHECK_TIMEOUT:-"10"}

echo "Running collectstatic"
python manage.py collectstatic --noinput

echo "Starting server"
exec gunicorn chats.asgi -c gunicorn.conf.py

do_gosu(){
user="$1"
shift 1
Expand Down Expand Up @@ -45,30 +37,32 @@ do_gosu(){


if [[ "start" == "$1" ]]; then
do_gosu "${APP_USER}:${APP_GROUP}" exec gunicorn "${GUNICORN_APP}" \
--name="${APP_NAME}" \
do_gosu "${PROJECT_USER}:${PROJECT_GROUP}" python manage.py collectstatic --noinput
do_gosu "${PROJECT_USER}:${PROJECT_GROUP}" exec gunicorn "${GUNICORN_APP}" \
--name="${APPLICATION_NAME}" \
--chdir="${PROJECT_PATH}" \
--bind=0.0.0.0:8080 \
--log-config="${GUNICORN_LOG_CONF}" \
--bind=0.0.0.0:8000 \
-c "${GUNICORN_CONF}"
elif [[ "start-websocket" == "$1" ]]; then
do_gosu "${PROJECT_USER}:${PROJECT_GROUP}" exec daphne -b 0.0.0.0 -p 8000 chats.asgi:application
elif [[ "celery-worker" == "$1" ]]; then
celery_queue="celery"
if [ "${2}" ] ; then
celery_queue="${2}"
fi
do_gosu "${APP_USER}:${APP_GROUP}" exec celery \
-A "${CELERY_APP}" --workdir="${APP_PATH}" worker \
do_gosu "${PROJECT_USER}:${PROJECT_GROUP}" exec celery \
-A "${CELERY_APP}" --workdir="${PROJECT_PATH}" worker \
-Q "${celery_queue}" \
-O fair \
-l "${LOG_LEVEL}" \
--autoscale=4,1
--autoscale=${CELERY_MAX_WORKERS},1
elif [[ "healthcheck-celery-worker" == "$1" ]]; then
celery_queue="celery"
if [ "${2}" ] ; then
celery_queue="${2}"
fi
HEALTHCHECK_OUT=$(
do_gosu "${APP_USER}:${APP_GROUP}" celery -A "${CELERY_APP}" \
do_gosu "${PROJECT_USER}:${PROJECT_GROUP}" celery -A "${CELERY_APP}" \
inspect ping \
-d "${celery_queue}@${HOSTNAME}" \
--timeout "${HEALTHCHECK_TIMEOUT}" 2>&1
Expand Down
73 changes: 35 additions & 38 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,55 @@ ARG PYTHON_VERSION="3.9"
ARG POETRY_VERSION="1.1.15"

ARG BUILD_DEPS="\
python3-dev \
build-essential \
gettext \
libpq-dev \
cmake \
pkg-config \
autoconf \
libtool \
automake"
python3-dev \
build-essential \
gettext \
libpq-dev \
cmake \
pkg-config \
autoconf \
libtool \
automake"

ARG RUNTIME_DEPS="\
tzdata \
curl \
gosu \
gettext \
postgresql-client \
ffmpeg \
libmagic1"
tzdata \
curl \
gosu \
gettext \
postgresql-client \
ffmpeg \
libmagic1"

FROM python:${PYTHON_VERSION}-slim as base

ARG POETRY_VERSION

ARG APP_PORT="8000"
ARG APP_VERSION="0.1"

# set environment variables
ENV APP_VERSION=${APP_VERSION} \
RUNTIME_DEPS=${RUNTIME_DEPS} \
BUILD_DEPS=${BUILD_DEPS} \
APPLICATION_NAME="Chats" \
PROJECT_PATH=/chats \
PROJECT_USER=app_user \
PROJECT_GROUP=app_group \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PYTHONIOENCODING=UTF-8 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PATH="/install/bin:${PATH}" \
APP_PORT=${APP_PORT}
RUNTIME_DEPS=${RUNTIME_DEPS} \
BUILD_DEPS=${BUILD_DEPS} \
APPLICATION_NAME="Chats" \
PROJECT_PATH=/chats \
PROJECT_USER=app_user \
PROJECT_GROUP=app_group \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PYTHONIOENCODING=UTF-8 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PATH="/install/bin:${PATH}" \
APP_PORT=${APP_PORT}

LABEL app=${VERSION} \
os="debian" \
os.version="10" \
name="${APPLICATION_NAME} ${APP_VERSION}" \
description="${APPLICATION_NAME} image" \
maintainer="${APPLICATION_NAME} Team"
os="debian" \
os.version="10" \
name="${APPLICATION_NAME} ${APP_VERSION}" \
description="${APPLICATION_NAME} image" \
maintainer="${APPLICATION_NAME} Team"

RUN addgroup --gid 1999 "${PROJECT_GROUP}" \
&& useradd --system -m -d ${PROJECT_PATH} -u 1999 -g 1999 "${PROJECT_USER}"
&& useradd --system -m -d ${PROJECT_PATH} -u 1999 -g 1999 "${PROJECT_USER}"

# set work directory
WORKDIR ${PROJECT_PATH}
Expand All @@ -78,7 +77,7 @@ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y ${BUILD_DEPS}

COPY --from=build-poetry "${PROJECT_PATH}/requirements.txt" /tmp/dep/
RUN --mount=type=cache,mode=0755,target=/pip_cache,id=pip pip install --cache-dir /pip_cache --prefix=/install -r /tmp/dep/requirements.txt

Expand Down Expand Up @@ -106,5 +105,3 @@ EXPOSE 8000

ENTRYPOINT ["bash", "docker-entrypoint.sh"]
CMD ["start"]
# CMD ["sh", "docker-entrypoint.sh"]
# #ENTRYPOINT ["docker-entrypoint.sh"]

0 comments on commit 7d39cad

Please sign in to comment.