Setup

Lando

Using lando to run laravel in docker post aqui

Set up a laravel project!

First we will initialize a new laravel project.

If you already have an existing project, just go into the root of the project and run lando init and follow the guide. After that you can skip to the next section!

Go to the root of where you store your web projects. Now run the following command. Be sure to replace my-app before you run it! The name cannot contain spaces!

SITENAME=my-app; docker run --rm --interactive --tty \
  --volume $PWD:/app \
  --user $(id -u):$(id -g) \
  composer create-project laravel/laravel $SITENAME ^9.0 \
  && cd $SITENAME \
  && lando init --source cwd --webroot ./public --recipe laravel --name $SITENAME

When the command has completed, you will now have a brand new laravel project with the name you provided. In the root of the new directory you will find a file named .lando.yml. This is the config file generated by lando.

It will look something like this

name: my-app
recipe: laravel
config:
  webroot: ./public

These lines mean the following:

name: The name lando uses to reference the site. This will also be prefix for the sitename when we open it in the browser later on! recipe: Lando uses recipes to make setting up a new project really fast. Here we are using the laravel recipe config: Here we can pass config values to the recipe. We have set it to use the ./public directory to serve files

Configure the docker containers

Lets add some more items to our config so it resembles more what we normally use with laravel

name: my-app
recipe: laravel
config:
  webroot: ./public
  php: '8.0'
  via: apache #or nginx
  database: mysql #or mariadb or postgres
  cache: redis #or memcached

Here we are setting which php version to use and we are telling lando which webserver, database and cache-server to install. All the containers can be suffixed with a version by using eg. :2.0

Next set up your .env file with the correct values. If you are using postgres, the database config is a bit different. Refer to the docs for the correct values.

DB_CONNECTION=mysql
DB_HOST=database
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=laravel

REDIS_HOST=cache

Time to try it out Now it’s time to start our new app! Simply run lando start

Lando will now download the correct containers and start them all up. If all went well you should see a message looking something like this

Your app has started up correctly.
Here are some vitals:
 
 NAME            my-app
 LOCATION        /path/to/my-app
 SERVICES        appserver, database
 APPSERVER URLS  https://localhost:49548
                 http://localhost:49549
                 http://my-app.lndo.site/
                 https://my-app.lndo.site/

Your ports are probably different that above, but that is just because lando will find a random port on each run. Notice the services. These are the names of the containers we have built. The appserver is the main container used for running php!

You can now try opening one of the http:// links, and you should see the laravel welcome page!

Using laravel through lando

So the first thing we want to do is to make sure that all migrations are run. To run artisan commands we just replace php with lando, and it will run artisan inside the php container. lando artisan migrate Try out a new more artisan command to see that it actually works just as running it locally! Or you can use composer lando composer We can also run arbitrary php commands in the same way lando php -v This should give you the php version number running inside the container We can even jump right into the php container by running lando ssh You are now inside the container, and can run any command as if you were on your own computer. Type exit and his enter to get back to your own machine.

Tooling

Running migrations is something we need to do quite often, so let us make a handy shortcut for it!

Add the following to your .lando.yml file

name: my-app
recipe: laravel
config:
  webroot: ./public
  php: '8.0'
  via: apache
  database: mysql
  cache: redis
tooling: 
  migrate:
    service:
    cmd: php artisan migrate

We are adding a new command (tool) called migrate. It will be run on the service named appserver which is the one running php. The command it will run is php artisan migrate

Now try running lando migrate As you can see it works, and we can now shorten the command just a bit. Feel free to add your own commands to make your life a little easier.

You can always get a full list of available commands by running lando

Add a container for running laravel mix

In case you want to run laravel mix (or similar) we need a container for that. Add the following to your .lando.yml file

name: my-app
recipe: laravel
config:
  webroot: ./public
  php: '8.1'
  via: apache
  database: mysql
  cache: redis
services: 
  node:
    type: node:16
    scanner: false
    build:
      - npm install 
tooling:
  migrate:
    service: appserver
    cmd: php artisan migrate

Here we are telling lando to add an extra service, named node.

We want it to install node version 16. Lando will automatically try scanning the new service to see if it can connect to it using http. As we do not want this, we will disable the scanner.

Lastly we will tell it to run npm install as soon as it is has added the new service (just so we don’t forget)

Run lando rebuild -y to rebuild all the services, and add the new node service. As all the previous containers already exist and haven’t changed, it should be pretty fast.

Like we did with the migrate command earlier we will now also add a command for running npm Add the following to your .lando.yml file under the tooling: section

name: my-app
recipe: laravel
config:
  webroot: ./public
  php: '8.1'
  via: apache
  database: mysql
  cache: redis
services:
  node:
    type: node:18
    scanner: false
    build:
      - npm install
tooling: 
  migrate:
    service: appserver
    cmd: php artisan migrate
  npm: 
    service: node
    cmd: npm

Now run lando npm run dev to confirm that it’s working

Add mailhog to catch mails

Let us also add mailhog, to make sure we don’t accidentially send emails to our clients

First add the service

name: my-app
recipe: laravel
config:
  webroot: ./public
  php: '8.1'
  via: apache
  database: mysql
  cache: redis
