Skip to content

Commit f1fe93b

Browse files
authored
Merge pull request #70 from gapitio/docker
feat: make docker image smaller
2 parents ccd8e14 + 71cf0a5 commit f1fe93b

6 files changed

Lines changed: 73 additions & 53 deletions

File tree

‎Dockerfile‎

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,129 @@
1-
FROM python:3.12-slim-bookworm
1+
FROM python:3.12-slim-trixie AS builder
22
WORKDIR /
33

4-
5-
ENV PYTHONUNBUFFERED=1
6-
ENV PIP_DISABLE_PIP_VERSION_CHECK=1
7-
ENV PIP_NO_CACHE_DIR=1
8-
94
ARG BUILD_DATE
105
ARG RELEASE
116
ARG VERSION
127

13-
ENV GAPIT_VERSION=${RELEASE}
148
ENV SERVER_VERSION=${RELEASE}
159
ENV CLIENT_VERSION=4.0.0
1610
ENV WEBUI_VERSION=${RELEASE}
1711

18-
ENV NGINX_WORKER_PROCESSES=1
19-
ENV NGINX_WORKER_CONNECTIONS=1024
20-
21-
ENV UWSGI_PROCESSES=5
22-
ENV UWSGI_LISTEN=100
23-
ENV UWSGI_BUFFER_SIZE=8192
24-
ENV UWSGI_MAX_WORKER_LIFETIME=30
25-
ENV UWSGI_WORKER_LIFETIME_DELTA=3
26-
ENV FRONTEND_BASE_URL=/
27-
28-
ENV HEARTBEAT_SEVERITY=major
12+
ENV PYTHONUNBUFFERED=1
13+
ENV PIP_DISABLE_PIP_VERSION_CHECK=1
14+
ENV PIP_NO_CACHE_DIR=1
2915

3016
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
3117

