Skip to content

Instantly share code, notes, and snippets.

@akmandev
Last active February 23, 2020 22:35
Show Gist options
  • Save akmandev/ed229ce97bec07302ebf2b5bac704151 to your computer and use it in GitHub Desktop.
Save akmandev/ed229ce97bec07302ebf2b5bac704151 to your computer and use it in GitHub Desktop.
Nginx & Php container based on Alpine Linux
## Simple Usage: docker build -t web . && docker run -p 80:8080 -it --name="web" web
FROM alpine:latest
LABEL Maintainer="Tim de Pater <code@trafex.nl>, Ozan Akman <info@ozanakman.com.tr>" \
Description="Lightweight Nginx & PHP container based on Alpine Linux."
# trust this project public key to trust the packages.
ADD https://dl.bintray.com/php-alpine/key/php-alpine.rsa.pub /etc/apk/keys/php-alpine.rsa.pub
# make sure you can use HTTPS
RUN apk --update add ca-certificates
# add the repository, make sure you replace the correct versions if you want.
RUN echo "https://dl.bintray.com/php-alpine/v3.10/php-7.4" >> /etc/apk/repositories
# install php and some extensions
RUN apk --no-cache add php php-fpm php-mysqli php-json php-openssl php-curl \
php-zlib php-xml php-phar php-intl php-dom php-xmlreader php-ctype php-session \
php-mbstring php-gd nginx supervisor curl bash
# Install composer from the official image
COPY --from=composer /usr/bin/composer /usr/bin/composer
# Configure nginx
COPY config/nginx.conf /etc/nginx/nginx.conf
# Remove default server definition
RUN rm /etc/nginx/conf.d/default.conf
# Configure PHP-FPM
COPY config/fpm-pool.conf /etc/php/php-fpm.d/www.conf
COPY config/php.ini /etc/php/conf.d/custom.ini
# Configure Php
RUN ln -s /usr/bin/php7 /usr/bin/php
# Configure supervisord
COPY config/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
# Setup document root
RUN mkdir -p /var/www/html
# Make sure files/folders needed by the processes are accessable when they run under the nobody user
RUN chown -R nobody.nobody /var/www/html && \
chown -R nobody.nobody /run && \
chown -R nobody.nobody /var/lib/nginx && \
chown -R nobody.nobody /var/tmp/ && \
chown -R nobody.nobody /var/log/nginx
# Make the document root a volume
VOLUME /var/www/html
# Switch to use a non-root user from here on
USER nobody
# Add application
WORKDIR /var/www/html
COPY --chown=nobody src/ /var/www/html/
# Expose the port nginx is reachable on
EXPOSE 8080
# Let supervisord start nginx & php-fpm
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
# Configure a healthcheck to validate that everything is up&running
HEALTHCHECK --timeout=10s CMD curl --silent --fail http://127.0.0.1:8080/fpm-ping
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment