In this blog, we will see how to Containerize Laravel Applications With Docker by using laradock.
- Setup for Single Project
- Setup for Multiple Projects
1. Docker Introduction
1.1 Docker Platform
Docker is an open platform for developing code, shipping, and running applications. Docker helps you to keep separate your applications and your infrastructure so you can deliver software quickly.
The container is a loosely isolated environment, Docker gives the ability to package and run an application here. In a given host you can run many containers simultaneously.
Containers are lightweight and they keep everything needed to run the application, so you do not need to depend on what is currently installed on the host.
You can share containers while you work, everyone you share with gets the same container that works in the same way.
1.2 Docker architecture
Docker uses a client-server model.
You can run the Docker client and daemon on the same system or a Docker client can interact with a remote Docker daemon.
REST API, over UNIX sockets or a network interface used to communicate The Docker client and daemon
1.3 The Docker daemon
The Docker daemon (dockerd) hears for Docker API requests.
The Docker daemon contains Docker objects like images, containers, networks, and volumes. To manage Docker services a daemon can also share with other daemons.
1.4 The Docker client
For Docker users, the Docker client is the primary way to interact with Docker.
commands like docker run, the client sends these commands to Docker daemon, which carries them out. The Docker client can talk with multiple daemons.
1.5 Docker Desktop
Docker Desktop is an application for your Mac or Windows environment that helps you to build and share containerized applications and microservices. Docker Desktop contains the Docker daemon (dockerd), the Docker client (docker), Docker Compose, Docker Content Trust, Kubernetes, and Credential Helper.
1.6 Docker registries
A Docker registry is utilised to store Docker images. Docker Hub is a public registry, and By default Docker Hub is configured to look for images. You can even run a private registry.
1.7 Docker Hub
A service provided by Docker for finding and sharing container images with your team is Docker Hub. Docker Hub is a public registry that everybody can access.
Docker Hub provides the following major features:
- Repositories can use to Push and pull container images.
- Manage access to container images’ private repositories.
- You can pull and use container images provided by Docker.
- You can pull and use container images provided by external vendors(Docker verified publisher).
- Push container images to Docker Hub which is Automatically built from GitHub and Bitbucket.
- Trigger actions using webhooks after a successful event to a repository to integrate Docker Hub with other services.
1.8 Docker Compose
Multi-container Docker applications use a tool called Compose for defining and running.
Using Compose have a three-step process:
- Add Dockerfile into your app’s environment.
- Add docker-compose.yml with Definitions of the services that make up your app so they can be run together in a container.
- You can alternatively run the command docker-compose up using the docker-compose binary It starts and runs your entire app.
Compose has commands to manage the whole lifecycle of your application:
- Start, stop, and rebuild services
- View the status of running services
- Stream the log output of running services
- Run a one-off command on a service
2. Docker with laravel application
Laradock is a development environment that supports PHP for Docker.
- You can switch PHP versions: 8.0, 7.4, 7.3, 7.2, 7.1, 5.6…
- You can Choose a database engine: MySQL, Postgres, MariaDB…
- Each software runs on a separate container: PHP-FPM, NGINX, PHP-CLI…
- Simple edits on Dockerfile to customize any container,
- You can use Laradock per project or a single Laradock for all projects.
- By using environment variables in Containers you can install/remove the software.
- Well-structured Dockerfiles (Dockerfile) and the latest version of the Docker Compose file (docker-compose).
Setup a demo stack PHP, NGINX, MySQL, Redis, and Composer:
1 – Inside your PHP project Clone Laradock:
git clone https://github.com/Laradock/laradock.git
2 – Inside the laradock folder and rename .env.example to .env.
cp .env.example .env
3 – Run containers:
docker-compose up -d nginx mysql workspace
4 – Open the .env file in your project and set the following:
5 – Open your browser and visit localhost: http://localhost.
2.1.1 Setup for Single Project
If we have a laravel project we have to clone laradock on the root directory of your project.
>git submodule add https://github.com/Laradock/laradock.git
If not using git yet in your project
>git clone https://github.com/Laradock/laradock.git
that folder structure will look like this
copy .env.example to .env in laradock folder
>cp .env.example .env
in .env set APP_CODE_PATH_HOST=../
in apache2/sites folder copy config file sample.conf.example to project.conf.
And add domain name on hosts file 127.0.0.1 project.test
Run these commands to build your environment and run containers
>docker-compose up -d apache2 MySQL PHPMyAdmin
If we need to run composer, artisan commands we can enter the project directory by using docker-compose
>docker-compose exec workspace bash
Here we can run artisan, composer commands
Have to update database environment variables on the .env file in your workspace.
to enter mysql container we can use docker-compose and we can create a database if we need
>docker-compose exec MySQL bash
>mysql -u root -p
Finally, we did set up for a single project so we can go with the browser to run your project.
2.1.2 Setup for Multiple Projects
We can follow the laradock documentation
Clone the laradock repository on your machine by using the below command
>git clone https://github.com/laradock/laradock.git
Let’s create new laravel projects on the same directory
>composer create-project laravel/laravel project1
>composer create-project laravel/laravel project2
Your folder will be like this
Changes in laradock directory:
In the .env file APP_CODE_PATH_HOST, variable should point to the parent directory.
Before that we have to copy .env file from .env.example.
create config files in your web servers To point to the different project’s directory, For apache2 the config files have to be listed in apache2/sites directory.
Config files named should be like project1.conf, project2.conf
Default names are app.conf.example, laravel.conf.example and symfony.conf.example.
We have to rename or copy it to *.conf (ex: project1.conf)
By using docker-compose we can run apache2, MySQL, phpmyadmin
>docker-compose up -d apache2 MySQL phpmyadmin
We can check if apache2 and PHPMyAdmin are running in the browser
If we update domains in the hosts file then we can use the domain name instead of localhost
By using the following command we can enter the project’s parent directory from here itself we can run artisan commands.
>docker-compose exec workspace bash
In our case we used port 8081 for PHPMyAdmin, it may conflict if use the same port for another container,
We can create databases
We have to enter MySQL container by using following command.
>docker-compose exec mysql bash
Enter as a root user
>mysql -u root -p root
Here we can play with MySQL queries
We can see updates in PHPMyAdmin
So here we can use databases for our project by changing mysql environment variables in the project’s .env file
Now you can run projects with different domains project1.test and project2.test
2.2 Laravel Sail
Laravel Sail is used for communicating with Laravel’s default docker development environment.
Sail gives facility to building a Laravel application with PHP, MySQL, and, Redis without Docker experience.
Sail is the docker-compose.yml file.
The sail script is stored at the root of your project.
To interact with the Docker containers The sail script provides a CLI with conventional methods.
All new Laravel applications have automatically installed Laravel Sail.
>curl -s https://laravel.build/example-app | bash
For existing applications, we can install Sail by running the following commands.
>composer require laravel/sail –dev
>php artisan sail:install
The above command is used to publish a docker-compose.yml file to the root of your project.
Sail start and stop
To start all Docker containers which are defined in your project’s docker-compose.yml file you have to execute the up command.
> ./vendor/bin/sail up
>alias sail=’[-f sail] && bash sail || bash vendor/bin/sail’
Now you can directly use “sail up” instead of “./vendor/bin/sail up”.
To stop sail you can use command >sail stop
2.2.1 Custom Domain with Multiple Laravel Sail applications
Create a directory to run multiple Laravel applications
So you have to create a docker-compose.yml file and add contents like mentioned in the screenshot.
And run command >docker-compose up -d to start docker.
Install more than one project. In our case, we named two projects: example-app and example-app-2.
Like the above screenshot, we have to add environment variables in our project’s .env file.
And run >sail up we can see the results in the browser.
Here we discussed the Basics of docker, laradock, laradock setup with a single project, set up with multiple projects, and Laravel Sail. Hope we can get some idea about docker with this blog.