Skip to main content
This guide walks you through building and deploying a Laravel application with MariaDB to Magic Containers with GitHub Container Registry. You’ll need:
  • A GitHub account for source code and container registry
  • A bunny.net account with Magic Containers enabled

Create the Laravel app

Create a new Laravel project:
Update your .env to use MariaDB:
.env
Use 127.0.0.1 instead of localhost for DB_HOST. Magic Containers share a localhost network between containers, but PHP/PDO interprets localhost as a Unix socket connection which will fail. Using 127.0.0.1 forces a TCP connection.

Create the Nginx config

Create docker/nginx.conf:
docker/nginx.conf

Create the supervisord config

The app container runs both PHP-FPM and Nginx using supervisord. Create docker/supervisord.conf:
docker/supervisord.conf

Create the entrypoint script

Create docker/entrypoint.sh:
docker/entrypoint.sh
The entrypoint creates an empty .env file so Laravel reads configuration from the container’s environment variables instead of a file. Config caching happens at startup (not at build time) so it picks up the runtime environment. The script also waits for MariaDB to be ready before running migrations.

Create the Dockerfile

Dockerfile
Do not run php artisan config:cache in the Dockerfile. Caching config at build time bakes in the build environment’s values, which won’t match the runtime environment variables set in Magic Containers. The entrypoint script handles config caching at startup instead.
Create a .dockerignore to keep the .env file out of the image:
.dockerignore

Create the docker-compose file

Create docker-compose.yml for local development:
docker-compose.yml
The docker-compose.yml uses DB_HOST=db (the service name) for local development. In Magic Containers, the bunny.json sets DB_HOST=127.0.0.1 since containers share the same localhost network.

Run locally

Visit http://localhost:8000.

Generate an app key

Generate a key to use in production:
Keep this value for the next step.

Build and push to GitHub Container Registry

Create .github/workflows/deploy.yml to automatically build and push on every commit to main:
.github/workflows/build.yml
Push your code to trigger the workflow:
If your package is private, set the visibility to Public in GitHub or configure Magic Containers with registry credentials.

Deploy to Magic Containers

1

Create a new app

In the bunny.net dashboard, go to Magic Containers and click Add App. Enter a name and select your deployment option.
2

Add a container

Click Add Container, then configure:
3

Add an endpoint

Go to the Endpoints tab, click Add New Endpoint, and set the container port to 80.
4

Deploy

Click Add Container, then Next Step, and Confirm and Create.
For more details, see the quickstart guide. When configuring the app, add two containers:

App container

  • Image: ghcr.io/<your-username>/app-laravel:latest
  • Endpoint: port 80
  • Environment variables:
    • APP_ENV = production
    • APP_DEBUG = false
    • APP_KEY = the key from php artisan key:generate --show
    • DB_CONNECTION = mariadb
    • DB_HOST = 127.0.0.1
    • DB_PORT = 3306
    • DB_DATABASE = laravel
    • DB_USERNAME = laravel
    • DB_PASSWORD = laravel

Database container

  • Image: mariadb:11
  • Volume: /var/lib/mysql
  • Environment variables:
    • MARIADB_DATABASE = laravel
    • MARIADB_USER = laravel
    • MARIADB_PASSWORD = laravel
    • MARIADB_ROOT_PASSWORD = root

Test your app

You can add a custom hostname from the Endpoints section in your app settings.

Continuous deployment

The workflow automatically deploys to Magic Containers on every push to main. Configure the following in your repository settings:
  • Variable APP_ID - your Magic Containers app ID
  • Secret BUNNYNET_API_KEY - your bunny.net API key

Key differences for Magic Containers

When deploying Laravel to Magic Containers, there are a few important things to keep in mind:

Next steps

  1. Automate deploys with GitHub Actions
  2. Add a custom hostname
  3. Add a persistent volume