Skip to content

Commit 2b2a2fc

Browse files
authored
Merge pull request #16753 from noone-silent/5.0.x-dockerfile
T16752-create-docker-image-on-release
2 parents 69d3524 + c9746f9 commit 2b2a2fc

File tree

4 files changed

+224
-17
lines changed

4 files changed

+224
-17
lines changed

.github/workflows/build-docker.yml

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,55 @@ name: Build Dockerfiles
22

33
on:
44
push:
5-
paths:
6-
- 'docker/**'
7-
8-
permissions:
9-
contents: read # to fetch code (actions/checkout)
5+
tags:
6+
- v*
107

118
jobs:
129
build:
1310
runs-on: ubuntu-latest
14-
11+
permissions:
12+
contents: read
13+
packages: write
14+
attestations: write
15+
id-token: write
1516
strategy:
1617
fail-fast: false
1718
matrix:
18-
php:
19-
- '8.1'
20-
- '8.2'
21-
- '8.3'
22-
- '8.4'
19+
php: [ '8.1', '8.2', '8.3', '8.4' ]
2320

2421
name: Build Dockerfile PHP ${{ matrix.php }}
2522
steps:
2623
- uses: actions/checkout@v4
2724

28-
- name: Build Dockerfile
29-
run: docker build docker/${{ matrix.php }}
25+
- name: Login to Github Registry
26+
uses: docker/login-action@v3
27+
with:
28+
registry: ghcr.io
29+
username: ${{ github.repository_owner }}
30+
password: ${{ secrets.GITHUB_TOKEN }}
31+
32+
- name: Login to Docker Hub
33+
uses: docker/login-action@v3
34+
with:
35+
username: ${{ secrets.DOCKERHUB_LOGIN }}
36+
password: ${{ secrets.DOCKERHUB_TOKEN }}
37+
38+
- name: Set up QEMU
39+
uses: docker/setup-qemu-action@v3
40+
41+
- name: Set up Docker Buildx
42+
uses: docker/setup-buildx-action@v3
43+
44+
- name: Build and push
45+
uses: docker/build-push-action@v6
46+
with:
47+
push: true
48+
file: docker/Dockerfile
49+
tags: |
50+
phalcon/cphalcon:${{ github.ref_name }}-php${{ matrix.php }}
51+
ghcr.io/phalcon/cphalcon:${{ github.ref_name }}-php${{ matrix.php }}
52+
build-args: |
53+
PHP_VERSION=${{ matrix.php }}
54+
PHALCON_VERSION=${{ github.ref_name }}
55+
cache-from: type=gha
56+
cache-to: type=gha,mode=max

