Distributing Laravel Zero apps with Docker

Published in Laravel Zero, PHP on Feb 4, 2023

I've recently started distributing my Laravel Zero applications with Docker. Laravel Zero already supports generating
Phar files using Box, and Box supports generating a Dockerfile from a Phar.

In Laravel Zero, this is as easy as running the following command:

vendor/laravel-zero/framework/bin/box docker [builds/application]

The generated Dockerfile will look like this:

FROM php:8.0-cli-alpine

COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/

RUN install-php-extensions zlib

COPY builds/application /application

ENTRYPOINT ["/application"]

This allows you to install any required dependencies as part of the run block, for example if you required the OAuth and GD extensions:

RUN docker-php-ext-install zlib oauth gd

Note: The zlib extension is required for GZ compression with Box, if you are using this.

Once this has been generated, you can try building your Docker image with:

docker build -t application .

If everything works as expected, you can try running your built image with Docker using:

docker run --rm application

I personally like to store my built Docker images on GitHub Container Repository (GHCR). If you want to set this up, you
can use this GitHub Action workflow.

name: Docker

on:
    push:
        tags:
            - 'v*'

jobs:
    build:
        runs-on: ubuntu-latest
        permissions:
            contents: read
            packages: write
        name: Build
        steps:

            -   name: Checkout
                uses: actions/checkout@v3

            -   name: Login to Docker Hub
                uses: docker/login-action@v2
                with:
                    registry: ghcr.io
                    username: ${{ github.repository_owner }}
                    password: ${{ secrets.GITHUB_TOKEN }}

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

            -   name: Extract metadata (tags, labels) for Docker
                id: meta
                uses: docker/metadata-action@v4
                with:
                    images: ghcr.io/${{ github.repository }}
                    tags: |
                        type=semver,pattern={{version}}
                        type=semver,pattern={{major}}

            -   name: Build and push
                uses: docker/build-push-action@v4
                with:
                    context: .
                    push: true
                    platforms: linux/amd64,linux/arm64
                    tags: ${{ steps.meta.outputs.tags }}
                    labels: ${{ steps.meta.outputs.labels }}

For other ways to distribute your Laravel Zero applications, check out my blog posts
on Distributing Laravel Zero apps with Windows package managers,
and Distributing Laravel Zero apps with Brew.

1
2
0
2