Skip to content

Commit

Permalink
refactor: ♻️ add nginx to docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
haipham22 committed Jul 27, 2023
1 parent 901d27a commit 6f287b3
Show file tree
Hide file tree
Showing 16 changed files with 410 additions and 118 deletions.
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
# Maintain dependencies for GitHub Actions
- package-ecosystem: "docker"
directory: "/"
schedule:
interval: "daily"
64 changes: 43 additions & 21 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,55 @@
name: Build docker php-fpm-composer

on:
# schedule:
# - cron: "0 13 * * 1" # run on every monday
schedule:
- cron: "0 2 1 * *" # At 02:00 on day-of-month 1.
push:
branches:
- main

concurrency:
concurrency:
group: ${{ github.ref }}
cancel-in-progress: true

env:
IMAGE_NAME: haipham22/nginx-php-fpm

jobs:
prepare:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build docker images
uses: docker/build-push-action@v3
with:
context: .
file: Dockerfile
cache-from: ${{ env.IMAGE_NAME }}:8.1-fpm
platforms: linux/amd64
tags: |-
${{ env.IMAGE_NAME }}:latest
build-args: PHP_FPM_VERSION=8.1-fpm-alpine

buildx:
needs: prepare
strategy:
matrix:
dockerfiles: ["7.3", "7.4", "8.0", "8.1"]
use-debug: [1, 0]
php-version:
[
"7.3-fpm-alpine",
"7.4-fpm-alpine",
"8.0-fpm-alpine",
"8.1-fpm-alpine",
"8.2-fpm-alpine",
]
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -35,25 +68,14 @@ jobs:
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and push
if: ${{ matrix.use-debug == 0 }}
uses: docker/build-push-action@v3
with:
context: .
file: Dockerfile-alpine
file: Dockerfile
platforms: linux/amd64,linux/arm64
pull: true
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/php-fpm-composer:${{ matrix.dockerfiles }}-alpine
build-args: PHP_BUILD_VERSION=${{ matrix.dockerfiles }}