3218
RUN apt-get update && \
3319
apt-get upgrade -y && \
3420
apt-get install -y --no-install-recommends \
3521
build-essential \
36-
curl \
3722
git \
3823
gnupg2 \
3924
libldap2-dev \
4025
libpq-dev \
4126
libsasl2-dev \
42-
postgresql-client \
4327
python3-dev \
44-
supervisor \
45-
libpcre3 \
46-
libpcre3-dev \
28+
libpcre2-dev \
29+
libpcre2-8-0 \
4730
ca-certificates \
4831
xmlsec1 && \
4932
apt-get -y clean && \
5033
apt-get -y autoremove && \
5134
rm -rf /var/lib/apt/lists/*
5235

53-
RUN curl -fsSL https://nginx.org/keys/nginx_signing.key | apt-key add - && \
54-
echo "deb https://nginx.org/packages/debian/ buster nginx" | tee /etc/apt/sources.list.d/nginx.list && \
55-
apt-get update && \
56-
apt-get install -y --no-install-recommends \
57-
nginx && \
58-
apt-get -y clean && \
59-
apt-get -y autoremove && \
60-
rm -rf /var/lib/apt/lists/*
6136

6237
COPY requirements*.txt /app/
6338

6439
# hadolint ignore=DL3013
65-
RUN pip install --no-cache-dir pip virtualenv jinja2 && \
40+
RUN pip install --no-cache-dir pip virtualenv && \
6641
python3.12 -m venv /venv && \
6742
/venv/bin/pip install --no-cache-dir --upgrade setuptools && \
6843
/venv/bin/pip install --no-cache-dir --requirement /app/requirements.txt && \
69-
/venv/bin/pip install --no-cache-dir --requirement /app/requirements-docker.txt
44+
/venv/bin/pip install --no-cache-dir --requirement /app/requirements-docker.txt && \
45+
/venv/bin/pip install --no-cache-dir jinja2
46+
7047
ENV PATH=$PATH:/venv/bin
48+
49+
#download and install alerta client
7150
ADD https://github.com/gapitio/python-alerta-client/releases/download/v${CLIENT_VERSION}/alerta-client.tar.gz /tmp/client/client.tar.gz
7251
RUN tar zxvf /tmp/client/client.tar.gz -C /tmp/client/ && \
7352
find /tmp/client/dist -name "*-py2.py3-none-any.whl" -print0 | xargs -0 -I{} /venv/bin/pip install {}
53+
54+
#download and install alerta server/backend
7455
ADD https://github.com/gapitio/alerta/releases/download/${SERVER_VERSION}/alerta-api.tar.gz /tmp/backend/alerta.tar.gz
7556
RUN tar zxvf /tmp/backend/alerta.tar.gz -C /tmp/backend && \
7657
find /tmp/backend/dist -name "*-py2.py3-none-any.whl" -print0 | xargs -0 -I{} /venv/bin/pip install {}
77-
COPY install-plugins.sh /app/install-plugins.sh
78-
COPY plugins.txt /app/plugins.txt
79-
RUN /app/install-plugins.sh
8058

81-
ENV ALERTA_SVR_CONF_FILE=/app/alertad.conf
82-
ENV ALERTA_CONF_FILE=/app/alerta.conf
8359

60+
#download alerta frontend
8461
ADD https://github.com/gapitio/alerta-webui/releases/download/${WEBUI_VERSION}/alerta-webui.tar.gz /tmp/webui.tar.gz
8562
RUN tar zxvf /tmp/webui.tar.gz -C /tmp && \
8663
mv /tmp/dist /web
8764

65+
66+
FROM python:3.12-slim-trixie AS runtime
67+
WORKDIR /
68+
69+
COPY --from=builder /venv /venv
70+
COPY --from=builder /web /web
71+
COPY --from=builder /app /app
72+
73+
ARG RELEASE
74+
ENV GAPIT_VERSION=${RELEASE}
75+
76+
ENV PYTHONUNBUFFERED=1
77+
ENV PIP_DISABLE_PIP_VERSION_CHECK=1
78+
ENV PIP_NO_CACHE_DIR=1
79+
ENV PATH=$PATH:/venv/bin
80+
81+
ENV NGINX_WORKER_PROCESSES=1
82+
ENV NGINX_WORKER_CONNECTIONS=1024
83+
84+
ENV UWSGI_PROCESSES=5
85+
ENV UWSGI_LISTEN=100
86+
ENV UWSGI_BUFFER_SIZE=8192
87+
ENV UWSGI_MAX_WORKER_LIFETIME=30
88+
ENV UWSGI_WORKER_LIFETIME_DELTA=3
89+
ENV FRONTEND_BASE_URL=/
90+
91+
ENV HEARTBEAT_SEVERITY=major
92+
8893
ENV ALERTA_SVR_CONF_FILE=/app/alertad.conf
8994
ENV ALERTA_CONF_FILE=/app/alerta.conf
9095
ENV ALERTA_WEB_CONF_FILE=/web/config.json
9196

97+
RUN apt-get update && \
98+
apt-get upgrade -y && \
99+
apt-get install -y --no-install-recommends \
100+
gnupg2 \
101+
libpq-dev \
102+
supervisor \
103+
ca-certificates \
104+
xmlsec1 \
105+
nginx && \
106+
apt-get -y clean && \
107+
apt-get -y autoremove && \
108+
rm -rf /var/lib/apt/lists/*
109+
92110
COPY config/templates/app/ /app
93111
COPY config/templates/web/ /web
94112

95113
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
96114
&& ln -sf /dev/stderr /var/log/nginx/error.log
97115

116+
# user and permissions
98117
RUN chgrp -R 0 /app /venv /web && \
99118
chmod -R g=u /app /venv /web && \
100119
useradd -u 1001 -g 0 -d /app alerta
101120

102121
USER 1001
103122

104-
COPY docker-entrypoint.sh /usr/local/bin/
105-
123+
# entry point
106124
ENV INIT_LOG=false
107-
125+
COPY docker-entrypoint.sh /usr/local/bin/
108126
ENTRYPOINT ["docker-entrypoint.sh"]
109127

110128
EXPOSE 8080 1717
111-
CMD ["supervisord", "-c", "/app/supervisord.conf"]
129+
CMD ["supervisord", "-c", "/app/supervisord.conf"]

‎config/templates/app/nginx.conf.j2‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ http {
6060
index index.html;
6161

6262
location {{ env.FRONTEND_BASE_URL }} {
63+
default_type text/html;
6364
try_files $uri $uri/ {{ env.FRONTEND_BASE_URL }}index.html;
6465
}
6566

‎docker-entrypoint.sh‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ MAXAGE=${ADMIN_KEY_MAXAGE:-315360000} # default=10 years
2121
if [ ! -f "${ALERTA_SVR_CONF_FILE}" ]; then
2222
if [ $INIT_LOG = true ]; then echo "# Create server configuration file."; fi
2323
export SECRET_KEY=${SECRET_KEY:-$(< /dev/urandom tr -dc A-Za-z0-9_\!\@\#\$\%\^\&\*\(\)-+= | head -c 32)}
24-
python3 -c "${JINJA2}" < ${ALERTA_SVR_CONF_FILE}.j2 >${ALERTA_SVR_CONF_FILE}
24+
/venv/bin/python3 -c "${JINJA2}" < ${ALERTA_SVR_CONF_FILE}.j2 >${ALERTA_SVR_CONF_FILE}
2525
fi
2626

2727
# Init admin users and API keys
@@ -43,7 +43,7 @@ if [ ! -f "${ALERTA_CONF_FILE}" ]; then
4343
# Add API key to client config, if required
4444
if [ "${AUTH_REQUIRED,,}" == "true" ]; then
4545
if [ $INIT_LOG = true ]; then echo "# Auth enabled; add admin API key to client configuration."; fi
46-
HOUSEKEEPING_SCOPES="--scope read --scope write:alerts --scope admin:management --scope write:notification_rules --scope delete:alerts"
46+
HOUSEKEEPING_SCOPES="--scope read --scope write:alerts --scope admin:management --scope write:notification.rules --scope delete:alerts"
4747
if grep -qE 'CUSTOMER_VIEWS.*=.*True' ${ALERTA_SVR_CONF_FILE};then
4848
HOUSEKEEPING_SCOPES="--scope admin:alerts ${HOUSEKEEPING_SCOPES}"
4949
fi
@@ -54,26 +54,26 @@ if [ ! -f "${ALERTA_CONF_FILE}" ]; then
5454
--text "Housekeeping")
5555
fi
5656
if [ $INIT_LOG = true ]; then echo "# Create client configuration file."; fi
57-
python3 -c "${JINJA2}" < ${ALERTA_CONF_FILE}.j2 >${ALERTA_CONF_FILE}
57+
/venv/bin/python3 -c "${JINJA2}" < ${ALERTA_CONF_FILE}.j2 >${ALERTA_CONF_FILE}
5858
fi
5959

6060
# Generate supervisord config, if not supplied
6161
if [ ! -f "${SUPERVISORD_CONF_FILE}" ]; then
6262
if [ $INIT_LOG = true ]; then echo "# Create supervisord configuration file."; fi
63-
python3 -c "${JINJA2}" < ${SUPERVISORD_CONF_FILE}.j2 >${SUPERVISORD_CONF_FILE}
63+
/venv/bin/python3 -c "${JINJA2}" < ${SUPERVISORD_CONF_FILE}.j2 >${SUPERVISORD_CONF_FILE}
6464
fi
6565

6666
# Generate nginx config, if not supplied.
6767
if [ ! -f "${NGINX_CONF_FILE}" ]; then
6868
if [ $INIT_LOG = true ]; then echo "# Create nginx configuration file."; fi
69-
python3 -c "${JINJA2}" < ${NGINX_CONF_FILE}.j2 >${NGINX_CONF_FILE}
69+
/venv/bin/python3 -c "${JINJA2}" < ${NGINX_CONF_FILE}.j2 >${NGINX_CONF_FILE}
7070
fi
7171
nginx -t -c ${NGINX_CONF_FILE}
7272

7373
# Generate uWSGI config, if not supplied.
7474
if [ ! -f "${UWSGI_CONF_FILE}" ]; then
7575
if [ $INIT_LOG = true ]; then echo "# Create uWSGI configuration file."; fi
76-
python3 -c "${JINJA2}" < ${UWSGI_CONF_FILE}.j2 >${UWSGI_CONF_FILE}
76+
/venv/bin/python3 -c "${JINJA2}" < ${UWSGI_CONF_FILE}.j2 >${UWSGI_CONF_FILE}
7777
fi
7878

7979
if [ ! -d "/web${FRONTEND_BASE_URL}" ]; then
@@ -84,7 +84,7 @@ fi
8484
# Generate web config, if not supplied.
8585
if [ ! -f "${ALERTA_WEB_CONF_FILE}" ]; then
8686
if [ $INIT_LOG = true ]; then echo "# Create web configuration file."; fi
87-
python3 -c "${JINJA2}" < ${ALERTA_WEB_CONF_FILE}.j2 >${ALERTA_WEB_CONF_FILE}
87+
/venv/bin/python3 -c "${JINJA2}" < ${ALERTA_WEB_CONF_FILE}.j2 >${ALERTA_WEB_CONF_FILE}
8888
fi
8989

9090
if [ $INIT_LOG = true ]; then
@@ -97,7 +97,7 @@ if [ $INIT_LOG = true ]; then
9797
nginx -v
9898
echo uwsgi $(uwsgi --version)
9999
psql --version
100-
python3 --version
100+
/venv/bin/python3 --version
101101
/venv/bin/pip list
102102

103103
echo

‎tests/docker-compose.test.postgres.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ services:
2222
- ADMIN_USERS=admin@alerta.io,devops@alerta.io # default password: alerta
2323
- ADMIN_KEY=demo-key
2424
- ADMIN_KEY_MAXAGE=31536000 # 1 year in seconds
25-
- PLUGINS=reject,blackout,normalise,enhance
25+
- PLUGINS=reject,blackout
2626
- HEARTBEAT_SEVERITY=warning
2727
# Disable hearbeat alerts by setting below env var to empty string
2828
# - HEARTBEAT_SEVERITY=

‎tests/docker/Dockerfile‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
FROM ruby:3.0
1+
FROM ruby:4.0
22

33
COPY Gemfile* ./
44
RUN gem install bundler && bundle install --jobs=3 --retry=3
55

6-
RUN apt-get -q update && apt-get -qy install netcat
6+
RUN apt-get -q update && apt-get -qy install netcat-traditional
77
RUN wget https://raw.githubusercontent.com/eficode/wait-for/v2.2.0/wait-for && chmod +x wait-for
88

99
COPY .rspec .

‎tests/docker/Gemfile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Gemfile
22
source "https://rubygems.org"
33

4+
gem 'logger'
45
gem "rspec"
56
gem 'rest-client'
67
gem 'nokogiri'

0 commit comments

Comments
 (0)