Skip to content

Commit

Permalink
Merge pull request #580 from clementvtrd/feat/standalone-php-image
Browse files Browse the repository at this point in the history
feat: add standalone target
  • Loading branch information
clementvtrd authored Jun 28, 2024
2 parents 07670b5 + 27e789e commit 7ee2a9a
Show file tree
Hide file tree
Showing 25 changed files with 229 additions and 166 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install Task
uses: arduino/setup-task@v2
Expand All @@ -49,8 +47,6 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install Task
uses: arduino/setup-task@v2
Expand Down
26 changes: 26 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Docker

on:
push:
paths:
- '**/Dockerfile'
- .github/workflows/docker.yaml

jobs:
hadolint:

runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
dockerfile: [docker/api/Dockerfile, docker/web/Dockerfile]

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Lint Dockerfile
uses: hadolint/hadolint-action@master
with:
dockerfile: ${{ matrix.dockerfile }}
2 changes: 0 additions & 2 deletions .github/workflows/web.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install Task
uses: arduino/setup-task@v2
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[![PHP Code Quality](https://github.com/clementvtrd/boilerplate-hexagonal/actions/workflows/api.yaml/badge.svg?branch=develop)](https://github.com/clementvtrd/boilerplate-hexagonal/actions/workflows/api.yaml)
[![Docker](https://github.com/clementvtrd/boilerplate-hexagonal/actions/workflows/docker.yaml/badge.svg)](https://github.com/clementvtrd/boilerplate-hexagonal/actions/workflows/docker.yaml)
[![Web Code Quality](https://github.com/clementvtrd/boilerplate-hexagonal/actions/workflows/web.yaml/badge.svg)](https://github.com/clementvtrd/boilerplate-hexagonal/actions/workflows/web.yaml)

# Hexagonal boilerplate

Expand Down
8 changes: 4 additions & 4 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ tasks:
cmds:
- task: yarn
vars:
CLI_ARGS: install --frozen-lockfile
CLI_ARGS: install --immutable
sources:
- ./apps/web/package.json
- ./apps/web/yarn.lock
Expand All @@ -65,12 +65,12 @@ tasks:
composer:
desc: Shortcut to run Composer CLI
cmds:
- docker compose -f docker-compose.yml run --rm php composer {{.CLI_ARGS}}
- docker compose -f docker-compose.yml run --rm api composer {{.CLI_ARGS}}

console:
desc: Shortcut to run Symfony console
cmds:
- docker compose run --rm php console {{.CLI_ARGS}}
- docker compose run --rm api bin/console {{.CLI_ARGS}}

phpstan:
desc: Shortcut to run PHPStan
Expand Down Expand Up @@ -116,4 +116,4 @@ tasks:
CLI_ARGS: doctrine:database:create --if-not-exists {{.CLI_ARGS}}
- task: console
vars:
CLI_ARGS: doctrine:migrations:migrate {{.CLI_ARGS}}
CLI_ARGS: doctrine:migrations:migrate --allow-no-migration {{.CLI_ARGS}}
2 changes: 1 addition & 1 deletion apps/api/.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###> symfony/framework-bundle ###
APP_ENV=dev
APP_ENV=prod
APP_SECRET=c12d7d08f4f220319c0585636ac85352
###< symfony/framework-bundle ###

Expand Down
111 changes: 0 additions & 111 deletions apps/api/Dockerfile

This file was deleted.

5 changes: 3 additions & 2 deletions apps/api/config/packages/twig.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
twig:
default_path: '%kernel.project_dir%/src/Infrastructure/Twig/templates'
when@dev:
twig:
default_path: '%kernel.project_dir%/src/Infrastructure/Twig/templates'

when@test:
twig:
Expand Down
2 changes: 0 additions & 2 deletions apps/api/php/conf.d-dev/opcache.ini

This file was deleted.

2 changes: 0 additions & 2 deletions apps/api/php/conf.d-test/opcache.ini

This file was deleted.

2 changes: 0 additions & 2 deletions apps/api/php/conf.d/opcache.ini

This file was deleted.

2 changes: 2 additions & 0 deletions apps/web/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
defaultSemverRangePrefix: ""

nodeLinker: node-modules

enableGlobalCache: false
4 changes: 3 additions & 1 deletion docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
services:
php:
api:
build:
context: ./apps/api
target: test
env_file:
- ./apps/api/.env
volumes:
- ./apps/api:/app
depends_on:
database:
condition: service_healthy
Expand Down
33 changes: 14 additions & 19 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,32 @@ services:
volumes:
- ./certs:/etc/certs:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
depends_on:
api: { condition: service_healthy }
node: { condition: service_started }

api:
build:
context: ./services/nginx
dockerfile: Dockerfile
target: nginx-dev
restart: unless-stopped
labels:
- "traefik.enable=true"
- "traefik.http.routers.nginx.tls=true"
- "traefik.http.routers.nginx.rule=Host(`api.app.localhost`)"
- "traefik.http.services.nginx.loadbalancer.server.port=80"
volumes:
- ./apps/api:/app:ro

php:
build:
context: ./apps/api
context: .
dockerfile: docker/api/Dockerfile
target: dev
env_file:
- ./apps/api/.env
volumes:
- ./apps/api:/app
restart: unless-stopped
labels:
- "traefik.enable=true"
- "traefik.http.routers.api.tls=true"
- "traefik.http.routers.api.rule=Host(`api.app.localhost`)"
- "traefik.http.services.api.loadbalancer.server.port=80"
depends_on:
database:
condition: service_healthy
restart: true
database: { condition: service_healthy }

node:
build:
context: ./apps/web
context: .
dockerfile: docker/web/Dockerfile
target: dev
env_file:
- ./apps/web/.env
Expand Down
9 changes: 9 additions & 0 deletions docker/api/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*

!bin/
!config/
!public/
!src/
!composer.json
!composer.lock
!symfony.lock
86 changes: 86 additions & 0 deletions docker/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
FROM composer:2.7.6 AS composer

#####################################

FROM php:8.3-fpm-alpine3.17 AS base

WORKDIR /app

ARG UID=1000

RUN adduser -D -u ${UID} -G www-data user \
&& chown -R user:www-data /app

RUN mkdir -p /app && chown -R user:www-data /app

RUN apk update && apk add --no-cache \
autoconf=2.71-r1 \
bash=5.2.15-r0 \
git=2.38.5-r0 \
libzip-dev=1.9.2-r2 \
icu-dev=72.1-r1 \
rabbitmq-c-dev=0.11.0-r5 \
nginx=1.22.1-r1

# hadolint ignore=DL3018,SC2086
RUN apk add --no-cache --virtual .phpize-deps ${PHPIZE_DEPS}

RUN pecl install apcu amqp && docker-php-ext-enable apcu amqp

RUN docker-php-ext-configure zip && docker-php-ext-install -j"$(nproc)" \
zip \
intl \
opcache \
pdo \
pdo_mysql

RUN apk del --no-network .phpize-deps

COPY --chown=user:www-data --chmod=550 docker/api/php/www.conf /usr/local/etc/php-fpm.d/www.conf
COPY --chown=user:www-data --chmod=550 docker/api/nginx/nginx.conf /etc/nginx/nginx.conf
COPY --chown=user:www-data --chmod=550 docker/api/nginx/site.conf /etc/nginx/conf.d/default.conf
COPY --chown=user:www-data --chmod=550 docker/api/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh

RUN chown -R user:www-data /var/lib/nginx && chmod -R 770 /var/lib/nginx \
&& chown -R user:www-data /var/log/nginx && chmod -R 770 /var/log/nginx

USER user:www-data

HEALTHCHECK --interval=60s --timeout=5s \
CMD [ "200" = "$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1/ping)" ] || exit 1

CMD ["nginx", "-g", "daemon off;"]
ENTRYPOINT ["docker-entrypoint.sh"]

#####################################

FROM base AS dev

ENV APP_ENV=dev
ENV APP_DEBUG=1

COPY --from=composer /usr/bin/composer /usr/local/bin/composer
COPY docker/api/php/php-dev.ini /usr/local/etc/php/php.ini

#####################################

FROM dev AS test

ENV APP_ENV=test
ENV APP_DEBUG=0

COPY docker/api/php/php-test.ini /usr/local/etc/php/php.ini

#####################################

FROM base AS release

ENV APP_ENV=prod
ENV APP_DEBUG=0

COPY --from=composer --chown=user:www-data /usr/bin/composer /usr/local/bin/composer
COPY --chown=user:www-data apps/api /app/
COPY --chown=user:www-data docker/api/php/php-prod.ini /usr/local/etc/php/php.ini

RUN composer install --no-dev --no-scripts --no-interaction --no-progress \
&& composer dump-autoload --optimize --no-dev --classmap-authoritative
Loading

0 comments on commit 7ee2a9a

Please sign in to comment.