From Homestead to Docker on existing Laravel 7.x Project

Kidd Tang
4 min readAug 1, 2021

I decided to touch up my web portal for Laravel 8 updates and running on Docker (with Laravel Sail).

Are you going to upgrade your Laravel or moving to Laravel Sail in your dev environment?
Read my journey for upgrading my portal…

Step 1 — Update your packages & Resolve Dependency Issues

Just make everything updated before going to update the core.

composer update

If you see the message: Package fzaninotto/faker is abandoned. You can quickly swap the replacement.

composer remove fzaninotto/faker

Then install

composer require fakerphp/faker --dev

Also, try to clear those packages that require an old PHP version if you found it in composer.json.

Step 2 — Read the Official Upgrade Guide

Laravel always provides a detailed upgrade guide for you to upgrade your Laravel, read all sections, and apply the changes accordingly.

# PHP 7.3.0 Required
✨# Updating Dependencies

I do not use Horizon / Passport / Socialite / Telescope in my project, so it is pretty clean and easy to upgrade.

Highlighted the changes of composer.json

#Collections — #The isset Method

Search the app folder and found no related codes.

# Database — #Seeder & Factory Namespaces

You have to do the following according to the documentation:

  • Add namespace namespace Database\Seeders; for all seeder files
  • Rename the seeder folder to database/seeders
  • Add namespace namespace Database\Factories; for all factory files
  • update the composer.json autoload section
List of file changes

#Eloquent — #Model Factories, #The Castable Interface,
#
Increment / Decrement Events

I don’t use Model Factory and the features above, it can be skipped.

#Framework — #Maintenance Mode Updates

I update the public/index.php according to the guide with the reference of public/index.php of Laravel 8.x Github.

#Framework — #The php artisan down --message Option,
#The php artisan serve — no-reload Option,
#
Manager $app Property,
#
The elixir Helper

The rest of the notes are not relevant to my current project.

✨# Routing — #Automatic Controller Namespace Prefixing

This is an optional change, I will leave it unchanged.

#Events, ✨#Mail, ✨#Pagination, ✨#Queue, ✨#Scheduling, ✨#Session, ✨#Testing, ✨#Validation

These notes in the upgrade guide are not relevant to my current project.

✨#Miscellaneous

You can check the comparison of 7.x and 8.x and sync anything if necessary.

Step 3 — Execute your Test

This is a tiny single-page portal and yet to write any test, I will skip this. If you wrote the Tests, you need to run and fix the issues.

Step 4 — Install Laravel Sail

The application is now ready to migrate to the Laravel Sail, install it with

composer require laravel/sail --devphp artisan sail:install
choose the services you need

You may refer to Laravel official documentation to learn more.

Step 5 — Push & Pull from GitLab

Use your favorite tools to push to the cloud-based repo and pull on the WSL user’s directory.

Step 6 — Update .env file

.env is not stored in the Git, copy and update the parameters accordingly.
The database username and password have to be updated.

DB_CONNECTION=mysql
DB_HOST=mariadb
DB_PORT=3306
DB_DATABASE=dogcomp_db
DB_USERNAME=sail
DB_PASSWORD=password

Note: if you are using MySQL, replace the mariadb with mysql

Step 7— Prepare to Sail Up

The WSL2 Ubuntu has no composer for us to populate the packages. So we can use this image to do the task for us.

First, you have to go up one level from your project folder, then execute the command below (replace the project folder accordingly)

docker run --rm -v $(pwd):/opt -w /opt laravelsail/php80-composer:latest bash -c "cd <your project folder> && composer install"

Step 8 — Sail Up

Everything should be ready! You can execute sail up -d (if you have done the set up of the Alias)

Step 9 — Restore DB

If you want to restore the Database, you can open the CLI from Docker Desktop

Temporary put your sql file in the project root, then execute

mysql -h mariadb -u sail -p <your db_name> < /var/www/html/<your sql file>.sql

Input the password, then it should be able to restore.

Note: if you are using MySQL, replace the mariadb with mysql

If you like my article, please clap, follow and share my articles (or you could ☕ buy me a coffee ☕ ). Take care and see you soon!

--

--

Kidd Tang

Full-stack web developer with electronic engineering background.