Skip to content

Commit 659806c

Browse files
committed
Run the services in the container as non-privileged user
1 parent ba1dd42 commit 659806c

File tree

5 files changed

+63
-14
lines changed

5 files changed

+63
-14
lines changed

Dockerfile

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,35 @@ RUN apk --no-cache add php7 php7-fpm php7-mysqli php7-json php7-openssl php7-cur
1111
COPY config/nginx.conf /etc/nginx/nginx.conf
1212

1313
# Configure PHP-FPM
14-
COPY config/fpm-pool.conf /etc/php7/php-fpm.d/zzz_custom.conf
14+
COPY config/fpm-pool.conf /etc/php7/php-fpm.d/www.conf
1515
COPY config/php.ini /etc/php7/conf.d/zzz_custom.ini
1616

1717
# Configure supervisord
1818
COPY config/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
1919

20-
# Add application
20+
# Make sure files/folders needed by the processes are accessable when they run under the nobody user
21+
RUN touch /run/nginx.pid && \
22+
touch /run/supervisord.pid && \
23+
chown -R nobody.nobody /run/nginx.pid && \
24+
chown -R nobody.nobody /run/supervisord.pid && \
25+
chown -R nobody.nobody /var/tmp/nginx && \
26+
chown -R nobody.nobody /var/lib/nginx/logs
27+
28+
# Setup document root
2129
RUN mkdir -p /var/www/html
30+
31+
# Switch to use a non-root user from here on
32+
USER nobody
33+
34+
# Add application
2235
WORKDIR /var/www/html
23-
COPY src/ /var/www/html/
36+
COPY --chown=nobody src/ /var/www/html/
37+
38+
# Expose the port nginx is reachable on
39+
EXPOSE 8080
2440

25-
EXPOSE 80
41+
# Let supervisord start nginx & php-fpm
2642
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
2743

44+
# Configure a healthcheck to validate that everything is up&running
2845
HEALTHCHECK --timeout=10s CMD curl --silent --fail http://127.0.0.1/fpm-ping

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1-
Docker PHP-FPM 7.2 & Nginx 1.14 on Alpine Linux
2-
==============================================
1+
# Docker PHP-FPM 7.2 & Nginx 1.14 on Alpine Linux
32
Example PHP-FPM 7.2 & Nginx 1.14 setup for Docker, build on [Alpine Linux](http://www.alpinelinux.org/).
43
The image is only +/- 35MB large.
54

65

6+
* Built on the lightweight and secure Alpine Linux distribution
7+
* Very small Docker image size (+/-35MB)
8+
* Uses PHP 7.2 for better performance, lower cpu usage & memory footprint
9+
* Optimized for 100 concurrent users
10+
* Optimized to only use resources when there's traffic (by using PHP-FPM's ondemand PM)
11+
* The servers Nginx, PHP-FPM and supervisord run under a non-privileged user (nobody) to make it more secure
12+
* The logs of all the services are redirected to the output of the Docker container (visible with `docker logs -f <container name>`)
13+
14+
715
[![Docker Pulls](https://img.shields.io/docker/pulls/trafex/alpine-nginx-php7.svg)](https://hub.docker.com/r/trafex/alpine-nginx-php7/)
816

9-
Usage
10-
-----
11-
Start the Docker containers:
17+
### Breaking changes (26/01/2019)
18+
19+
Please note that the new builds since 26/01/2019 are exposing a different port to access Nginx.
20+
To be able to run Nginx as a non-privileged user, the port it's running on needed
21+
to change to a non-privileged port (above 1024).
22+
23+
The last build of the old version that exposed port 80 was `trafex/alpine-nginx-php7:ba1dd422`
24+
25+
## Usage
26+
27+
Start the Docker container:
1228

13-
docker run -p 80:80 trafex/alpine-nginx-php7
29+
docker run -p 80:8080 trafex/alpine-nginx-php7
1430

1531
See the PHP info on http://localhost, or the static html page on http://localhost/test.html

config/fpm-pool.conf

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
error_log = /dev/stderr
44

55
[www]
6+
; The address on which to accept FastCGI requests.
7+
; Valid syntaxes are:
8+
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
9+
; a specific port;
10+
; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
11+
; a specific port;
12+
; 'port' - to listen on a TCP socket to all addresses
13+
; (IPv6 and IPv4-mapped) on a specific port;
14+
; '/path/to/unix/socket' - to listen on a unix socket.
15+
; Note: This value is mandatory.
16+
listen = 127.0.0.1:9000
17+
618
; Enable status page
719
pm.status_path = /fpm-status
820

@@ -18,7 +30,7 @@ pm = ondemand
1830
; forget to tweak pm.* to fit your needs.
1931
; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
2032
; Note: This value is mandatory.
21-
pm.max_children = 50
33+
pm.max_children = 100
2234

2335
; The number of seconds after which an idle process will be killed.
2436
; Note: Used only when pm is set to 'ondemand'
@@ -29,7 +41,7 @@ pm.process_idle_timeout = 10s;
2941
; This can be useful to work around memory leaks in 3rd party libraries. For
3042
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
3143
; Default Value: 0
32-
pm.max_requests = 500
44+
pm.max_requests = 1000
3345

3446
; Make sure the FPM workers can reach the environment variables for configuration
3547
clear_env = no

config/nginx.conf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
worker_processes 1;
2+
error_log stderr warn;
23
pid /run/nginx.pid;
34

45
events {
@@ -20,8 +21,8 @@ http {
2021
keepalive_timeout 65;
2122

2223
server {
23-
listen [::]:80 default_server;
24-
listen 80 default_server;
24+
listen [::]:8080 default_server;
25+
listen 8080 default_server;
2526
server_name _;
2627

2728
sendfile off;

config/supervisord.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
[supervisord]
22
nodaemon=true
3+
logfile=/dev/null
4+
logfile_maxbytes=0
5+
pidfile=/run/supervisord.pid
36

47
[program:php-fpm]
58
command=php-fpm7 -F

0 commit comments

Comments
 (0)