- name: Build and push with xdebug
if: ${{ matrix.use-debug == 1 }}
uses: docker/build-push-action@v3
with:
context: .
file: Dockerfile-alpine-xdebug
platforms: linux/amd64,linux/arm64
push: true
pull: true
tags: ${{ secrets.DOCKER_USERNAME }}/php-fpm-composer:${{ matrix.dockerfiles }}-alpine-xdebug
build-args: PHP_BUILD_VERSION=${{ matrix.dockerfiles }}
cache-from: ${{ env.IMAGE_NAME }}:${{ matrix.php-version }}
tags: |-
${{ env.IMAGE_NAME }}:${{ matrix.php-version }}
build-args: PHP_FPM_VERSION=${{ matrix.php-version }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea/*
.env
.env.*
51 changes: 51 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
ARG PHP_FPM_VERSION=8.1-fpm-alpine

FROM php:${PHP_FPM_VERSION}
LABEL Maintainer="Hải Phạm <[email protected]>"
LABEL Description="Lightweight container with Nginx 1.24 & PHP based on Alpine Linux."

# Setup document root
RUN mkdir -p /app

WORKDIR /app

# https://github.com/mlocati/docker-php-extension-installer
# Install packages and remove default server definition
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/

RUN chmod +x /usr/local/bin/install-php-extensions && \
sync && \
install-php-extensions ctype curl dom gd intl mbstring mysqli opcache openssl phar session xml memcached xmlreader && \
apk add --no-cache curl nginx supervisor && \
rm -rf /var/cache/apk/*

# Configure nginx - http
COPY config/nginx.conf /etc/nginx/nginx.conf
# Configure nginx - default server
COPY config/conf.d /etc/nginx/conf.d/

# Configure PHP-FPM
COPY config/php-fpm.d/zz-docker.conf /usr/local/etc/php-fpm.d/zz-docker.conf
COPY config/php-fpm.d/fpm-pool.conf /usr/local/etc/php-fpm.d/www.conf
COPY config/php.ini /usr/local/etc/php/conf.d/custom.ini

# Configure supervisord
COPY config/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Make sure files/folders needed by the processes are accessable when they run under the www-data user
RUN chown -R www-data.www-data /app /run /var/lib/nginx /var/log/nginx

# Switch to use a non-root user from here on
USER www-data

# Add application
COPY --chown=www-data src/ /app/

# 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
19 changes: 0 additions & 19 deletions Dockerfile-alpine

This file was deleted.

19 changes: 0 additions & 19 deletions Dockerfile-alpine-xdebug

This file was deleted.

138 changes: 79 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,92 +1,112 @@
# php-fpm-composer
# Docker PHP-FPM & Nginx 1.24 on Alpine Linux

## Usage

Start the Docker container:

## Getting started
```shell
docker run -p 80:8080 haipham22/nginx-php-fpm
```

To make it easy for you to get started with GitLab, here's a list of recommended next steps.
See the PHP info on http://localhost, or the static html page on http://localhost/test.html

Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
Or mount your own code to be served by PHP-FPM & Nginx

## Add your files
```shell
docker run -p 80:8080 -v ~/sourcecode:/app haipham22/nginx-php-fpm
```

- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
## Use in local development

```
cd existing_repo
git remote add origin https://gitlab.com/haipham22/php-fpm-composer.git
git branch -M main
git push -uf origin main
```
PHP configuration `custom.ini`

```ini
[Date]
date.timezone="UTC"
expose_php= Off

## Integrate with your tools
[Opcache]
opcache.enable=0
opcache.memory_consumption=64
opcache.interned_strings_buffer=64
opcache.max_accelerated_files=32531
opcache.validate_timestamps=0
opcache.save_comments=1
opcache.fast_shutdown=0
```

- [ ] [Set up project integrations](https://gitlab.com/haipham22/php-fpm-composer/-/settings/integrations)
Run with docker

## Collaborate with your team
```shell
docker run -v "`pwd`/custom.ini:/usr/local/etc/php/conf.d/custom.ini" haipham22/nginx-php-fpm
```

- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
Run with docker compose

## Test and Deploy
```yaml
version: "3"

Use the built-in continuous integration in GitLab.
services:
app:
build: .
image: haipham22/nginx-php-fpm
volumes:
- ./custom.ini:/usr/local/etc/php/conf.d/custom.ini
- ./:/app
ports:
- "8080:8080"
```
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
## Adding composer
***
If you need [Composer](https://getcomposer.org/) in your project, here's an easy way to add it.
# Editing this README
```Dockerfile
FROM haipham22/nginx-php-fpm:latest

When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
# Install composer via install-php-extensions
RUN install-php-extensions composer

## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
# Run composer install to install the dependencies
RUN composer install --optimize-autoloader --no-interaction --no-progress
```

## Name
Choose a self-explaining name for your project.
## Configuration

## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
In [config/](config/) you'll find the default configuration files for Nginx, PHP and PHP-FPM.
If you want to extend or customize that you can do so by mounting a configuration file in the correct folder;

## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
Nginx configuration:

## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
```shell
docker run -v "`pwd`/nginx-server.conf:/etc/nginx/conf.d/server.conf" haipham22/nginx-php-fpm
```

## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
PHP configuration:

## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
```shell
docker run -v "`pwd`/php-setting.ini:/usr/local/etc/php/conf.d/settings.ini" haipham22/nginx-php-fpm
```

## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
PHP-FPM configuration:

## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
```shell
docker run -v "`pwd`/php-fpm-settings.conf:/usr/local/etc/php-fpm.d/server.conf" haipham22/nginx-php-fpm
```

## Contributing
State if you are open to contributions and what your requirements are for accepting them.
## TODO

For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
- [x] Add nginx inside docker
- [x] Change `fastcgi_pass` to `unix` socket instead `TCP` socket
- [ ] Optimize docker image size
- [ ] Add defaults 5xx page html
- [ ] Testing for running

You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Acknowledgements

## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
This project started as a fork of https://github.com/TrafeX/docker-php-nginx. Thanks TrafeX!

## License
For open source projects, say how it is licensed.
Different with original repo:

## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
- Use php:xxx-fpm-alpine as base docker images
- Use [mlocati/docker-php-extension-installer](https://github.com/mlocati/docker-php-extension-installer) to easy switch php versions ex 7.3, 7.4, 8.0,...
14 changes: 14 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: "3"

services:
app:
build: .
ports:
- "8080:8080"
sut:
image: alpine:3.13
depends_on:
- app
command: /tmp/run_tests.sh
volumes:
- "./run_tests.sh:/tmp/run_tests.sh:ro"
Loading

0 comments on commit 6f287b3

Please sign in to comment.