Giter VIP home page Giter VIP logo

dockerfile-laravel's Introduction

Dockerfile-Laravel

Provides a PHP generator to produce Dockerfiles and related files. It's target is mostly Laravel, but should work for most PHP applications.

Usage

This project provides a CLI application that generates a Dockerfile for your Laravel applications!

Current Features

The Dockerfile generated by this project aims to provide support for:

  1. Running your app by default in an Nginx-flavored web server.
  2. Or, running your app in an Octane-flavored web server: FrankenPHP, RoadRunner, Swoole.
  3. Building static assets using Vite or Mix
  4. Running the Scheduler for your background tasks
  5. Generating Fly.io relevant scripts essential for deploying your application as a Laravel Fly App, if applicable

The Dockerfile generated uses the base image published from https://github.com/fly-apps/laravel-docker/tree/main. Every feature from the Docker image published from that repository, is of course, by nature of things-passed-over to a dependent, also available in the Dockerfile generated by this project ( unless overridden ).

Installation

Local

Run composer require fly-apps/dockerfile-laravel --dev in the Laravel project's base directory. After a successful installation, you should now have the binary available at your project's bin directory: vendor/bin/dockerfile-laravel.

Global

Run composer global require fly-apps/dockerfile-laravel. After a successful installation, you should now have the binary available at your composer's, bin directory: <path-to-composer-home>/vendor/bin/dockerfile-laravel. You can view the path to your composer's home directory by checking with composer -n config --global home.

Aliasing

Once dockerfile-laravel has successfully been installed, you can create an alias for it by running: alias dockerfile-laravel='/path-to/vendor/bin/dockerfile-laravel'. Aliasing will allow you to run dockerfile-laravel instead of including the full path to your dockerfile-laravel installation.

If you want to make this package available even outside of your current directory, you can add it to your shell configuration file in your home directory, such as ~/.zshrc or ~/.bashrc, like so:

alias dockerfile-laravel="/path-to/vendor/bin/dockerfile-laravel"

Restart your terminal afterwards, and you should be able to access the dockerfile-laravel globally, thanks to this alias saved in your shell configuration.

Generating the Dockerfile

Now that you have the dockerfile-laravel installed, you can use it to generate a Dockerfile for your Laravel application. Simply run the generate command of the package:

# Locally run:
vendor/bin/dockerfile-laravel generate

# or, with Alias configured:
dockerfile-laravel generate

And that should generate a Dockerfile at the current directory the command was run in.

Options

  • -o, --octane[=OCTANE] - If using Octane, provide which flavor - one of: roadrunner, swoole, frankenphp
  • --no-assets - Skip compiling static assets
  • --force - Overwrite existing files
  • --skip - Keep existing files
  • --dev - Include dev dependencies in the creation of the image, i.e. the local .env file

The dockerfile generator accepts the following options:

Testing out the Dockerfile

With your Dockerfile generated, you should now be able to build this Dockerfile into a Docker image:

docker build -t <image-name> . 

You can run an instance of this image afterwards:

docker run -p <port-in-your-machine>:8080 <image-name>

And access a containerized instance of your Laravel app in the browser by visiting 127.0.0.1:<port-in-your-machine>!

Local Development

Feeling a little adventurous, and want to make some experiments with the package? You can do some local development if you want.

Get Familiar with These

Of course, the age-old-question when starting with any new repository: where does one start? First, get familiar with the VIP files of this project:

  1. app/Commands/GenerateCommand.php

    • This is the entrypoint to the application. This is in charge of generating a Dockerfile in a project's base directory based on the dockerfile.blade.php template, and the flags passed to it.
  2. resources/views/dockerfile.blade.php

    • This is the template used to generate a Dockerfile

Finish Set Up

Before diving into making local development changes, there're some to-do's to get you setup with local development on the package:

  1. Clone the repository
  2. Get the repo dependencies with composer install
  3. And, just in case you ever find an error about files not being available, you might be able to fix this with providing the proper permission to the box package used by Laravel Zero framework: chmod 755 vendor/laravel-zero/framework/bin/box.

Then, yes. After set up, you can make your changes!

Testing Changes