CHANGELOG-5.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [5.9.3](https://github.com/phalcon/cphalcon/releases/tag/v5.9.3) (2025-xx-xx)
44

55
### Changed
6+
- Added Multi-Stage Dockerfile and Github action for release Docker images to ghcr.io and Docker Hub. [#16752](https://github.com/phalcon/cphalcon/issues/16752)
67

78
### Added
89

docker-compose.yml

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
# For local development only.
22

33
services:
4+
cphalcon-8.0:
5+
container_name: cphalcon-8.0
6+
hostname: cphalcon-80
7+
build:
8+
dockerfile: docker/Dockerfile
9+
target: dev
10+
args:
11+
PHP_VERSION: 8.0
12+
working_dir: /srv
13+
volumes:
14+
- .:/srv
15+
416
cphalcon-8.1:
517
container_name: cphalcon-8.1
618
hostname: cphalcon-81
7-
build: docker/8.1
19+
build:
20+
dockerfile: docker/Dockerfile
21+
target: dev
22+
args:
23+
PHP_VERSION: 8.1
824
working_dir: /srv
925
ports:
1026
- "9501:9501"
@@ -14,23 +30,35 @@ services:
1430
cphalcon-8.2:
1531
container_name: cphalcon-8.2
1632
hostname: cphalcon-82
17-
build: docker/8.2
33+
build:
34+
dockerfile: docker/Dockerfile
35+
target: dev
36+
args:
37+
PHP_VERSION: 8.2
1838
working_dir: /srv
1939
volumes:
2040
- .:/srv
2141

2242
cphalcon-8.3:
2343
container_name: cphalcon-8.3
2444
hostname: cphalcon-83
25-
build: docker/8.3
45+
build:
46+
dockerfile: docker/Dockerfile
47+
target: dev
48+
args:
49+
PHP_VERSION: 8.3
2650
working_dir: /srv
2751
volumes:
2852
- .:/srv
2953

3054
cphalcon-8.4:
3155
container_name: cphalcon-8.4
3256
hostname: cphalcon-84
33-
build: docker/8.4
57+
build:
58+
dockerfile: docker/Dockerfile
59+
target: dev
60+
args:
61+
PHP_VERSION: 8.4
3462
working_dir: /srv
3563
volumes:
3664
- .:/srv

docker/Dockerfile

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
ARG PHP_VERSION=8.4
2+
3+
FROM php:${PHP_VERSION}-fpm AS base
4+
5+
# This part installs the required php extensions to compile phalcon.
6+
# Additional extensions that are mostly used are installed here too.
7+
8+
ARG PHP_VERSION=8.4
9+
ARG UID=1000
10+
ARG GID=1000
11+
ARG USER=phalcon
12+
ARG GROUP=phalcon
13+
14+
# hadolint ignore=DL3022
15+
COPY --from=ghcr.io/mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
16+
17+
SHELL [ "/bin/bash", "-o", "pipefail", "-c" ]
18+
19+
RUN set -eux \
20+
# Add user and group
21+
&& groupadd -g "${GID}" "${GROUP}" \
22+
&& useradd -l -u "${UID}" -g "${GID}" -d /app "${USER}" \
23+
&& usermod -s /bin/bash "${USER}" \
24+
&& mkdir /app \
25+
&& chown "${USER}":"${GROUP}" /app \
26+
&& chmod 0770 /app \
27+
# Install base extensions
28+
&& install-php-extensions \
29+
apcu \
30+
gettext \
31+
gd \
32+
igbinary \
33+
imagick \
34+
intl \
35+
mysqli \
36+
opcache \
37+
pdo_mysql \
38+
pdo_pgsql \
39+
pgsql \
40+
redis \
41+
xsl \
42+
yaml \
43+
zip \
44+
# Copy ini file \
45+
&& mv /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini \
46+
# Write ini files
47+
&& sed -i -e "s/;error_log = syslog/error_log = \/proc\/self\/fd\/2/" /usr/local/etc/php/php.ini \
48+
# Write fpm file
49+
&& sed -i -e "/error_log = .*/c\error_log = \/proc\/self\/fd\/2" /usr/local/etc/php-fpm.conf \
50+
&& sed -i -e "/;pid = .*/c\pid = /run/php/php-fpm.pid" /usr/local/etc/php-fpm.conf \
51+
# Write fpm pool
52+
&& sed -i -e "s/^;clear_env = no$/clear_env = no/" /usr/local/etc/php-fpm.d/www.conf \
53+
&& sed -i -e "s/;owner = www-data/owner = ${USER}/g" /usr/local/etc/php-fpm.d/www.conf \
54+
&& sed -i -e "s/;user = www-data/user = ${USER}/g" /usr/local/etc/php-fpm.d/www.conf \
55+
&& sed -i -e "s/;group = www-data/group = ${GROUP}/g" /usr/local/etc/php-fpm.d/www.conf \
56+
# Load healthcheck script \
57+
&& curl -o /usr/local/bin/php-fpm-healthcheck \
58+
https://raw.githubusercontent.com/renatomefi/php-fpm-healthcheck/master/php-fpm-healthcheck \
59+
&& chown "${USER}":"${GROUP}" /usr/local/bin/php-fpm-healthcheck \
60+
&& chmod 0770 /usr/local/bin/php-fpm-healthcheck \
61+
# Set correct pid file location and permissions \
62+
&& mkdir -p /run/php \
63+
&& chown "${USER}":"${GROUP}" /run/php \
64+
&& chmod 0770 /run/php \
65+
# Cleanup
66+
&& apt-get autoremove --purge -y \
67+
&& apt-get autoclean -y \
68+
&& apt-get clean -y \
69+
&& rm -rf /tmp/* /var/tmp/* \
70+
&& find /var/cache/apt/archives /var/lib/apt/lists -not -name lock -type f -delete \
71+
&& find /var/cache -type f -delete \
72+
&& find /var/log -type f -delete
73+
74+
HEALTHCHECK --interval=5s --timeout=1s \
75+
CMD php-fpm-healthcheck || exit 1
76+
77+
FROM base AS dev
78+
79+
# This part prepares a dev environment to compile phalcon from zephir
80+
81+
# hadolint ignore=DL3022
82+
COPY --from=composer/composer:2 --chown=${USER}:${GROUP} --chmod=0770 /usr/bin/composer /usr/bin/composer
83+
84+
WORKDIR /srv
85+
86+
SHELL [ "/bin/bash", "-o", "pipefail", "-c" ]
87+
88+
RUN set -eux \
89+
&& apt-get update \
90+
&& apt-get install -yq --no-install-recommends zip=3.* unzip=6.* \
91+
&& echo 'memory_limit = -1' > "$(php-config --ini-dir)/90-memory.ini" \
92+
&& install-php-extensions zephir_parser \
93+
# Cleanup
94+
&& apt-get autoclean -y \
95+
&& apt-get clean -y \
96+
&& rm -rf /tmp/* /var/tmp/*
97+
98+
FROM dev AS phalcon
99+
100+
# This part compiles phalcon from zephir code. It should not be used for anything else.
101+
# It should also not be used as base for anything else.
102+
103+
COPY ./ /srv
104+
105+
RUN set -eux \
106+
&& rm -rf /srv/vendor \
107+
&& composer global require phalcon/zephir:dev-development \
108+
&& /root/.composer/vendor/bin/zephir fullclean \
109+
&& /root/.composer/vendor/bin/zephir build \
110+
&& cat compile-errors.log
111+
112+
FROM base AS prod
113+
114+
# This part builds the last step required for production image.
115+
116+
ARG PHALCON_VERSION=5.9.2
117+
118+
ENV PATH=/app/bin:/app:${PATH} \
119+
PHP_VERSION=${PHP_VERSION} \
120+
PHALCON_VERSION=${PHALCON_VERSION}
121+
122+
LABEL org.opencontainer.image.title="Phalcon ${PHALCON_VERSION} with php ${PHP_VERSION}" \
123+
org.opencontainer.image.description="Docker image including Phalcon and PHP on Debian Bookworm" \
124+
org.opencontainer.image.authors="Phalcon Team <team@phalconphp.com>" \
125+
org.opencontainer.image.vendor="Phalcon PHP Framework" \
126+
org.opencontainer.image.licenses="BSD-3-Clause" \
127+
org.opencontainer.image.version="${PHALCON_VERSION}-php${PHP_VERSION}" \
128+
org.opencontainer.image.url="https://github.com/phalcon/cphalcon/" \
129+
org.opencontainer.image.source="https://github.com/phalcon/cphalcon/tree/${PHALCON_VERSION}/docker/Dockerfile"
130+
131+
COPY --from=phalcon /srv/ext/modules/phalcon.so /tmp/phalcon.so
132+
133+
SHELL [ "/bin/bash", "-o", "pipefail", "-c" ]
134+
135+
RUN set -eux \
136+
# Install phalcon \
137+
&& mv /tmp/phalcon.so "$(php-config --extension-dir)/phalcon.so" \
138+
&& echo "extension=phalcon.so" > /usr/local/etc/php/conf.d/70-phalcon.ini \
139+
# Cleanup
140+
&& rm -rf /tmp/* /var/tmp/* /srv /root/.composer /usr/bin/composer \
141+
&& apt-get autoremove --purge -y curl \
142+
&& apt-get autoclean -y \
143+
&& apt-get clean -y \
144+
&& rm -rf /tmp/* /var/tmp/* \
145+
&& find /var/cache/apt/archives /var/lib/apt/lists -not -name lock -type f -delete \
146+
&& find /var/cache -type f -delete \
147+
&& find /var/log -type f -delete
148+
149+
WORKDIR /app
150+
151+
USER ${USER}

0 commit comments

Comments
 (0)