Simplify your Laravel setup with a seamless Docker environment on Windows

We looked at setting up an existing project on Windows with Laravel, but we haven’t looked at setting up a new project. It is different and simpler than bringing in an existing project.

The first thing that you’ll need to do is make sure that your new PC is up-to-date. Next, you’ll need to update WSL. Open PowerShell and run the following command: wsl --update.

Install Docker

Can’t do much without Docker. Go to https://www.docker.com/ and download the latest version of Docker for Windows.

Run through the prompts to install Docker. Once installed, make sure that WSL is selected. Open Docker Desktop, click on the gear icon (Settings) in the top right of the screen, and verify that WSL 2 is selected under the General tab.

Install Ubuntu

Head over to the Microsoft Store and search for Ubuntu. I downloaded the latest available version.

 

This would probably be a good time to restart your computer and make sure that all of the changes have taken effect.

Remove credstore

You may or may not have to do this. I did. Docker would just not work properly without this step. Open your Ubuntu system and cd ~/.docker/. It’s time to make a modification to config.json inside of the .docker directory. Once inside the directory, run sudo nano config.json.

{
 "auths": {},
 "credstore": "desktop.exe"
}

If you see the credtore entry, remove it and save it: CTRL + X and then press y and then Enter key.

Curl

The Laravel docs want you to use curl to pull the application instructions and pipe it to bash.

For simplicity, make sure that you’re in your user directory. If you don’t know, cd ~ to get back to your user’s directory in Ubuntu. Type ${PWD} to see the directory that you’re in.

dinocajic@Alien-Laptop:~$ ${PWD}
-bash: /home/dinocajic: Is a directory

Pull the alpine/curl image from Docker: https://hub.docker.com/r/alpine/curl.

docker pull alpine/curl

That’s all you need. It’s time to initialize your Laravel application.

docker run --rm alpine/curl:3.14 -s https://laravel.build/example-app | bash
dinocajic@Alien-Laptop:~$ docker run --rm alpine/curl:3.14 -s https://laravel.build/example-app | bash
Unable to find image 'alpine/curl:3.14' locally
3.14: Pulling from alpine/curl
97518928ae5f: Pulling fs layer
bc2c78b44af4: Pulling fs layer
05df90dbd213: Pulling fs layer
05df90dbd213: Download complete
bc2c78b44af4: Verifying Checksum
bc2c78b44af4: Download complete
97518928ae5f: Download complete
97518928ae5f: Pull complete
bc2c78b44af4: Pull complete
05df90dbd213: Pull complete
Digest: sha256:c64976d53728ca1b4918a49257845af27e343c4a79090788f83afe9f3e800965
Status: Downloaded newer image for alpine/curl:3.14
latest: Pulling from laravelsail/php82-composer
bb263680fed1: Pull complete
...

Sail

Time to run sail up and launch your application. If ./vendor/bin/sail up doesn’t work right away, try ./vendor/bin/sail build and then ./vendor/bin/sail up.

dinocajic@Alien-Laptop:~$ cd example-app/
dinocajic@Alien-Laptop:~/example-app$ ./vendor/bin/sail up
[+] Running 10/10
 ⠿ Network example-app_sail               Created                                                                                                                     0.1s
 ⠿ Volume "example-app_sail-redis"        Created                                                                                                                     0.0s
 ⠿ Volume "example-app_sail-meilisearch"  Created                                                                                                                     0.0s
 ⠿ Volume "example-app_sail-mysql"        Created                                                                                                                     0.0s
 ⠿ Container example-app-mailpit-1        Created                                                                                                                     0.4s
 ⠿ Container example-app-mysql-1          Created                                                                                                                     0.5s
 ⠿ Container example-app-meilisearch-1    Created                                                                                                                     0.5s
 ⠿ Container example-app-selenium-1       Created                                                                                                                     0.5s
 ⠿ Container example-app-redis-1          Created                                                                                                                     0.4s
 ⠿ Container example-app-laravel.test-1   Created                                                                                                                     0.2s

Docker Container

Open Docker Desktop and you’ll see your container running.

 

Click on laravel.test-1 so that we can do our migrations. Click on the Terminal button to launch the terminal.

# php artisan migrate

   INFO  Preparing database.  

  Creating migration table ........................................................................................................ 83ms DONE

   INFO  Running migrations.  

  2014_10_12_000000_create_users_table ........................................................................................... 138ms DONE
  2014_10_12_100000_create_password_reset_tokens_table ........................................................................... 145ms DONE
  2019_08_19_000000_create_failed_jobs_table ...................................................................................... 96ms DONE
  2019_12_14_000001_create_personal_access_tokens_table .......................................................................... 286ms DONE

#

That’s it. Visit localhost in your browser to see the application running.

 

Laravel Series

Continue your Laravel Learning.

Simplify your Laravel setup with a seamless Docker environment on Windows

Setting Up a New Laravel Application 100% with Docker on Windows

In this guide, learn how to set up a brand-new Laravel application entirely with Docker on Windows. Discover how to configure containers, manage environment variables, handle dependencies, and streamline your development workflow in a consistent, reproducible environment.

Leave a Reply