Once you've made your changes, you can test them locally by running php dockerfile-laravel generate. This will call the command found in app/Commands/GenerateCommand.php.

Building Changes

Great! You've tested your changes locally. It's time to re-build the stand-alone application for the repository, so that your changes get included in the standalone application that is known as dockerfile-laravel.

Simply run the build command:

php dockerfile-laravel app:build

Build View Changes

If changes were made in any of the view files, make sure to clear the cached views( For now, this can be done by manually deleting the cached files ). The path to where the views' caches are stored is configured in config/view.php.

Remember, if you don't delete cached views, your fresh "view changes" are unlikely to be included in the features of the built application.

So, if your view changes don't seem to be working, do delete any cached view files you find, then re-build your changes.

Testing the newly-Built Application

Once you've successfully built your changes, you have to test how it turned out. Simply, in the base of any Laravel project of your choosing run the following:

<path-to-dockerfile-laravel-dir>/builds/dockerfile-laravel generate

And that's it! You should now see a fresh Dockerfile available for your project.

Test Cases

There are two general purpose test cases in tests/Feature/GenerateCommandTest.php:

  1. generates proper templates for each supported base
  • Tests that generated files match their references found in the tests/Feature/Supported folder.
  • Each sub folder in this folder contains:
    • Configuration files like composer.json, or other binaries, used by the generate command to detect and determine how files are generated
    • Reference files are files containing the expected contents of files generated from the generate command, i.e., a Dockerfile
  1. generates templates with proper snippets
  • Tests that specific "snippets" are properly included in specific generated files for special occassions/package support. These snippets expectations are found in tests/Feature/Snippets folder.
  • Each sub folder in this folder contains:
    • Configuration file: their respective composer.json file. Which, when combined with the composer.json files of base sub folders found in tests/Feature/Supported folder, should cause specific generated files to contain additional "snippets". Their composer.json file contains an additional custom key: extra.templates that lets the test know which specific generated files to test
    • Reference files: files that match the names found in extra.templates, and contain the actual snippet expected to be added in that specific template that is generated.
  • Each sub folder in this folder are combined and tested with all sub folders under tests/Feature/Supported. This is because their identified snippets are expected to work with all the "base" configurations found in the Supported subfolder.

These test cases are used to make sure new changes would not break existing features of the generate command. These can be run through ./vendor/bin/pest.

Contribution

Once you've cooked up and tested your local changes, you can make a Pull Request to request its inclusion in this repository. ( BONUS points if you add a test case for your change! )

Of course, you don't have to create a PR. If there are any feature or bug that makes sense for the repository, you can also create an Issue about it.

Once submitted, the Laravel team at Fly.io will review your PR/Issue and decide whether it can be successfully added into the official repository. Your contribution will be much appreciated---thank you!

dockerfile-laravel's People

Contributors

ktanaug21 avatar rubys avatar fideloper avatar heyjorgedev avatar

Stargazers

Katie Schilling avatar  avatar Ariel dos Santos Nunes avatar Besfort Oruci avatar  avatar Thomas Off avatar Miftah Shidiq avatar Kenneth Ferguson avatar Alex Justesen avatar Alessandro Gambin da Silva avatar No name avatar Siavash Bamshadnia avatar Ash avatar Shinan avatar Aleks avatar

Watchers

Thomas <b>H. Ptacek" avatar Chris Smith avatar  avatar Jeremy Jacob Anderson avatar  avatar

Forkers

heyjorgedev

dockerfile-laravel's Issues

Support for FilamentPHP

What and Why:
What: Run Filament Cache command when Filament is installed for a Laravel application

Why: Thanks to community post, it was discovered that Laravel apps with Filament installed get really low requests per second( I tested, and got 5 requests per second ). This can be improved by running Filament's cache commands.

How:
Revise Laravel's Dockerfile template to run the Filament cache commands when "filament/filament" is detected in the composer.json file

Related to:
flyctl PR
Community post

SetUp Github Actions to Run for changes pushed to main

Build a new binary of the package for production when new changes are pushed to the repository.

Also, look at this from packagist:

