Skip to content

Commit db33458

Browse files
authored
Merge pull request #19 from nginxinc/docker-alpine-nginx-amplify
Alpine based Dockerfile
2 parents 7dc03f7 + ab0ddda commit db33458

File tree

5 files changed

+357
-0
lines changed

5 files changed

+357
-0
lines changed

Alpine/Dockerfile

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
FROM alpine:3.8
2+
3+
LABEL maintainer="NGINX Docker Maintainers <[email protected]>"
4+
5+
ENV NGINX_VERSION 1.14.2
6+
7+
RUN GPG_KEYS=B0F4253373F8F6F510D42178520A9993A1C052F8 \
8+
&& CONFIG="\
9+
--prefix=/etc/nginx \
10+
--sbin-path=/usr/sbin/nginx \
11+
--modules-path=/usr/lib/nginx/modules \
12+
--conf-path=/etc/nginx/nginx.conf \
13+
--error-log-path=/var/log/nginx/error.log \
14+
--http-log-path=/var/log/nginx/access.log \
15+
--pid-path=/var/run/nginx.pid \
16+
--lock-path=/var/run/nginx.lock \
17+
--http-client-body-temp-path=/var/cache/nginx/client_temp \
18+
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
19+
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
20+
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
21+
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
22+
--user=nginx \
23+
--group=nginx \
24+
--with-http_ssl_module \
25+
--with-http_realip_module \
26+
--with-http_addition_module \
27+
--with-http_sub_module \
28+
--with-http_dav_module \
29+
--with-http_flv_module \
30+
--with-http_mp4_module \
31+
--with-http_gunzip_module \
32+
--with-http_gzip_static_module \
33+
--with-http_random_index_module \
34+
--with-http_secure_link_module \
35+
--with-http_stub_status_module \
36+
--with-http_auth_request_module \
37+
--with-http_xslt_module=dynamic \
38+
--with-http_image_filter_module=dynamic \
39+
--with-http_geoip_module=dynamic \
40+
--with-threads \
41+
--with-stream \
42+
--with-stream_ssl_module \
43+
--with-stream_ssl_preread_module \
44+
--with-stream_realip_module \
45+
--with-stream_geoip_module=dynamic \
46+
--with-http_slice_module \
47+
--with-mail \
48+
--with-mail_ssl_module \
49+
--with-compat \
50+
--with-file-aio \
51+
--with-http_v2_module \
52+
" \
53+
&& addgroup -S nginx \
54+
&& adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G nginx nginx \
55+
&& apk add --no-cache --virtual .build-deps \
56+
gcc \
57+
libc-dev \
58+
make \
59+
openssl-dev \
60+
pcre-dev \
61+
zlib-dev \
62+
linux-headers \
63+
curl \
64+
gnupg1 \
65+
libxslt-dev \
66+
gd-dev \
67+
geoip-dev \
68+
&& curl -fSL https://nginx.org/download/nginx-$NGINX_VERSION.tar.gz -o nginx.tar.gz \
69+
&& curl -fSL https://nginx.org/download/nginx-$NGINX_VERSION.tar.gz.asc -o nginx.tar.gz.asc \
70+
&& export GNUPGHOME="$(mktemp -d)" \
71+
&& found=''; \
72+
for server in \
73+
ha.pool.sks-keyservers.net \
74+
hkp://keyserver.ubuntu.com:80 \
75+
hkp://p80.pool.sks-keyservers.net:80 \
76+
pgp.mit.edu \
77+
; do \
78+
echo "Fetching GPG key $GPG_KEYS from $server"; \
79+
gpg --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$GPG_KEYS" && found=yes && break; \
80+
done; \
81+
test -z "$found" && echo >&2 "error: failed to fetch GPG key $GPG_KEYS" && exit 1; \
82+
gpg --batch --verify nginx.tar.gz.asc nginx.tar.gz \
83+
&& rm -rf "$GNUPGHOME" nginx.tar.gz.asc \
84+
&& mkdir -p /usr/src \
85+
&& tar -zxC /usr/src -f nginx.tar.gz \
86+
&& rm nginx.tar.gz \
87+
&& cd /usr/src/nginx-$NGINX_VERSION \
88+
&& ./configure $CONFIG --with-debug \
89+
&& make -j$(getconf _NPROCESSORS_ONLN) \
90+
&& mv objs/nginx objs/nginx-debug \
91+
&& mv objs/ngx_http_xslt_filter_module.so objs/ngx_http_xslt_filter_module-debug.so \
92+
&& mv objs/ngx_http_image_filter_module.so objs/ngx_http_image_filter_module-debug.so \
93+
&& mv objs/ngx_http_geoip_module.so objs/ngx_http_geoip_module-debug.so \
94+
&& mv objs/ngx_stream_geoip_module.so objs/ngx_stream_geoip_module-debug.so \
95+
&& ./configure $CONFIG \
96+
&& make -j$(getconf _NPROCESSORS_ONLN) \
97+
&& make install \
98+
&& rm -rf /etc/nginx/html/ \
99+
&& mkdir /etc/nginx/conf.d/ \
100+
&& mkdir -p /usr/share/nginx/html/ \
101+
&& install -m644 html/index.html /usr/share/nginx/html/ \
102+
&& install -m644 html/50x.html /usr/share/nginx/html/ \
103+
&& install -m755 objs/nginx-debug /usr/sbin/nginx-debug \
104+
&& install -m755 objs/ngx_http_xslt_filter_module-debug.so /usr/lib/nginx/modules/ngx_http_xslt_filter_module-debug.so \
105+
&& install -m755 objs/ngx_http_image_filter_module-debug.so /usr/lib/nginx/modules/ngx_http_image_filter_module-debug.so \
106+
&& install -m755 objs/ngx_http_geoip_module-debug.so /usr/lib/nginx/modules/ngx_http_geoip_module-debug.so \
107+
&& install -m755 objs/ngx_stream_geoip_module-debug.so /usr/lib/nginx/modules/ngx_stream_geoip_module-debug.so \
108+
&& ln -s ../../usr/lib/nginx/modules /etc/nginx/modules \
109+
&& strip /usr/sbin/nginx* \
110+
&& strip /usr/lib/nginx/modules/*.so \
111+
&& rm -rf /usr/src/nginx-$NGINX_VERSION \
112+
\
113+
# Bring in gettext so we can get `envsubst`, then throw
114+
# the rest away. To do this, we need to install `gettext`
115+
# then move `envsubst` out of the way so `gettext` can
116+
# be deleted completely, then move `envsubst` back.
117+
&& apk add --no-cache --virtual .gettext gettext \
118+
&& mv /usr/bin/envsubst /tmp/ \
119+
\
120+
&& runDeps="$( \
121+
scanelf --needed --nobanner --format '%n#p' /usr/sbin/nginx /usr/lib/nginx/modules/*.so /tmp/envsubst \
122+
| tr ',' '\n' \
123+
| sort -u \
124+
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
125+
)" \
126+
&& apk add --no-cache --virtual .nginx-rundeps $runDeps \
127+
&& apk del .build-deps \
128+
&& apk del .gettext \
129+
&& mv /tmp/envsubst /usr/local/bin/ \
130+
\
131+
# Bring in tzdata so users could set the timezones through the environment
132+
# variables
133+
&& apk add --no-cache tzdata \
134+
\
135+
### Amplify specific section starts here
136+
# make sure log files are agent-readable
137+
&& touch /var/log/nginx/access.log \
138+
&& touch /var/log/nginx/error.log \
139+
\
140+
# Install packages required by agent
141+
&& apk add --no-cache python \
142+
procps \
143+
util-linux \
144+
py-pip \
145+
\
146+
# install packages required for agent build
147+
&& apk add --no-cache --virtual .amplify\
148+
python-dev \
149+
build-base \
150+
git \
151+
linux-headers \
152+
&& cd / \
153+
\
154+
# clone latest agent from repository and build it
155+
&& git clone https://github.com/nginxinc/nginx-amplify-agent \
156+
&& cd nginx-amplify-agent/ \
157+
&& pip install --no-cache-dir -r packages/nginx-amplify-agent/requirements.txt \
158+
&& python setup.py install \
159+
\
160+
# make sure agent log exists
161+
&& mkdir -p /var/log/amplify-agent \
162+
&& touch /var/log/amplify-agent/agent.log \
163+
\
164+
# create agent config file
165+
&& cp /etc/amplify-agent/agent.conf.default /etc/amplify-agent/agent.conf \
166+
\
167+
# Cleanup
168+
&& cd .. \
169+
&& rm -Rf nginx-amplify-agent/ \
170+
&& find /usr/lib/python2.7 -name \*\.pyo -exec rm {} \; \
171+
&& find /usr/lib/python2.7 -name \*\.pyc -exec rm {} \; \
172+
&& apk del .amplify
173+
174+
COPY ./entrypoint.sh /entrypoint.sh
175+
COPY conf/nginx.conf /etc/nginx/nginx.conf
176+
COPY conf/nginx.vh.default.conf /etc/nginx/conf.d/default.conf
177+
COPY conf/stub_status.conf /etc/nginx/conf.d/stub_status.conf
178+
179+
EXPOSE 80
180+
181+
STOPSIGNAL SIGTERM
182+
183+
ENTRYPOINT ["/entrypoint.sh"]

Alpine/conf/nginx.conf

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
user nginx;
3+
worker_processes 1;
4+
5+
error_log /var/log/nginx/error.log warn;
6+
pid /var/run/nginx.pid;
7+
8+
9+
events {
10+
worker_connections 1024;
11+
}
12+
13+
14+
http {
15+
include /etc/nginx/mime.types;
16+
default_type application/octet-stream;
17+
18+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
19+
'$status $body_bytes_sent "$http_referer" '
20+
'"$http_user_agent" "$http_x_forwarded_for"';
21+
22+
log_format main_ext '$remote_addr - $remote_user [$time_local] "$request" '
23+
'$status $body_bytes_sent "$http_referer" '
24+
'"$http_user_agent" "$http_x_forwarded_for" '
25+
'"$host" sn="$server_name" '
26+
'rt=$request_time '
27+
'ua="$upstream_addr" us="$upstream_status" '
28+
'ut="$upstream_response_time" ul="$upstream_response_length" '
29+
'cs=$upstream_cache_status' ;
30+
31+
access_log /var/log/nginx/access.log main_ext;
32+
error_log /var/log/nginx/error.log warn;
33+
34+
sendfile on;
35+
#tcp_nopush on;
36+
37+
keepalive_timeout 65;
38+
39+
#gzip on;
40+
41+
include /etc/nginx/conf.d/*.conf;
42+
}

Alpine/conf/nginx.vh.default.conf

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
server {
2+
listen 80;
3+
server_name localhost;
4+
5+
#charset koi8-r;
6+
#access_log /var/log/nginx/host.access.log main;
7+
8+
location / {
9+
root /usr/share/nginx/html;
10+
index index.html index.htm;
11+
}
12+
13+
#error_page 404 /404.html;
14+
15+
# redirect server error pages to the static page /50x.html
16+
#
17+
error_page 500 502 503 504 /50x.html;
18+
location = /50x.html {
19+
root /usr/share/nginx/html;
20+
}
21+
22+
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
23+
#
24+
#location ~ \.php$ {
25+
# proxy_pass http://127.0.0.1;
26+
#}
27+
28+
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
29+
#
30+
#location ~ \.php$ {
31+
# root html;
32+
# fastcgi_pass 127.0.0.1:9000;
33+
# fastcgi_index index.php;
34+
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
35+
# include fastcgi_params;
36+
#}
37+
38+
# deny access to .htaccess files, if Apache's document root
39+
# concurs with nginx's one
40+
#
41+
#location ~ /\.ht {
42+
# deny all;
43+
#}
44+
}
45+

Alpine/conf/stub_status.conf

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
server {
2+
root /usr/share/nginx/html;
3+
listen 127.0.0.1:80;
4+
server_name 127.0.0.1;
5+
location /nginx_status {
6+
stub_status on;
7+
allow 127.0.0.1;
8+
deny all;
9+
}
10+
}

Alpine/entrypoint.sh

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/sh
2+
#
3+
# This script launches nginx and the NGINX Amplify Agent.
4+
#
5+
# Unless already baked in the image, a real API_KEY is required for the
6+
# NGINX Amplify Agent to be able to connect to the backend.
7+
#
8+
# If AMPLIFY_IMAGENAME is set, the script will use it to generate
9+
# the 'imagename' to put in the /etc/amplify-agent/agent.conf
10+
#
11+
# If several instances use the same imagename, the metrics will
12+
# be aggregated into a single object in Amplify. Otherwise NGINX Amplify
13+
# will create separate objects for monitoring (an object per instance).
14+
#
15+
16+
# Variables
17+
agent_conf_file="/etc/amplify-agent/agent.conf"
18+
agent_log_file="/var/log/amplify-agent/agent.log"
19+
nginx_status_conf="/etc/nginx/conf.d/stub_status.conf"
20+
api_key=""
21+
amplify_imagename=""
22+
23+
# Launch nginx
24+
echo "starting nginx ..."
25+
nginx -g "daemon off;" &
26+
27+
nginx_pid=$!
28+
29+
test -n "${API_KEY}" && \
30+
api_key=${API_KEY}
31+
32+
test -n "${AMPLIFY_IMAGENAME}" && \
33+
amplify_imagename=${AMPLIFY_IMAGENAME}
34+
35+
if [ -n "${api_key}" -o -n "${amplify_imagename}" ]; then
36+
echo "updating ${agent_conf_file} ..."
37+
38+
if [ ! -f "${agent_conf_file}" ]; then
39+
test -f "${agent_conf_file}.default" && \
40+
cp -p "${agent_conf_file}.default" "${agent_conf_file}" || \
41+
{ echo "no ${agent_conf_file}.default found! exiting."; exit 1; }
42+
fi
43+
44+
test -n "${api_key}" && \
45+
echo " ---> using api_key = ${api_key}" && \
46+
sh -c "sed -i.old -e 's/api_key.*$/api_key = $api_key/' \
47+
${agent_conf_file}"
48+
49+
test -n "${amplify_imagename}" && \
50+
echo " ---> using imagename = ${amplify_imagename}" && \
51+
sh -c "sed -i.old -e 's/imagename.*$/imagename = $amplify_imagename/' \
52+
${agent_conf_file}"
53+
54+
test -f "${agent_conf_file}" && \
55+
chmod 644 ${agent_conf_file} && \
56+
chown nginx ${agent_conf_file} > /dev/null 2>&1
57+
58+
test -f "${nginx_status_conf}" && \
59+
chmod 644 ${nginx_status_conf} && \
60+
chown nginx ${nginx_status_conf} > /dev/null 2>&1
61+
fi
62+
63+
if ! grep '^api_key.*=[ ]*[[:alnum:]].*' ${agent_conf_file} > /dev/null 2>&1; then
64+
echo "no api_key found in ${agent_conf_file}! exiting."
65+
fi
66+
67+
echo "starting amplify-agent ..."
68+
nginx-amplify-agent.py start --config=/etc/amplify-agent/agent.conf > /dev/null 2>&1 < /dev/null
69+
70+
if [ $? != 0 ]; then
71+
echo "couldn't start the agent, please check ${agent_log_file}"
72+
exit 1
73+
fi
74+
75+
wait ${nginx_pid}
76+
77+
echo "nginx master process has stopped, exiting."

0 commit comments

Comments
 (0)