Hi everyone, welcome to my blog post. In this article, I will show you how I deployed the Laravel application in this article with docker. The application itself can be found on this repository.
To get started, you will need to install Docker on your machine. For this project, I installed Docker on an Ubuntu 20.04 machine on Digital Ocean. You can follow this tutorial to set yours up.
Before diving into the tutorial, here are some important points to note about Docker, Laravel, and docker-compose.
What is Docker?
To fully understand what Docker is, we need to explain a concept called containerization
. Containerization refers to the process of packaging an application and all that is required to run it, such as its runtime dependencies, configuration files, etc, in any kind of environment. These environments could be Linux, Windows, macOS machines, Desktops, or the cloud. Docker is one of the most popular containerization solutions in the market.
Docker is an Open-Source solution that helps to create, manage, and distribute applications easily.
Dockerizing Laravel
Typically, when you build a Laravel application, you will need at least these three components for it to work:
Server
PHP server
Database
So, to dockerize this app, we can create multiple containers that will cater to the different components we have mentioned above. We can build one container each for the three components we will work with.
This is where docker compose
comes in. Docker Compose is a tool that is used to manage multiple containers in a project. With this tool, we can create a .yaml
or .yml
file to configure the services that will run in all the containers, and with a single command, we can spin up all the services/components required for the app to function or pull down all the resources.
Steps Taken to Dockerize the Laravel App
Create a User and add to the Docker group:
One of the best practices in using the docker command is not to run the commands as a sudo user. So we need to create a docker group and a user that will run the docker commands as a non-root user.
sudo groupadd docker sudo adduser NAME-OF-USER
However, on some systems such as Ubuntu, the Docker group is automatically created as soon as you install Docker. So, If it exists, you will get a response like this:
groupadd: group 'docker' already exists
If it didn't exist on the system, the command will execute without any output. The next step is to add the user to the Docker group:
sudo usermod -aG docker NAME-OF-USER
Cloning the Repository:
Then, switch to the new user's terminal (the Docker user) with this command:
sudo su NAME-OF-USER
And clone the repository to get the codebase into the system. The command to do this is:
git clone https://github.com/Adephumie/laravel-realworld-example-app.git laravel-app
Editing the .env file:
The next step is to change into the directory and copy the
.env.example
file into another file called.env
. These are the commands to do that:ls cd laravel-app cp .env.example .env
You should note at this point that the
.env
file, which is the environment configuration file, holds sensitive information about your server hence the file must not be shared with the public. That is why you will always see.env.example
file on public GitHub repos.The values in the Database Server part of the
docker-compose.yaml
file will be mapped to the Database configuration section of the.env
file. Make sure to change theDB_DATABASE
,DB_USERNAME
, andDB_PASSWORD
values to your choice.DB_CONNECTION=pgsql DB_HOST=pgsql DB_PORT=5432 DB_DATABASE=laravel-realworld DB_USERNAME=sail DB_PASSWORD=password
Install Dependencies with Docker:
According to the README.md file in the repository, You can install the project's dependencies using Docker with:
docker run --rm -it \ --volume $PWD:/app \ --user $(id -u):$(id -g) \ composer create-project
About the Dockerfile:
The
Dockerfile
for this app, according to the docker-compose file, is found in the path declared in thecontext
. However, this link takes you to the Dockerfile for this app.Starting the Containers:
According to the repository, the preferred way of setting up the project is with Laravel Sail. Laravel Sail is a lightweight command-line tool for interacting with Laravel's default Docker environment. Sail commands are invoked using the
vendor/bin/sail
script that is included with all new Laravel applications. To Start the containers:./vendor/bin/sail up -d
Setting an Alias:
Owing to how long the sail command is, you can create an alias with this command:
alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'
Migrate Database:
Data migration is achieved with this:
sail artisan migrate --seed
After successfully executing these commands, on your browser, copy the IP address of your VM and open on port 3000 as shown below:
http://IP-Address:3000/api
You should have an image as shown below.
Conclusion
This marks the end of this article. We have successfully deployed the Laravel app with Docker and Laravel Sail using the docker-compose file. Thank you.