services: 
  node:
    type: node:18
    scanner: false
    build:
      - npm install
  mail: 
    type: mailhog
    portforward: true
    hogfrom:
      - appserver 
tooling:
  migrate:
    service: appserver
    cmd: php artisan migrate
  npm:
    service: node
    cmd: npm

Here we add the mailhog service with the name mail. We want to let the scanner check this service as it actually has a webinterface, and we tell lando to make sure that all http requests are directed to the container. We also tell mailhog that it should grab mails from the appserver container.

Run lando rebuild -y to start the service.

Once the service is started, lando will show you an url on which you can see the mailhog interface. Here all emails from laravel will end up.

To make sure it works, change your .env file to use the new mail service

MAIL_DRIVER=smtp
MAIL_HOST=mail
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null

Now when we send emails from laravel, they will be caught by mailhog.

Proxy

Before we wrap things up lets just add a proxy to the mailhog service, to make using it a bit easier.

Add the following to the bottom of your ## .lando.yml file (replace my-app with your actual project name)

name: my-app
recipe: laravel
config:
  webroot: ./public
  php: '8.1'
  via: apache
  database: mysql
  cache: redis
services:
  node:
    type: node:18
    scanner: false
    build:
      - npm install
  mail:
    type: mailhog
    portforward: true
    hogfrom:
      - appserver
tooling:
  migrate:
    service: appserver
    cmd: php artisan migrate
  npm:
    service: node
    cmd: npm
proxy: 
  mail:
    - mail.my-app.lndo.site

Run lando rebuild -y and when it is done you will see that lando now have assigned the the mail service to the provided url. This makes opening it in the future alot easier, as we can bookmark the url.

Wrap-up

We have now successfully set up a new laravel app using lando. I hope it was easy to follow and that you will be using lando in the future.

Resumen my-app en mi maquina

.lando.yml

name: my-app
recipe: laravel
config:
  webroot: ./public
  php: '8.1'
  via: apache
  database: mysql
  cache: redis

services:
  node:
    type: node:18
    scanner: false
    ports:
      - 3009:3009
    build:
      - npm install
  mail:
    type: mailhog
    portforward: true
    hogfrom:
      - appserver

tooling:
  migrate:
    service: appserver
    cmd: php artisan migrate
  npm:
    service: node
    cmd: npm
  dev:
    service: node
    cmd: npm run dev
  build:
    service: node
    cmd: npm run build

proxy:
  mail:
    - mail.my-app.lndo.site

.env

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:y0NzautyOL6tUnfMPpa/SwgtUUSzaGI2FSXqlmiMIrY=
APP_DEBUG=true
APP_URL=http://my-app.lndo.site

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=database
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=laravel

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=cache
REDIS_PASSWORD=null
REDIS_PORT=6379

# MAIL_MAILER=smtp
# MAIL_HOST=mailpit
# MAIL_PORT=1025
# MAIL_USERNAME=null
# MAIL_PASSWORD=null
# MAIL_ENCRYPTION=null
# MAIL_FROM_ADDRESS="hello@example.com"
# MAIL_FROM_NAME="${APP_NAME}"

# mailhog
MAIL_DRIVER=smtp
MAIL_HOST=mail
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1

VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

Crear una nueva app con JetStream

Ir a la raiz de los proyectos de laravel con lando: cd /home/enrique/laravel/lando/EnriqueSousa

Correr el comando de linea:

SITENAME=my-appjet; docker run --rm --interactive --tty \
  --volume $PWD:/app \
  --user $(id -u):$(id -g) \
  composer create-project laravel/laravel $SITENAME ^9.0 \
  && cd $SITENAME \
  && lando init --source cwd --webroot ./public --recipe laravel --name $SITENAME

Con esto no instala jetstream, pero una vez instalada la app, podemos instalar: in laravel/installer its easy laravel new project --jet

but with composer lando composer require laravel/jetstream // then lando php artisan jetstream:install livewire // or php artisan jetstream:install inertia –teams

lando npm install && lando npm run dev

and for migrations just run this: lando php artisan migrate

Lando Laravel Quick Start.md

Para que funcione correctamente css con VITE colocar el siguiente codigo en vite.config.js asi queda el archivo:

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: ['resources/css/app.css', 'resources/js/app.js'],
            refresh: true,
        }),
    ],
    server: {
        https: false,
        host: true,
        port: 3009,
        hmr: {host: 'localhost', protocol: 'ws'},
    },
});

ahora lando stop y lando start y correr: lando migrate lando dev Listo! Ya podemos apreciar el css en la forma de jetstream de login y register!

Cleaning up Lando containers

liga

Depending on how many containers and projects you may have with Lando / Docker, things can go wrong at some time on your local environment, so here is a basic troubleshooting.

First aid Update first Lando and Docker, not a bad idea to start with this. Run lando destroy on unused / stale projects. Keep in mind that when you are working on several projects in the same time, the containers port must be different, so try to use different ports for your services (MaiHog, Solr, …).

Kill ’em all Sometimes, it will not be enough and it may be a good idea to restart from scratch. List all containers (even the ones that are not running) docker ps --all Kill all running containers docker kill $(docker ps -q) Delete all stopped containers docker rm $(docker ps -a -q) Delete all images docker rmi $(docker images -q)