Skip to content

Instantly share code, notes, and snippets.

@wojas
Created June 3, 2016 06:29
Show Gist options
  • Save wojas/089dbf1a8d26345f836eb277c8edceb4 to your computer and use it in GitHub Desktop.
Save wojas/089dbf1a8d26345f836eb277c8edceb4 to your computer and use it in GitHub Desktop.
docker-compose for nginx with php5-fpm using small alpine images
version: '2'
services:
php:
image: php:5.6-fpm-alpine
volumes:
- data:/_data
- ./site:/var/www/html:ro
networks:
- backend
command: sh -c 'chown www-data /_data && exec php-fpm'
nginx:
image: nginx:stable-alpine
volumes:
- ./site:/var/www/html:ro
- ./nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- "8000:80"
networks:
- backend
networks:
backend:
volumes:
data:
driver: local
user nginx;
worker_processes 1;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /dev/stdout main;
#sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root html;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
location ~ /\.ht {
deny all;
}
}
}
@wojas
Copy link
Author

wojas commented Jun 3, 2016

Main challenge with making the php-fpm and nginx images work together is that they both need access to the same files. Nginx needs to be able to find the php files to decide to forward the request to php-fpm, and will serve all static files. Php usually only requires the php files, but might need other files depending on the application.

With this compose file, the site files are kept outside of the docker images, in the checkout folder from which you run docker-compose. This has the added benefit of making it useable for development. If you want to build an image that ships the whole site, you end up adding the same files both to the nginx image and the php image. Alternatively, you can use a shared volume, but then you need a way to inject the site after the build, since volumes are only available during runtime.

All logs are sent to stdout and stderr. You can redirect them to a logging service by configuring logging, or by changing the nginx config to log to files in a volume instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment