Get Ready to Docker
Closing down the PHP project today felt a little sad. It’s a series that I’ve been working on for quite some time now and finally felt like I’ve exhausted the subject. I’ll probably continue with some more advanced topics since I don’t want it to end. As I write that, I feel a little better about it now. Let’s continue with my absolute favorite framework, Laravel.
We briefly looked at the installation process in the previous article, and to be honest, it’s about as simple as it gets.
There are a few different ways to install Laravel, but we’re not going to focus on anything else other than Docker. This is pretty standard in the industry and at the end of the day, it’s your development machine. You can setup the project however you like, as long as it runs.
Docker Setup
Windows environments have proven difficult to install Docker Desktop onto. The Laravel documentation does offer a very thorough explanation on how to install it, along with all the dependencies that are required to setup prior to the installation, like installing and enabling WSL2. As long as you read the following steps, you’ll be able to install it on a Windows machine, if you haven’t already.
https://laravel.com/docs/10.x/installation#sail-on-windows
On macOS and Linux, the process is similar. You’ll need to download Docker Desktop.
After you’ve finished installing Docker Desktop, make sure that it’s running. You’ll see the docker icon in your menu bar.
Installing Laravel
Next, open your terminal and cd into a location that you want to install your application into. For example, I created a www
directory on my user’s account where I will store all of my web-projects. After we cd
into that folder, we can run the following command:
curl -s "https://laravel.build/example-app" | bash
You can grab the URL and visit it in your browser. You’ll see a response like the following if you do.
docker info > /dev/null 2>&1 # Ensure that Docker is running... if [ $? -ne 0 ]; then echo "Docker is not running." exit 1 fi docker run --rm \ --pull=always \ -v "$(pwd)":/opt \ -w /opt \ laravelsail/php81-composer:latest \ bash -c "laravel new example-app && cd example-app && php ./artisan sail:install --with=mysql,redis,meilisearch,mailhog,selenium " cd example-app ./vendor/bin/sail pull mysql redis meilisearch mailhog selenium ./vendor/bin/sail build CYAN='\033[0;36m' LIGHT_CYAN='\033[1;36m' BOLD='\033[1m' NC='\033[0m' echo "" if sudo -n true 2>/dev/null; then sudo chown -R $USER: . echo -e "${BOLD}Get started with:${NC} cd example-app && ./vendor/bin/sail up" else echo -e "${BOLD}Please provide your password so we can make some final adjustments to your application's permissions.${NC}" echo "" sudo chown -R $USER: . echo "" echo -e "${BOLD}Thank you! We hope you build something incredible. Dive in with:${NC} cd example-app && ./vendor/bin/sail up" fi
You can see that it’s just a script that will create your project. You can change the example-app
URL parameter to whatever you want your project name to be in https://laravel.build/example-app
. For this project, I’ll name it laravel-tutorials
.
curl -s "https://laravel.build/laravel-tutorials" | bash
Press enter and you’ll see your project being built.
It will ask you for your password so that changes can be made. You’re clear to do so. You’ll see the final confirmation.
Please provide your password so we can make some final adjustments to your application's permissions. Password: Thank you! We hope you build something incredible. Dive in with: cd laravel-tutorials && ./vendor/bin/sail up dinocajic@Dinos-iMac www %
Perfect. The last step tells us what to do. Execute the following command:
cd laravel-tutorials && ./vendor/bin/sail up
Once it boots up, you’ll see something that looks similar to the screenshot below.
It states that the Server is running on http://0.0.0.0:80
Paste the URL into your browser and viola, Laravel is installed.
Stopping Docker Containers
Once you’re done, go back to your terminal and hit CTRL + C
on your keyboard. It will stop all of your Docker containers.
If you check your Docker Desktop, you should see that your project is no longer running.
Starting Laravel Project with Docker
You can always cd
into your Laravel folder via terminal and run: ./vendor/bin/sail up
. Or, you can go to your Docker Desktop and click on the Play button for your project.
I prefer the Docker Desktop -> Play method. It’s just easier. You can also quickly see all of the various containers, so if you need to execute specific scripts on a container, you can click on it and go into the CLI.
Running Commands
You’ll need to run the commands from inside of the Laravel container. It’s usually named laravel.test-1
.
Click on the container and you’ll be inside of the Logs section.
Click on the Terminal tab to go inside of the terminal for that container.
This is where you can now start typing your commands, like php artisan migrate
. We’ll get into that later.
If you don’t like to be tied down to Docker, you can click on the Open in external terminal
link at the top right of the screen.
This opens up your terminal with the Docker container already running.
You can then execute the required commands.
Opening Your Project
The project is stored on your machine. Open it up in your IDE. You’ll see the folder structure in there. We’ll cover where things are stored next time, but for now, let’s open the docker-copmose.yml
file. You’ll see how the Laravel/Docker project was constructed.
You’ll also notice that it has environmental variables, like the following:
${APP_PORT:-80}:80
That APP_PORT
can be defined in the .env
file since it’s not in there by default. We’ll explore that later. For now, any time that your changes are made here, they will be reflected when you reload the browser.
Summary
This whole article could have been condensed into a few steps.
- Install Docker Desktop
- Run the following command in your directory:
curl -s “https://laravel.build/laravel-tutorials" | bash
- Once installed, run the following command:
cd laravel-tutorials && ./vendor/bin/sail up
- Test in your browser:
http://0.0.0.0:80
Now that you have the project setup and can open and close it without any issues, we’re ready to start diving into the various Laravel concepts. See you next time.
Laravel Series
Continue your Laravel Learning.
Empower Your Web Development Journey
Kickstart your web development with our Laravel Series Intro: Master the essentials of this elegant PHP framework for robust, modern web applications.
Get Ready to Docker
Efficient Laravel Installation with Docker: Streamline Your Development Environment for Professional Web Projects.
A Visual Guide to Directory Structures
Laravel – P3: Directory Structure Basics
Where do you put your files? How does Laravel know? These are a few questions that I normally get from individuals just starting out with Laravel.