Laravel in Docker Container

by sz3jdii
1 comment 8 mins read
Laravel

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? πŸ±β€πŸ’»

Laradock is the answer!
Laradock is the answer!

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 πŸš€

Say hello to Docker!
Say hello to Docker!

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.

Laradock Logo
Laradock

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:

Laradock inside repository
Laradock inside repository

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.

Laradock proper env files
Caution! Make sure that you are creating .env inside ./laradock directory! Don’t use .env.example from main project directory, use the one from ./laradock instead!

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

PHP_VERSION=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.

Laradock docker compose up
Laradock after running it

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.

Laradock running commands inside container
Laradock running commands inside container

Check if everything is working

Open up your browser and go to http://localhost/ you should see bellow image:

Opening Laradock in Webbrowser
Opening Laradock in Browser

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.

phpMyAdmin

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:

docker ps
Laradock phpMyAdmin port
Laradock phpMyAdmin port

As we can see port for phpMyAdmin is 8081, let’s check this!

phpMyAdmin panel
phpMyAdmin panel

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!

Related Articles

1 comment

Mark 2022-09-06 - 21:47

Thanks for your blog, nice to read. Do not stop.

Reply

Leave a Comment

This website uses cookies to improve your experience. I'll assume you're ok with this, but you can opt-out if you wish. Read Privacy Policy. Accept