Dockerized Laravel? Ok why not, but what would you say about dockerized Laravel with all required stuff such as MySQL, phpMyAdmin, Composer etc. Seems legit? 🐱💻
Preparation of a workshop by a developer can really take a lot of time and skills. Today, the role of a programmer is somehow connected with the role of a doctor. Each doctor has his or her specialization and field of expertise. There are no doctors specializing in every area of the human body. It is similar with the programmer, each of them specializes in a given programming language and, moreover, has a separate part of the application to be dealt with (front-end, back-end).
The old way…
Laravel needs at least a web server with PHP installed and a database server such as MySQL to work. Their configuration is not particularly difficult, but for a person unfamiliar with text environments, terminals and Linux, their installation and configuration may be simply troublesome. In order to relieve the programmer from the need to familiarize himself with the subject of servers, there are several ready-made solutions such as XAMPP. Unfortunately, the benefits of XAMPP become its disadvantage at some point. Especially when we want to issue php artisan commands, use a webpack, or run a database other than MySQL.
The new way 🚀
Docker is an open source containerization platform. It enables developers to package applications into containers – standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment.
How can we use Docker here?
Thanks to Docker, we can reduce the necessity to configure such services as a web server, PHP or nginx. There’s a project called Laradock which is all-in-one solution for running all of the above technologies. It supports a variety of common services, all pre-configured to provide a ready PHP development environment.
How to start with?
We need to start by setting up the Docker environment on our operating system. For this purpose, I refer to the official Docker documentation here. However, in the future, I plan to prepare my own instructions, so it’s worth waiting for and be up to with my blog. 😎
Open up our Laravel project and clone Laradock into it. Use the below command:
git submodule add https://github.com/Laradock/laradock.git
Your project should now look like this:
That’s almost it!
We almost have everything in order to run our dockerized Laravel app. Last thing which we have to do is configure .env file for laradock where we can specify PHP version and check default passwords for DB. In this example I’ll use MySQL engine with phpMyAdmin panel for it.
Let’s go to ./laradock/ and copy file .env.example from that directory to the same directory but with other name: .env.
In this example I use Laravel 8 version so I can securely switch into PHP8. That’s why I set PHP_VERSION in ./laradock/.env to 8.0
Now let’s get back to our main project directory and set up database connection in .env file as follows:
DB_CONNECTION=mysql DB_HOST=mysql DB_PORT=3306 DB_DATABASE=default DB_USERNAME=default DB_PASSWORD=secret
Now we have everything so open up terminal and start docker containers. It may take a while with first run.
cd ./laradock && docker-compose up -d nginx mysql phpmyadmin workspace
We should see the bellow status after running containers.
After first install we need to run some commands such as:
composer install php artisan migrate npm install npm run watch-poll
In order to run these commands run bellow command inside ./laradock directory:
docker-compose exec --user=laradock workspace bash
After that command runs you can run above commands.
Check if everything is working
Open up your browser and go to http://localhost/ you should see bellow image:
From now on, you can start coding your application. Due to the fact that Laradock creates docker volumes for both the database and project files, each of the files, after being changed in the home system, will also be changed inside the container. Therefore, after any change in the code, all you have to do is refresh the browser window.
As I mentioned we can also use phpMyAdmin for managing MySQL DB. Panel is implemented inside Laradock, so all we need to do is to check on which port phpMyAdmin container is accepting connections.
Run the following command (you have to exit from workspace container if you are currently inside it) and check port for phpMyAdmin:
As we can see port for phpMyAdmin is 8081, let’s check this!
Enter db host, user and password which are located in ./laradock/.env by default it is.
Hostname: mysql Username: default Password: secret
You should now see all databases for your app.
That’s all! Enjoy!