aschmelyun / docker-compose-laravel

A docker-compose workflow for local Laravel development
https://youtube.com/user/aschmelyun
MIT License
2.59k stars 1.16k forks source link

docker-compose-laravel

A pretty simplified Docker Compose workflow that sets up a LEMP network of containers for local Laravel development. You can view the full article that inspired this repo here.

Usage

To get started, make sure you have Docker installed on your system, and then clone this repository.

Next, navigate in your terminal to the directory you cloned this, and spin up the containers for the web server by running docker-compose up -d --build app.

After that completes, follow the steps from the src/README.md file to get your Laravel project added in (or create a new blank one).

Note: Your MySQL database host name should be mysql, not localhost. The username and database should both be homestead with a password of secret.

Bringing up the Docker Compose network with app instead of just using up, ensures that only our site's containers are brought up at the start, instead of all of the command containers as well. The following are built for our web server, with their exposed ports detailed:

Three additional containers are included that handle Composer, NPM, and Artisan commands without having to have these platforms installed on your local computer. Use the following command examples from your project root, modifying them to fit your particular use case.

Permissions Issues

If you encounter any issues with filesystem permissions while visiting your application or running a container command, try completing one of the sets of steps below.

If you are using your server or local environment as the root user:

If you are using your server or local environment as a user that is not root:

Then, either bring back up your container network or re-run the command you were trying before, and see if that fixes it.

Persistent MySQL Storage

By default, whenever you bring down the Docker network, your MySQL data will be removed after the containers are destroyed. If you would like to have persistent data that remains after bringing containers down and back up, do the following:

  1. Create a mysql folder in the project root, alongside the nginx and src folders.
  2. Under the mysql service in your docker-compose.yml file, add the following lines:
volumes:
  - ./mysql:/var/lib/mysql

Usage in Production

While I originally created this template for local development, it's robust enough to be used in basic Laravel application deployments. The biggest recommendation would be to ensure that HTTPS is enabled by making additions to the nginx/default.conf file and utilizing something like Let's Encrypt to produce an SSL certificate.

Compiling Assets

This configuration should be able to compile assets with both laravel mix and vite. In order to get started, you first need to add --host 0.0.0.0 after the end of your relevant dev command in package.json. So for example, with a Laravel project using Vite, you should see:

"scripts": {
  "dev": "vite --host 0.0.0.0",
  "build": "vite build"
},

Then, run the following commands to install your dependencies and start the dev server:

After that, you should be able to use @vite directives to enable hot-module reloading on your local Laravel application.

Want to build for production? Simply run docker-compose run --rm npm run build.

MailHog

The current version of Laravel (9 as of today) uses MailHog as the default application for testing email sending and general SMTP work during local development. Using the provided Docker Hub image, getting an instance set up and ready is simple and straight-forward. The service is included in the docker-compose.yml file, and spins up alongside the webserver and database services.

To see the dashboard and view any emails coming through the system, visit localhost:8025 after running docker-compose up -d site.