Laravel Docker image - NPM & Vite


I’d like to share a couple issues I’ve found with the Dockerfile generated for Laravel projects with fly launch.
The first one I’ve already reported on GitHub with a pull request (Laravel - update Node version to 18 and fixes error during build when running `npm ci` by lorenzocattaneo · Pull Request #2016 · superfly/flyctl · GitHub). I’m posting this here because I’m not sure if this image repo should also reflect the same change.

The other one is not really a bug but more of a documentation/DX issue. The easiest way to reproduce it is to generate a new Laravel project, install the breeze vue starter kit, and set up pusher. Then fly launch and deploy.

When running a project with Vite + Pusher, Laravel expects to find env vars prefixed with VITE_ for each required value. Those are already present in the default .env when generating a new project.

However, when building with fly deploy, or even locally with docker build ., .env and all .env.* files are ignored due to .dockerignore. Env variables declared in fly.toml are also not available at build time. The build is successful, but the app then presents a white screen and in console there’s an error showing pusher needs an app key.

Personally I can think of 2 solutions:

  • adding in the Dockerfile one ARG for each of these variables, empty by default. Then it should be enough to set these values in fly.toml under the [build.args] section, which is what I’ve done in my own project
  • don’t make any changes, add a page to the fly docs in the Laravel section about using Vite that details what to change to make it work. I’d be happy to contribute this doc if this is the preferred solution

If anyone has a better/alternative solution that would also be great.

Thanks, that all makes sense!

The base image repo doesn’t do anything with Node (instead Node is used later as part of a multi-stage build - it pulls in the official node Docker image based on the version selected).

The DX issue is definitely a THING for us to think on! Needing those env vars at build time is indeed annoying.

One idea we have cooking is an artisan (or other CLI command) to help generate Fly Dockerfiles, which may be able to help with issues like this (Pulling in .env items for example).

I’ll see what we can come up with!

1 Like