Skip to content

Running the Application with Docker and Laravel Sail

Azizul Hakim edited this page Oct 20, 2023 · 2 revisions

1.Setup Docker

First, make sure you have Docker installed on your local machine. We will be using Laravel Sail to manage our Docker operations.

If you're using Windows and facing difficulties setting up Docker, WSL2, and Laravel Sail, here are some helpful resources:

Additionally, you can refer to the official Laravel Sail Documentation for more information.

2.Navigate to the Project Directory:

Move to the project directory using the following command:

Go to the project directory

cd your-project

3.Copy .env.example to .env:

Before proceeding, copy the .env.example file to .env to set up your environment variables:

cp .env.example .env

Update the MySQL credentials as follows in your .env file:

DB_USERNAME=sail
DB_PASSWORD=password

Also, update the phpunit.xml file and add a testing database:

<env name="DB_DATABASE" value="testing"/>

4.Install Local Development Packages: To install local development packages, including Husky and other Laravel-specific packages, run the following commands:

npm install #for husky and other Laravel packages
npx husky install #only once

To install Composer packages needed for CS Fixer to run independently outside of the Docker shell, run:

composer install

5.Start The Application Container:

start you container by running

./vendor/bin/sail up
# or if you have an alias configured:
sail up

You can find your application is running at http://localhost:80

6.Running Migration and Seeder To initialize the database and start with some default data, you can run migrations and seeders using the following commands:

Step 1: Migrate the Database

The sail artisan migrate command is used to create database tables based on your application's migration files. Run the following command:

sail artisan migrate
# if no alias configured
./vendor/bin/sail artisan migrate

Step 2: Seed the Database To populate the database with default data, including users or initial records, you can use seeders. Use the php artisan db:seed command to run seeders:

sail artisan db:seed
# if no alias configured
./vendor/bin/sail db:seed

Step 3: Migrate and Seed in One Command To both migrate the database and seed it in a single command, you can use:

sail artisan migrate --seed
# if no alias configured
./vendor/bin/sail artisan migrate --seed

This command combines the migration and seeding steps, making it convenient for initial setup.

7.Run App health Check: After starting the app, it's essential to verify its health by performing the following steps:

Open your web browser or use a tool like curl to access the health check endpoint:

http://127.0.0.1/api/healthz

Upon hitting the health check endpoint, the app should respond with a JSON object similar to the following:

{
    "cache": true,
    "http": true,
    "storage": true,
    "database": true,
    "migration": true
}

If you receive a response like this, congratulations! Your app is healthy and functioning correctly.

Verifying the health of your application is an essential step to ensure that all components and services are running as expected. This check can help you identify and resolve issues promptly.

Remember to perform this health check regularly, especially after making significant changes to your application or its environment.

8.Stop Appliction Container: stop you container by running

./vendor/bin/sail down
# or if you have an alias configured:
sail down
Clone this wiki locally