Install Laravel Docker

Configuration Installation Laravel

Before we create a new Laravel application on your Windows machine, make sure to install Docker Desktop. Next, you should ensure that Windows Subsystem for Linux 2 (WSL2) is installed and enabled. Next, you should ensure that Windows Subsystem for Linux 2 (WSL2) is installed and enabled.

2 changes to make your life easier are. Install the docker plugin, which will allow you to manage containers and images in your IDE, and set your default terminal to bash. Create a folder with your desired project name. Md laravel-dockerized cd laravel-dockerized. Lets clone latest Laravel version into current directory. May 21, 2021 In this case, our Laravel application will be created in the /var/www/laraveldocker directory of the php-apache container. However, it will be persisted in the src directory in the project directory on the local development machine. Install Docker for your OS, clone this repo, add your Laravel app files to the src directory, and from the root project directory you just cloned run: docker-compose build && docker-compose up -d. As you have guessed, in this article we are talking about how to install Laravel in docker. Of course, this assumes you have some background knowledge of both technologies. This post is for people who already have a Laravel application, or that are working on it, and would like to install it in docker.

24 Feb, 2021 4 min read
Share

Welcome to this tutorial, today we are going to create a Laravel application with the new Laravel Sail package.

What is Laravel Sail?

In the previous versions of Laravel you had to install software on your local computer for development. Laravel 8.x comes with Laravel Sail. This package is a build in solution to run your Laravel project using docker.

Docker is a tool that makes it easy to create and run applications by using containers. All the software that the application needs lives in a container and is separated from the system OS. Let's look what that means for Laravel development.

Install & setup Laravel Sail

We are going to install and setup Laravel Sail in windows. Please look at the documentation if you want to install it on macOS or Linux.

Install Docker Desktop

The first step is to install Docker Desktop. Docker Desktop is a visual tool that lets you manage your Docker containers.

Install WSL2

Next, we must ensure that Windows Subsystem for Linux 2 (WSL2) is installed. With WSL2 let you run Linux on Windows.

First, we enable the 'WSL' optional feature. Open PowerShell as Administrator and run the following command:

Next, we need to update WSL to WSL2. Before installing WSL2 we need to enable the 'Virtual Machine Platform' optional feature. Open PowerShell as Administrator and run the following command:

Restart your machine to complete the WSL install and update to WSL2.

Now you need to download & install the Linux kernel update package. Download the package and double-click to run the installer.

Next run the following command to set WSL2 as your default version:

The last step is to download a Linux distribution from the Microsoft Store. I choose to download & install Ubuntu 20.04 LTS.

After installation, a console windows will open. You will then need to create a user account.

Configure Docker Desktop

Open Docker Desktop and click on the gear icon on the top. Then select the Settings > Generaltab.

Select the Use the WSL 2 based engine check box.

Then click on the Apply & Restart button.

That is it!! Now you are ready to create a Laravel project.

Create a new Laravel project

Launch Windows Terminal and open a new tab with a session for your Linux OS. Then run the following command:

This command will create a new Laravel application in a directory named example-app. Of course, you can change 'example-app' in this URL to anything you like.

When the project has been created, you can navigate to the application directory and start Laravel Sail:

The first time you run the Sail up command, Sail's application containers will be built on your machine. This could take several minutes. After that you can access the application in your web browser at: http://localhost.

Developing within WSL2

I use Visual Studio Code for developing. To modify the Laravel application files within WSL2 you must install the extension: Remote Development. After the installation you can open Docker Desktop and click on the Open in Visual Studio Code button on the Containers / Apps screen.

To run artisan commands, you must run the commands using Laravel Sail.

Install for existing application

When you host a Laravel project on GitHub and want to install it, follow the steps below.

First launch Windows Terminal and open a new tab with a session for your Linux OS. Then clone a repository:

Then run the following Docker command:

This command uses a small Docker container containing PHP and Composer to install the application's dependencies. After that you can run the following command to copy the .env file, generate an application key and run the database migrations.

Round up

In this tutorial we have learned how to create a project in Laravel 8.x. If you want to get updates about new blog posts, follow me on twitter!

Original post: larapeak.com

Like this article? Let the author know and give them a clap!

Download and install Laravel from repository:

Clone Laravel and create project from repository:

Get into the folder and run composer

Set permissions on the project directory so that it is owned by your non-root user:

Install Laravel Through Docker

Create and set up docker-compose.yml

Since we feel lucky today, we’ll create our docker-compose file with some customization over the containers names and ports redirects, in order not to use too common names and ports, that might conflict with some other containers on our host.

You can take a look at the final version of the docker-compose.yml file some lines below.

We’ll create 3 containers:

  • app named as app101 which holds our laravel app. Feeds from the Dockerfile we’ll create later.
  • webserver named as webserver2 which holds our web-server; nginx for this example. This service exposes port 8101 for default 80, and 8102 for 443, ad it feeds from the image nginx:alpine
  • db named as db2 which holds the MySQL service. This service exposes port 8103 for default 3306. We also must set the database name (laravel_docker) and the mysql password for root user (password admin for this example).
Install Laravel Docker

Persisting Data

We’ll add some extra configuration to our docker-compose file; we need to add volumes to our db service. Without them, every time the container stops or fails, all data would be missing.

So here we are telling that everything inside /var/lib/mysql will be replicated into the dbdata folder of our host. Plus, we persist the configuration of the database on /etc/mysql/my.cnf.

We also set the volume to be mounted on the local driver:

Our final docker-compose.yml file looks like:

Create Dockerfile

Now for making docker-compose to run, we need to create a Dockerfile file to set the image we named as laravel-sandbox, and set php, folders and ports as well.

Setting PHP

Create file laravel-sandbox/php/local.ini

Setting Nginx

Create file laravel-sandox/nginx/conf.d/app.conf

Setting MySQL

Create file laravel-sandbox/mysql/my.cnf

Running the container

Install Laravel Docker Download

Firt copy and rename the .env envioroment file for our app:

And set the correct values for the database connection due the values set on our docker-compose.yml

And finally we can run the container

Now, if everything went as expected and if type from the console

We should see something as:

We can run now the 2 extra following commands from the console for adding some security to our app.

So we added our encrypted key for the container and cached our configuration.

Migrating data

Now we have everything up && running, we should be able to see our app from the browser (http://localhost)

Install Laravel Docker Windows

For migrating data, we first must create an user for the database. So we first we access to the database this way:

And then we can access to the mysql client

The console will prompt asking for the password (MYSQL_ROOT_PASSWORD param on docker-compose.yml)

Now we should be able to see the database by typing:

Cached

mysql> show databases;

And create the user by typing the following commands:

Now we can skip out from the container;

Now we can permform the migrations;

ADDING AUTH AND SEEDING A USER FOR LOGIN

At this point, we might be interested on adding a user to the databases and performing actions for loged-in users. So from the console we perform make:auth:

Then we can add some /database/seeds/UsersTableSeeder.php to our project and enable it on DatabaseSeeder.php

Install Laravel Docker Mac

DatabaseSeeder.php

Laravel install docker is not running

UsersTableSeeder.php

Now we execute composer dump-autoload in order to make noticeof the new generated file:

Install Laravel Docker Mac

And fillally we proceed to execute the seeder

Now we can login into our app with user specified on UsersTableSeeder.php!