Skip to content

Commit 7813999

Browse files
author
Bryan Latten
committed
Merge pull request #20 from bryanlatten/feature-alpine
Dockerfile: based on alpine
2 parents a52a80c + 0835824 commit 7813999

File tree

8 files changed

+176
-48
lines changed

8 files changed

+176
-48
lines changed

Dockerfile

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
1-
FROM ubuntu:14.04.3
1+
FROM alpine:3.3
22
MAINTAINER Bryan Latten <[email protected]>
33

44
# Use in multi-phase builds, when an init process requests for the container to gracefully exit, so that it may be committed
5-
ENV SIGNAL_BUILD_STOP 99
6-
75
# Used with alternative CMD (worker.sh), leverages supervisor to maintain long-running processes
8-
ENV CONTAINER_ROLE=web
6+
ENV SIGNAL_BUILD_STOP=99 \
7+
CONTAINER_ROLE=web \
8+
CONF_NGINX_SITE="/etc/nginx/sites-available/default" \
9+
CONF_NGINX_SERVER="/etc/nginx/nginx.conf" \
10+
NOT_ROOT_USER=docker
11+
12+
# Create an unprivileged user
13+
RUN adduser -D -S -H $NOT_ROOT_USER
914

1015
# IMPORTANT: update is *part* of the upgrade statement to ensure the latest on each build.
11-
# Installs pre-reqs, security updates
12-
RUN apt-get update && \
13-
apt-get upgrade -yq && \
14-
apt-get -yq install \
15-
openssl \
16-
ca-certificates \
17-
software-properties-common \
16+
# Note: sed/grep replace the less performant, less functional busybox versions
17+
RUN apk update && \
18+
apk upgrade && \
19+
apk add \
20+
sed \
21+
grep \
1822
supervisor \
19-
nano
20-
21-
# Install latest nginx-stable
22-
RUN add-apt-repository ppa:nginx/stable -y && \
23-
apt-get update -yq && \
24-
apt-get install -yq nginx
23+
nginx \
24+
&& \
25+
rm -rf /var/cache/apk/*
2526

2627
# Overlay the root filesystem from this repo
2728
COPY ./container/root /
2829

2930
EXPOSE 80
30-
CMD ["/bin/bash", "/run.sh"]
31+
CMD ["/bin/sh", "/run.sh"]

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Variable | Example | Description
99
`SERVER_APP_NAME` | `SERVER_APP_NAME='view'` | Sets a kv pair to be consumed by logging service for easy parsing and searching
1010
`SERVER_GZIP_OPTIONS` | `SERVER_GZIP_OPTIONS=1` | Allows default set of static content to be served gzipped
1111
`SERVER_SENDFILE` | `SERVER_SENDFILE=off` | Allows runtime to specify value of nginx's `sendfile` (default, on)
12+
`SERVER_KEEPALIVE` | `SERVER_KEEPALIVE=30` | Define HTTP 1.1's keepalive timeout
13+
`SERVER_WORKER_CONNECTIONS` | `SERVER_WORKER_CONNECTIONS=2048` | Sets up the number of connections for worker processes
1214

1315

1416
### Runtime Commands
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#############################################################
2+
# Replace nginx configuration directly in this file,
3+
# removing the need to perform error-prone replacements
4+
# at build time.
5+
#
6+
# For run-time replacements, ie, consuming environment vars,
7+
# add to the run.d/nginx script
8+
#############################################################
9+
10+
user nobody;
11+
worker_processes auto;
12+
13+
pid /tmp/nginx.pid;
14+
15+
events {
16+
# @see http://serverfault.com/questions/209014/how-can-i-observe-what-nginx-is-doing-to-solve-1024-worker-connections-are-n
17+
worker_connections 1024;
18+
}
19+
20+
http {
21+
22+
include mime.types;
23+
default_type application/octet-stream;
24+
25+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
26+
'$status $body_bytes_sent "$http_referer" '
27+
'"$http_user_agent" "$http_x_forwarded_for" NGINX_SERVER';
28+
29+
error_log /dev/stdout info;
30+
access_log /dev/stdout main;
31+
32+
sendfile on;
33+
#tcp_nopush on;
34+
35+
keepalive_timeout 65;
36+
37+
#gzip on;
38+
39+
client_body_temp_path /tmp/client_body;
40+
fastcgi_temp_path /tmp/fastcgi_temp;
41+
proxy_temp_path /tmp/proxy_temp;
42+
scgi_temp_path /tmp/scgi_temp;
43+
uwsgi_temp_path /tmp/uwsgi_temp;
44+
45+
server {
46+
listen 80;
47+
server_name localhost;
48+
49+
#charset koi8-r;
50+
51+
location / {
52+
root html;
53+
index index.html index.htm;
54+
}
55+
56+
#error_page 404 /404.html;
57+
58+
# redirect server error pages to the static page /50x.html
59+
#
60+
error_page 500 502 503 504 /50x.html;
61+
location = /50x.html {
62+
root html;
63+
}
64+
65+
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
66+
#
67+
#location ~ \.php$ {
68+
# proxy_pass http://127.0.0.1;
69+
#}
70+
71+
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
72+
#
73+
#location ~ \.php$ {
74+
# root html;
75+
# fastcgi_pass 127.0.0.1:9000;
76+
# fastcgi_index index.php;
77+
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
78+
# include fastcgi_params;
79+
#}
80+
81+
# deny access to .htaccess files, if Apache's document root
82+
# concurs with nginx's one
83+
#
84+
#location ~ /\.ht {
85+
# deny all;
86+
#}
87+
}
88+
89+
90+
# another virtual host using mix of IP-, name-, and port-based configuration
91+
#
92+
#server {
93+
# listen 8000;
94+
# listen somename:8080;
95+
# server_name somename alias another.alias;
96+
97+
# location / {
98+
# root html;
99+
# index index.html index.htm;
100+
# }
101+
#}
102+
103+
104+
# HTTPS server
105+
#
106+
#server {
107+
# listen 443 ssl;
108+
# server_name localhost;
109+
110+
# ssl_certificate cert.pem;
111+
# ssl_certificate_key cert.key;
112+
113+
# ssl_session_cache shared:SSL:1m;
114+
# ssl_session_timeout 5m;
115+
116+
# ssl_ciphers HIGH:!aNULL:!MD5;
117+
# ssl_prefer_server_ciphers on;
118+
119+
# location / {
120+
# root html;
121+
# index index.html index.htm;
122+
# }
123+
#}
124+
125+
}

container/root/etc/nginx/sites-available/default

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# NOTE: the syntax in most directives is sadly duplicated in run.sh,
2-
# in order to replace at runtime with `sed`, therefore, check for changes in both places
31
server {
42
listen 80;
53

container/root/init.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/sh
22

33
RUN_SCRIPTS=/run.d
44
STATUS=0
@@ -13,7 +13,7 @@ for file in $RUN_SCRIPTS/*.sh; do
1313
echo "[init] executing ${file}"
1414

1515
# Note: -e will enforce that any subcommand that fails will fail the entire script run
16-
/bin/bash -e $file
16+
/bin/sh -e $file
1717

1818
STATUS=$? # Captures exit code from script that was run
1919

container/root/run.d/10-nginx.sh

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
1-
#!/bin/bash
2-
CONFIG_SITE=/etc/nginx/sites-available/default
3-
CONFIG_SERVER=/etc/nginx/nginx.conf
1+
#!/bin/sh
42

5-
echo '[nginx] setting sensible defaults'
6-
7-
# Configure nginx to use as many workers as there are cores for the running container
8-
# NOTE: worker_processes is only replaced when *not* set to auto
9-
sed -i "s/worker_processes [0-9]\+/worker_processes $(nproc)/" $CONFIG_SERVER
10-
sed -i "s/worker_connections [0-9]\+/worker_connections 1024/" $CONFIG_SERVER
11-
12-
# Uncomment prod-level tokens (none)
13-
sed -i "s/\#\ server_tokens/server_tokens/" $CONFIG_SERVER
14-
15-
echo '[nginx] piping logs to STDOUT'
16-
17-
# Set access/error log, and use them as a placeholder for injecting log_format key
18-
# IMPORTANT: string match the entire default access log path, making access_log + log_format injection idempotent
19-
sed -i "s/access_log \/var\/log\/nginx\/access\.log;/ log_format main \'\$remote_addr - \$remote_user [\$time_local] \"\$request\" \$status \$bytes_sent \"\$http_referer\" \"\$http_user_agent\" ${SERVER_APP_NAME}\';\n access_log \/dev\/stdout main;\n/" $CONFIG_SERVER
20-
sed -i "s/error_log [a-z\/\.\ \;]\+/error_log \/dev\/stdout info;/" $CONFIG_SERVER
3+
if [[ $SERVER_APP_NAME ]]
4+
then
5+
echo "[nginx] adding app name (${SERVER_APP_NAME}) to log format"
6+
sed -i "s/NGINX_SERVER/${SERVER_APP_NAME}/" $CONF_NGINX_SERVER
7+
else
8+
echo "[nginx] missing \$SERVER_APP_NAME to add to log lines, please add environment variable"
9+
fi
2110

2211
if [[ $SERVER_SENDFILE ]]
2312
then
2413
echo "[nginx] server sendfile status is ${SERVER_SENDFILE}"
25-
sed -i "s/sendfile on/sendfile ${SERVER_SENDFILE}/" $CONFIG_SERVER
14+
sed -i "s/sendfile .*;/sendfile ${SERVER_SENDFILE};/" $CONF_NGINX_SERVER
2615
fi
2716

2817
if [[ $SERVER_MAX_BODY_SIZE ]]
2918
then
3019
echo "[nginx] server client max body is ${SERVER_MAX_BODY_SIZE}"
31-
sed -i "s/client_max_body_size 1m/client_max_body_size ${SERVER_MAX_BODY_SIZE}/" $CONFIG_SITE
20+
sed -i "s/client_max_body_size .*;/client_max_body_size ${SERVER_MAX_BODY_SIZE};/" $CONF_NGINX_SITE
3221
fi
3322

3423
if [[ $SERVER_INDEX ]]
3524
then
3625
echo "[nginx] server index is ${SERVER_INDEX}"
37-
sed -i "s/index index.html index.htm/index ${SERVER_INDEX}/" $CONFIG_SITE
26+
sed -i "s/index .*;/index ${SERVER_INDEX};/" $CONF_NGINX_SITE
3827
fi
3928

4029
if [[ $SERVER_GZIP_OPTIONS ]]
4130
then
31+
echo "[nginx] enabling gzip"
4232
# Uncomments all gzip handling options
43-
sed -i "s/\#\ gzip/gzip/" $CONFIG_SERVER
33+
sed -i "s/\#gzip/gzip/" $CONF_NGINX_SERVER
34+
fi
35+
36+
if [[ $SERVER_KEEPALIVE ]]
37+
then
38+
echo "[nginx] setting keepalive ${SERVER_KEEPALIVE}"
39+
sed -i "s/\keepalive_timeout .*;/keepalive_timeout ${SERVER_KEEPALIVE};/" $CONF_NGINX_SERVER
40+
fi
41+
42+
if [[ $SERVER_WORKER_CONNECTIONS ]]
43+
then
44+
echo "[nginx] setting worker connection limit ${SERVER_WORKER_CONNECTIONS}"
45+
sed -i "s/\worker_connections .*;/worker_connections ${SERVER_WORKER_CONNECTIONS};/" $CONF_NGINX_SERVER
4446
fi

container/root/run.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#!/bin/bash
1+
#!/bin/sh
22

33
# Begin startup sequence
4-
/init.sh
4+
/bin/sh -e /init.sh
55

66
STATUS=$? # Captures exit code from script that was run
77

container/root/worker.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/sh
22

33
# Entrypoint for utilizing as a worker pool instead of a web server
44
# Based on configuration, can run multiple instances of a single worker process

0 commit comments

Comments
 (0)