Estou trabalhando num projeto junto com um amigo usando Docker e Ansible para deploy. Todas as vezes que subimos os container's depois de um tempo o PostgreSQL começa a consumir 100% de CPU conforme print a seguir
Nossos arquivos do docker estão assim configurados:
docker-compose
version: "3.7"
networks:
xpto_web_network:
name: xpto_web_network
services:
xpto_web_database:
container_name: xpto_web_database
image: postgres:16.1
restart: always
volumes:
- xpto_web-db:/var/lib/postgresql/data
environment:
- LC_ALL=C.UTF-8
- POSTGRES_DB=xpto_web_db
- POSTGRES_USER=xpto_web_dbmanager_2LiyBoLHeHo5yG
- POSTGRES_PASSWORD=...
- POSTGRES_HOST_AUTH_METHOD=md5
networks:
- xpto_web_network
ports:
- "5439:5432"
xpto_web_django:
container_name: xpto_web_django
image: xpto_web:1.0
environment:
- DB_NAME=xpto_web_db
- DB_PASSWORD=...
- DB_USER=xpto_web_dbmanager_2LiyBoLHeHo5yG
- DB_HOST=xpto_web_database
- DB_PORT=5432
- DEBUG=False
- ...
build:
context: .
dockerfile: ./Dockerfile
entrypoint: /home/app/xpto_web_django/xpto-web.sh
networks:
- xpto_web_network
volumes:
- xpto_web-media:/home/app/xpto_web_django/media
- xpto_web-static:/home/app/xpto_web_django/static
depends_on:
- xpto_web_database
gateway:
image: nginx:1.19-alpine
container_name: xpto_web_gateway
restart: unless-stopped
networks:
- xpto_web_network
expose:
- 443
- 82
ports:
- "8007:82"
depends_on:
- xpto_web_django
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf
- xpto_web-media:/home/app/xpto_web_django/media
- xpto_web-static:/home/app/xpto_web_django/static
volumes:
xpto_web-db:
xpto_web-media:
xpto_web-static:
Dockerfile
FROM python:3.12.1-slim as builder
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1
WORKDIR /app
RUN apt-get clean && apt-get update
RUN apt-get -y install sudo --no-install-recommends build-essential libpq-dev
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
COPY . /app
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt
EXPOSE 8000
FROM python:3.12.1-slim
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1
ENV HOME=/home/app
ENV APP_HOME=/home/app/xpto_web_django
RUN mkdir -p $HOME
RUN mkdir $APP_HOME
WORKDIR $APP_HOME
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /app .
RUN mkdir -p $APP_HOME/static
RUN mkdir -p $APP_HOME/media
RUN pip install -U setuptools pip
RUN pip install --no-cache /wheels/*
COPY ./xpto-web.sh ./xpto-web.sh
RUN chmod +x ./xpto-web.sh
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser $APP_HOME
USER appuser
nginx.conf
#nginx config file
upstream xptoweb {
server xpto_web_django:xxxx;
}
server {
listen xx;
charset utf-8;
server_name _;
# Frontend
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_pass http://xptoweb/;
}
location /static/ {
alias /home/app/xpto_web_django/static/;
}
location /media/ {
alias /home/app/xpto_web_django/media/;
}
location = /favicon.ico
{
access_log off; log_not_found off;
}
}
xpto-web.sh
#!/bin/bash
python manage.py migrate
python manage.py collectstatic --noinput
python mock_superuser.py
gunicorn base.wsgi:application --workers=1 --bind=0.0.0.0:8007