Laravel Schema Dump
Laravel Schema Dump is a command that allows you to dump the current database schema to a file. Here's how you can use it in your Spin Pro project.
Prerequisites
Make sure you already have a Spin Pro project with spin new
or spin init
.
Background
When we look at Laravel's documentation on php artisan schema:dump
, you can see there is discussion about how a database command-line client needs to be installed. These tools are not installed by default in serversideup/php (the base PHP image we use for Spin Pro) to minimize container size and reduce the security surface area.
Adding the command-line client can add a lot of weight to your image, so only use this process if you absolutely need it. You can learn more about the default configurations of the base PHP image below:
Learn more about serversideup/php's default configurations →
Installation
To add the command-line client to your Spin Pro project, we will need to add a line to the Dockerfile.php
file.
Dockerfile.php
############################################
# Base Image
############################################
FROM serversideup/php:8.4-fpm-nginx-alpine AS base
############################################
# Development Image
############################################
FROM base AS development
...
Looking at the file above, we can see Spin Pro is utilizing Docker Multi-Stage Builds to build your application image. This means you can choose where you want to install the command-line client.
In most cases, you'll want this for ALL targets of your image, but it is possibly to place this command under a specific target (like development
if needed) if you need to. In this example, we'll add it to the base
target so this client is available for all targets.
Dockerfile.php (updated with command-line client)
############################################
# Base Image
############################################
FROM serversideup/php:8.4-fpm-nginx-alpine AS base
# Install the command-line client
USER root # Switch to root to install the client
RUN docker-php-serversideup-dep-install-alpine mysql-client
USER www-data # Switch back to the www-data user
############################################
# Development Image
############################################
FROM base AS development
...
You can see we add RUN docker-php-serversideup-dep-install-alpine mysql-client
after the first FROM
statement. We call USER root
to switch to the root user to install the client, then call USER www-data
to switch back to the www-data user.
The docker-php-serversideup-dep-install-*
command is made available by serversideup/php and is used to install the command-line client for the given database. These commands are short cuts for updating the package list, installing the package, then cleaning up after the installation. You can also use apk
or apt-get
directly if you prefer.
Just make sure you're using the correct installation command for -alpine
or -debian
.
Choosing the correct client
You must use the client that matches your database. For example, if you are using MySQL, you must use the MySQL client. If you are using PostgreSQL, you must use the PostgreSQL client.
You must also ensure the client package name is correct (Debian vs Alpine).
Example installation commands
############################################
# Alpine
############################################
# SQLite
RUN docker-php-serversideup-dep-install-alpine sqlite
# MySQL
RUN docker-php-serversideup-dep-install-alpine mysql-client
# MariaDB
RUN docker-php-serversideup-dep-install-alpine mariadb-client
# PostgreSQL
RUN docker-php-serversideup-dep-install-alpine postgresql-client
############################################
# Debian
############################################
# SQLite
RUN docker-php-serversideup-dep-install-debian sqlite3
# MySQL
RUN docker-php-serversideup-dep-install-debian mysql-client
# MariaDB
RUN docker-php-serversideup-dep-install-debian mariadb-client
# PostgreSQL
RUN docker-php-serversideup-dep-install-debian postgresql-client
Testing your changes
Once you have these lines added, you can run a spin up --build
to test your changes and make sure the command-line client is installed and functioning as expected. Use spin exec
to run commands in your container once your container is running.