This package is not auto-updated. Please set up the [GitHub Hook](https://packagist.org/about#how-to-update-packages) for Packagist so that it gets updated whenever you push!

Publish as a Package in Packagist.org

We need this package to be installable via composer. This can be done by publishing it in https://packagist.org.
Before publishing please make sure to:

Create a Self-contained Self-executable binary out of the binary built

What:
Create a dockerfile-laravel binary that is standalone, and executable by itself

Why:
There are cases when the dockerfile-laravel package is not installable via composer.
In this cases we would want to download the binary instead.

Right now the phar file created by Laravel Zero relies on the environment it is run on to contain a suitable PHP interpreter.

HOW:
This is why we should try creating a standalone binary of our dockerfile-laravel generator through frankenphp's standalone binary creation

Detect Octane Flavor

What:
https://flyio.slack.com/archives/C042W39VAMB/p1710787888107569
Detect flavors of octane:

  1. FrankenPHP
    > Presence of frankenphp file in base directory
  2. RoadRunner
    > .rr dir in base directory
    > keywords in composer.json
  3. Swoole
    > This is actually a default option in the dockerfile generatedby flyctl for laravel apps with octane
    maybe
    > Can this be detectable if the swoole extension is available in the current dev environment? So check php.ini?

Why:
https://flyio.slack.com/archives/C042W39VAMB/p1711553910059719?thread_ts=1711549635.827029&cid=C042W39VAMB

TLDR: detectables should be defaultables.

Support for Generating .fly directory from dockerfile-laravel?

Should the .fly directory, which contains the /scripts folder that runs commands for caching, etc, be generated from the dockerfile-laravel command?

No:

  1. The name of the repository is dockerfile-laravel, from the name, it is assumed that the command generates dockerfile-related stuff. Not fly scripts

Yes:

  1. The dockerfile-laravel is already used by flyctl to generate the Dockerfile, it makes sense for it to generate other supporting scripts as well, like those found in the .fly directory
  2. Although the .fly scripts are not Dockerfile variations, these scripts contain startup commands the Dockerfile use for deploying a Laravel app to Fly.io. So since these are used by the Dockerfile( to setup a Laravel app for production ), we can say that these scripts are "deploy-to-fly-production" scripts, hence an indirect part of a Dockerfile the generated.

Detect Laravel Version

What:
Detect the Laravel Version of a Laravel project.

How:
Check the contents of the composer.json file, or the result of php artisan --version

Why:
We can generate Dockerfile content to accommodate to different Laravel versions

Exceptions only showing up as "unknown error" on fly.io

please let me know if this should rather be posted in the community forum.

If i trigger an exception all i see in the fly logs is: ERROR unknown error

more precisely:
Screenshot 2024-04-23 at 11 57 45

no details about it.
i tested this locally with frankenphp and stderr and did see the exception in frankenphp's log

logging.php

'stderr' => [
    'driver' => 'monolog',
    'level' => env('LOG_LEVEL', 'debug'),
    'handler' => StreamHandler::class,
    'formatter' => env('LOG_STDERR_FORMATTER'),
    'formatter_with' => [
        'includeStacktraces' => true,
    ],
    'with' => [
        'stream' => 'php://stderr',
    ],
    'processors' => [PsrLogMessageProcessor::class],
],

fly.toml

[build]
  [build.args]
    NODE_VERSION = '18'
    PHP_VERSION = '8.3'

[env]
    APP_ENV = 'production'
    LOG_CHANNEL = 'stderr'
    LOG_LEVEL = 'info'
    LOG_STDERR_FORMATTER = 'Monolog\Formatter\JsonFormatter'
    SESSION_DRIVER = 'cookie'
    SESSION_SECURE_COOKIE = 'true'


[http_service]
  internal_port = 8000
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ['app']

[processes]
  app = ""
#  cron = "cron -f"
#  worker = "php artisan queue:listen"

[[vm]]
  size = 'shared-cpu-1x'
  memory = "512MB"

Changes to view files are not reflected in built command

Changes in view files are not included in the built phar file after building changes. Using the storage/framework/views for storing view cache can make these cached views visible, deletable, and once deleted, allow for new changes to be reflected on the built package/application.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.