From 17d9793699952eb8bdd5e3b40b10c711027014d0 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Sat, 14 Jan 2023 10:14:57 +0200 Subject: [PATCH 1/7] Use new buildx 0.8 feature "contexts" --- Dockerfile | 8 ++++---- Dockerfile.alpine | 8 ++++---- docker-bake.hcl | 25 ++++++++++++++++++++----- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index 634d2d6196..fa8cacb5ae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ -FROM php:8.2-zts-bullseye AS builder +FROM php-base AS builder -COPY --from=golang:1.19-bullseye /usr/local/go/bin/go /usr/local/bin/go -COPY --from=golang:1.19-bullseye /usr/local/go /usr/local/go +COPY --from=golang-base /usr/local/go/bin/go /usr/local/bin/go +COPY --from=golang-base /usr/local/go /usr/local/go # This is required to link the frankenPHP binary to the PHP binary RUN apt-get update && \ @@ -45,7 +45,7 @@ RUN cd caddy/frankenphp && \ ENTRYPOINT ["/bin/bash","-c"] -FROM php:8.2-zts-bullseye AS final +FROM php-base AS final COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/ diff --git a/Dockerfile.alpine b/Dockerfile.alpine index 542989b694..1c548bf7ad 100644 --- a/Dockerfile.alpine +++ b/Dockerfile.alpine @@ -1,7 +1,7 @@ -FROM php:8.2-zts-alpine3.17 AS builder +FROM php-base AS builder -COPY --from=golang:1.19-alpine3.17 /usr/local/go/bin/go /usr/local/bin/go -COPY --from=golang:1.19-alpine3.17 /usr/local/go /usr/local/go +COPY --from=golang-base /usr/local/go/bin/go /usr/local/bin/go +COPY --from=golang-base /usr/local/go /usr/local/go RUN apk add --no-cache --virtual .build-deps \ $PHPIZE_DEPS \ @@ -44,7 +44,7 @@ RUN cd caddy/frankenphp && \ ENTRYPOINT ["/bin/sh","-c"] -FROM php:8.2-zts-alpine3.17 AS final +FROM php-base AS final COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/ diff --git a/docker-bake.hcl b/docker-bake.hcl index 097762efe9..9137de7d0b 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -3,10 +3,19 @@ variable "REPO_NAME" { } group "default" { - targets = ["bullseye", "alpine"] + targets = ["bullseye-variants", "alpine-variants"] +} + +group "bullseye-variants" { + targets = ["bullseye-php-82"] +} + +group "alpine-variants" { + targets = ["alpine-php-82"] } target "common" { + context = "." platforms = ["linux/amd64", "linux/arm64"] } @@ -14,16 +23,22 @@ target "common" { # FrankenPHP # -target "bullseye" { +target "bullseye-php-82" { inherits = ["common"] - context = "." + contexts = { + php-base = "docker-image://php:8.2-zts-bullseye" + golang-base = "docker-image://golang:1.19-bullseye" + } dockerfile = "Dockerfile" tags = ["${REPO_NAME}:bullseye", "${REPO_NAME}:latest"] } -target "alpine" { +target "alpine-php-82" { inherits = ["common"] - context = "." + contexts = { + php-base = "docker-image://php:8.2-zts-alpine3.17" + golang-base = "docker-image://golang:1.19-alpine3.17" + } dockerfile = "Dockerfile.alpine" tags = ["${REPO_NAME}:alpine"] } From b287092457d20c69d293a7aa37ca0db9d365e8f1 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Sat, 14 Jan 2023 10:58:09 +0200 Subject: [PATCH 2/7] Divide builders to own dockerfiles and use one dockerfile for final images --- Dockerfile | 49 +------------------ ...erfile.alpine => builder-alpine.Dockerfile | 18 +------ builder-bullseye.Dockerfile | 46 +++++++++++++++++ docker-bake.hcl | 38 +++++++++++--- 4 files changed, 78 insertions(+), 73 deletions(-) rename Dockerfile.alpine => builder-alpine.Dockerfile (71%) create mode 100644 builder-bullseye.Dockerfile diff --git a/Dockerfile b/Dockerfile index fa8cacb5ae..f341a79137 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,51 +1,4 @@ -FROM php-base AS builder - -COPY --from=golang-base /usr/local/go/bin/go /usr/local/bin/go -COPY --from=golang-base /usr/local/go /usr/local/go - -# This is required to link the frankenPHP binary to the PHP binary -RUN apt-get update && \ - apt-get -y --no-install-recommends install \ - libargon2-dev \ - libcurl4-openssl-dev \ - libonig-dev \ - libreadline-dev \ - libsodium-dev \ - libsqlite3-dev \ - libssl-dev \ - libxml2-dev \ - zlib1g-dev \ - && \ - apt-get clean - -WORKDIR /go/src/app - -COPY go.mod go.sum ./ -RUN go mod graph | awk '{if ($1 !~ "@") print $2}' | xargs go get - -RUN mkdir caddy && cd caddy -COPY caddy/go.mod caddy/go.sum ./caddy/ - -RUN cd caddy && go mod graph | awk '{if ($1 !~ "@") print $2}' | xargs go get - -COPY *.* ./ -COPY caddy caddy -COPY C-Thread-Pool C-Thread-Pool -COPY internal internal -COPY testdata testdata - -# todo: automate this? -# see https://github.com/docker-library/php/blob/master/8.2-rc/bullseye/zts/Dockerfile#L57-L59 for php values -ENV CGO_LDFLAGS="-lssl -lcrypto -lreadline -largon2 -lcurl -lonig -lz $PHP_LDFLAGS" CGO_CFLAGS=$PHP_CFLAGS CGO_CPPFLAGS=$PHP_CPPFLAGS - -RUN cd caddy/frankenphp && \ - go build && \ - cp frankenphp /usr/local/bin && \ - cp /go/src/app/caddy/frankenphp/Caddyfile /etc/Caddyfile - -ENTRYPOINT ["/bin/bash","-c"] - -FROM php-base AS final +FROM php-base COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/ diff --git a/Dockerfile.alpine b/builder-alpine.Dockerfile similarity index 71% rename from Dockerfile.alpine rename to builder-alpine.Dockerfile index 1c548bf7ad..d4dc9afb0c 100644 --- a/Dockerfile.alpine +++ b/builder-alpine.Dockerfile @@ -1,4 +1,4 @@ -FROM php-base AS builder +FROM php-base COPY --from=golang-base /usr/local/go/bin/go /usr/local/bin/go COPY --from=golang-base /usr/local/go /usr/local/go @@ -43,19 +43,3 @@ RUN cd caddy/frankenphp && \ cp /go/src/app/caddy/frankenphp/Caddyfile /etc/Caddyfile ENTRYPOINT ["/bin/sh","-c"] - -FROM php-base AS final - -COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/ - -WORKDIR /app - -RUN mkdir -p /app/public -RUN echo ' /app/public/index.php - -COPY --from=builder /usr/local/bin/frankenphp /usr/local/bin/frankenphp -COPY --from=builder /etc/Caddyfile /etc/Caddyfile - -RUN sed -i 's/php/frankenphp run/g' /usr/local/bin/docker-php-entrypoint - -CMD [ "--config", "/etc/Caddyfile" ] diff --git a/builder-bullseye.Dockerfile b/builder-bullseye.Dockerfile new file mode 100644 index 0000000000..2d0c208c4a --- /dev/null +++ b/builder-bullseye.Dockerfile @@ -0,0 +1,46 @@ +FROM php-base + +COPY --from=golang-base /usr/local/go/bin/go /usr/local/bin/go +COPY --from=golang-base /usr/local/go /usr/local/go + +# This is required to link the frankenPHP binary to the PHP binary +RUN apt-get update && \ + apt-get -y --no-install-recommends install \ + libargon2-dev \ + libcurl4-openssl-dev \ + libonig-dev \ + libreadline-dev \ + libsodium-dev \ + libsqlite3-dev \ + libssl-dev \ + libxml2-dev \ + zlib1g-dev \ + && \ + apt-get clean + +WORKDIR /go/src/app + +COPY go.mod go.sum ./ +RUN go mod graph | awk '{if ($1 !~ "@") print $2}' | xargs go get + +RUN mkdir caddy && cd caddy +COPY caddy/go.mod caddy/go.sum ./caddy/ + +RUN cd caddy && go mod graph | awk '{if ($1 !~ "@") print $2}' | xargs go get + +COPY *.* ./ +COPY caddy caddy +COPY C-Thread-Pool C-Thread-Pool +COPY internal internal +COPY testdata testdata + +# todo: automate this? +# see https://github.com/docker-library/php/blob/master/8.2-rc/bullseye/zts/Dockerfile#L57-L59 for php values +ENV CGO_LDFLAGS="-lssl -lcrypto -lreadline -largon2 -lcurl -lonig -lz $PHP_LDFLAGS" CGO_CFLAGS=$PHP_CFLAGS CGO_CPPFLAGS=$PHP_CPPFLAGS + +RUN cd caddy/frankenphp && \ + go build && \ + cp frankenphp /usr/local/bin && \ + cp /go/src/app/caddy/frankenphp/Caddyfile /etc/Caddyfile + +ENTRYPOINT ["/bin/bash","-c"] diff --git a/docker-bake.hcl b/docker-bake.hcl index 9137de7d0b..cba2c64c75 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -19,26 +19,48 @@ target "common" { platforms = ["linux/amd64", "linux/arm64"] } +target "common-bullseye" { + contexts = { + php-base = "docker-image://php:8.2-zts-bullseye" + golang-base = "docker-image://golang:1.19-bullseye" + } +} + +target "common-alpine" { + contexts = { + php-base = "docker-image://php:8.2-zts-alpine3.17" + golang-base = "docker-image://golang:1.19-alpine3.17" + } +} + +# Builders + +target "builder-bullseye" { + inherits = ["common-bullseye"] + dockerfile = "builder-bullseye.Dockerfile" +} + +target "builder-alpine" { + inherits = ["common-alpine"] + dockerfile = "builder-alpine.Dockerfile" +} + # # FrankenPHP # target "bullseye-php-82" { - inherits = ["common"] + inherits = ["common", "common-bullseye"] contexts = { - php-base = "docker-image://php:8.2-zts-bullseye" - golang-base = "docker-image://golang:1.19-bullseye" + builder = "target:builder-bullseye" } - dockerfile = "Dockerfile" tags = ["${REPO_NAME}:bullseye", "${REPO_NAME}:latest"] } target "alpine-php-82" { - inherits = ["common"] + inherits = ["common", "common-alpine"] contexts = { - php-base = "docker-image://php:8.2-zts-alpine3.17" - golang-base = "docker-image://golang:1.19-alpine3.17" + builder = "target:builder-alpine" } - dockerfile = "Dockerfile.alpine" tags = ["${REPO_NAME}:alpine"] } From 21fdfbfcfc4cf31a6de4c0b6f2994a040e343bc4 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Sat, 14 Jan 2023 10:58:19 +0200 Subject: [PATCH 3/7] Update .dockerignore --- .dockerignore | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/.dockerignore b/.dockerignore index bf9b07fdc2..d8c526a296 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1,10 @@ -Dockerfile -Dockerfile.dev +/.git/ +/.github/ +/.gitmodules/ +/.idea/ +/.vscode/ +/docs/ +/*Dockerfile* +/.*ignore +/*.hcl +/*.md From 3bf161599ff5903fd1079b01850428e6f80c34b7 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Sat, 14 Jan 2023 10:58:57 +0200 Subject: [PATCH 4/7] Use buildx for GHA --- .github/workflows/build.yaml | 24 ++++++++ .github/workflows/push.yaml | 104 ----------------------------------- .github/workflows/tests.yaml | 94 ------------------------------- 3 files changed, 24 insertions(+), 198 deletions(-) create mode 100644 .github/workflows/build.yaml delete mode 100644 .github/workflows/push.yaml delete mode 100644 .github/workflows/tests.yaml diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000000..3419376c79 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,24 @@ +name: Build + +on: + pull_request: + branches: + - docker-bake-the-sequel + +jobs: + + build: + + runs-on: ubuntu-latest + + steps: + + - uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Build and push + uses: docker/bake-action@v2 + with: + push: false diff --git a/.github/workflows/push.yaml b/.github/workflows/push.yaml deleted file mode 100644 index 8cecbc52b2..0000000000 --- a/.github/workflows/push.yaml +++ /dev/null @@ -1,104 +0,0 @@ -name: Build and push Docker image (latest) -on: - push: - branches: - - main - tags: - - v* - workflow_dispatch: - inputs: {} -jobs: - docker-tests: - runs-on: ubuntu-latest - strategy: - matrix: - dockerfile: [ "Dockerfile", "Dockerfile.alpine" ] - steps: - - uses: actions/checkout@v3 - - - name: Set up Docker Buildx - id: buildx - uses: docker/setup-buildx-action@master - with: - install: true - - - name: Build test image - uses: docker/build-push-action@v3 - with: - context: ./ - file: ${{ matrix.dockerfile }} - push: false - pull: true - target: builder - tags: frankenphp:${{ github.sha }}-builder - builder: ${{ steps.buildx.outputs.name }} - cache-from: type=gha - cache-to: type=gha,mode=max - outputs: type=docker,dest=/tmp/.builder.tar - - - name: Run tests - run: | - docker load -i /tmp/.builder.tar - docker run --rm frankenphp:${{ github.sha }}-builder "go test -race -v" - push-image: - runs-on: ubuntu-latest - strategy: - matrix: - dockerfile: [ "Dockerfile", "Dockerfile.alpine" ] - include: - - dockerfile: Dockerfile - flavor: "" - - dockerfile: Dockerfile.alpine - flavor: "-alpine" - steps: - - uses: actions/checkout@v3 - - - name: Docker Login - uses: docker/login-action@v2 - with: - #registry: ${{secrets.REGISTRY_LOGIN_SERVER}} - username: ${{secrets.REGISTRY_USERNAME}} - password: ${{secrets.REGISTRY_PASSWORD}} - - - name: Docker meta - id: meta - uses: docker/metadata-action@v4 - with: - # list of Docker images to use as base name for tags - images: ${{secrets.IMAGE_NAME}} - flavor: | - suffix=${{matrix.flavor}} - # generate Docker tags based on the following events/attributes - tags: | - type=schedule - type=ref,event=branch - type=ref,event=pr - type=semver,pattern={{version}} - type=semver,pattern={{major}}.{{minor}} - type=semver,pattern={{major}} - type=raw,value=latest,enable={{is_default_branch}} - type=sha - - - name: Set up Docker Buildx - id: buildx - uses: docker/setup-buildx-action@master - with: - install: true - - - name: Setup QEMU - uses: docker/setup-qemu-action@v2 - - - name: Build and Push Image - uses: docker/build-push-action@v3 - with: - context: ./ - file: ${{ matrix.dockerfile }} - push: true - pull: true - target: final - platforms: linux/amd64,linux/arm64 - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} - builder: ${{ steps.buildx.outputs.name }} - cache-from: type=gha - cache-to: type=gha,mode=max diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml deleted file mode 100644 index e1519cd24c..0000000000 --- a/.github/workflows/tests.yaml +++ /dev/null @@ -1,94 +0,0 @@ -name: Tests -on: - pull_request: - branches: - - main -jobs: - docker-tests: - runs-on: ubuntu-latest - strategy: - matrix: - dockerfile: [ "Dockerfile", "Dockerfile.alpine" ] - steps: - - uses: actions/checkout@v3 - - - name: Set up Docker Buildx - id: buildx - uses: docker/setup-buildx-action@master - with: - install: true - - - name: Build test image - uses: docker/build-push-action@v3 - with: - context: ./ - file: ${{ matrix.dockerfile }} - push: false - pull: true - target: builder - tags: frankenphp:${{ github.sha }}-builder - builder: ${{ steps.buildx.outputs.name }} - cache-from: type=gha - cache-to: type=gha,mode=max - outputs: type=docker,dest=/tmp/.builder.tar - - - name: Run tests - run: | - docker load -i /tmp/.builder.tar - docker run --rm frankenphp:${{ github.sha }}-builder "go test -a -v" - push-image: - runs-on: ubuntu-latest - strategy: - matrix: - dockerfile: [ "Dockerfile", "Dockerfile.alpine" ] - include: - - dockerfile: Dockerfile - flavor: "" - - dockerfile: Dockerfile.alpine - flavor: "-alpine" - steps: - - uses: actions/checkout@v3 - - - name: Docker meta - id: meta - uses: docker/metadata-action@v4 - with: - flavor: | - suffix=${{matrix.flavor}} - # list of Docker images to use as base name for tags - images: | - frankenphp - # generate Docker tags based on the following events/attributes - tags: | - type=schedule - type=ref,event=branch - type=ref,event=pr - type=semver,pattern={{version}} - type=semver,pattern={{major}}.{{minor}} - type=semver,pattern={{major}} - type=raw,value=latest - type=sha - - - name: Set up Docker Buildx - id: buildx - uses: docker/setup-buildx-action@master - with: - install: true - - - name: Setup QEMU - uses: docker/setup-qemu-action@v2 - - - name: Build and Push Image - uses: docker/build-push-action@v3 - with: - context: ./ - file: ${{ matrix.dockerfile }} - push: false - pull: true - target: final - platforms: linux/amd64,linux/arm64 - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} - builder: ${{ steps.buildx.outputs.name }} - cache-from: type=gha - cache-to: type=gha,mode=max From 4521926e80e3270cb30bc28574fbd1311f0dd3ca Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Sat, 14 Jan 2023 11:00:04 +0200 Subject: [PATCH 5/7] run gha on this branch --- .github/workflows/build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 3419376c79..99f3978777 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -1,7 +1,7 @@ name: Build on: - pull_request: + push: branches: - docker-bake-the-sequel From 665eac5c865e583f2c2ef25763ea12f668c409a5 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Tue, 31 Jan 2023 10:14:55 +0200 Subject: [PATCH 6/7] Update builder-bullseye.Dockerfile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kévin Dunglas --- builder-bullseye.Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder-bullseye.Dockerfile b/builder-bullseye.Dockerfile index 2d0c208c4a..5cfc369c60 100644 --- a/builder-bullseye.Dockerfile +++ b/builder-bullseye.Dockerfile @@ -3,7 +3,7 @@ FROM php-base COPY --from=golang-base /usr/local/go/bin/go /usr/local/bin/go COPY --from=golang-base /usr/local/go /usr/local/go -# This is required to link the frankenPHP binary to the PHP binary +# This is required to link the FrankenPHP binary to the PHP binary RUN apt-get update && \ apt-get -y --no-install-recommends install \ libargon2-dev \ From c5b035ab716cd7d3e6ecc9677e90e8d788ca1f42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Mon, 27 Feb 2023 19:26:24 +0100 Subject: [PATCH 7/7] fix: Dockerfile version --- Dockerfile | 1 + Dockerfile.dev | 1 + builder-alpine.Dockerfile | 1 + builder-bullseye.Dockerfile | 1 + 4 files changed, 4 insertions(+) diff --git a/Dockerfile b/Dockerfile index f341a79137..437b427249 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,4 @@ +# syntax=docker/dockerfile:1 FROM php-base COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/ diff --git a/Dockerfile.dev b/Dockerfile.dev index a36a9ff895..31bc64a3a9 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -1,3 +1,4 @@ +# syntax=docker/dockerfile:1 FROM golang:1.19 ARG LIBICONV_VERSION=1.17 diff --git a/builder-alpine.Dockerfile b/builder-alpine.Dockerfile index d4dc9afb0c..cacf1d8685 100644 --- a/builder-alpine.Dockerfile +++ b/builder-alpine.Dockerfile @@ -1,3 +1,4 @@ +# syntax=docker/dockerfile:1 FROM php-base COPY --from=golang-base /usr/local/go/bin/go /usr/local/bin/go diff --git a/builder-bullseye.Dockerfile b/builder-bullseye.Dockerfile index 5cfc369c60..bf7919f30f 100644 --- a/builder-bullseye.Dockerfile +++ b/builder-bullseye.Dockerfile @@ -1,3 +1,4 @@ +# syntax=docker/dockerfile:1 FROM php-base COPY --from=golang-base /usr/local/go/bin/go /usr/local/bin/go