Deploying Containerized Laravel Application Using Docker, AWS ECS, And Relational Database Service(RDS)
Prerequisite:
- The basics of Docker.
- Linux commands.
At times deploying applications can be frustrating. Well, this won’t be any different. While deploying this application, I stumbled into some challenges. I didn’t get it on the first try, but I hope you do.
During my internship at MakersValley, I had the opportunity to work with Laravel ( A PHP framework ) for the first time and it was a whole lot of fun to work with. I had to practicalize what I had learned about Docker and I also got the opportunity to deploy the application on AWS. The Internship was a success with a lot of positive feedback.
This is a detailed walk-through on building a containerized Laravel application using Docker and deploying it on AWS using AWS EC2 instances and RDS for our database.
At this point, I assume you have built your application.
Creating the Docker config file.
Now, Let’s build the docker file on our local computer to test.
docker build -t simplelaravelapplication .
Running the docker image and forwarding the ports to an external port
docker run -p ‘external port’:’docker internal port’ ‘image name’
example:
docker run -it -p 8080:80 simplelaravelapplication
use ”docker inspect” to get information about the docker container. From there you will see the gateway IP address. We can use that to access the docker container
Building the application on AWS using docker
These are the services used.
- EC2
- Elastic Container Service(ECS)
- Relational Database Service(RDS)
- AMI
- Code Build
To run our application on AWS using docker we have to modify our dockerfile slightly
After modifying the dockerfile, we will create A buildspec.yml file this is the file AWS will read to do every necessary configuration and build the docker image on AWS.
I will point you to a blog to set up AWS services for our application.
Checkout Gbenga Oni’s blog on Deploying a Docker-ized Laravel Application to AWS ECS with CodeBuild
I have modified the dockerfile and the buildspec.yml file because the dockerfile on the blog didn’t work for me.
After that, we can set up RDS for our database.
Setting up Relation Database Service ( RDS )
Now, setting up RDS is quite easy, we just have to click a few buttons here and there.
Let’s search for the Relational Database Service.
Like I said clicking is what we will be doing, So let’s click on the “create database” button.
It’s time to configure our database, the first option is to select the database creation method.
We can either choose between the Standard or Easy create.
Standard Create: The standard option gives you more options and configuration prowess which includes availability, security, backups, and maintenance.
Easy Create: This option makes use of the recommended best-practice configuration, and some of the configurations can be changed after the database has been created.
So, for this tutorial, we will be using the standard option.
After we are done selecting our options we can go ahead to select the engine options we want to use, there are multiple options to choose from but for this tutorial, we will be using MySQL.
Next up we choose the version of MySQL We want to use.
Let’s choose a template for the database, As a free tier account user choose the free tier option so that you don’t incur extra charges.
Let’s set up the database itself, the first thing is to choose the database instance identifier. Then the username and password, make sure you remember the password you are using.
We want to choose the Database instance class, we will be making use of the db.t2.micro as a free tier user that’s what I will be using.
For the storage, you can select any option suiting your application
For connectivity, let’s make it accessible publicly so we can connect it to our application and access it from our terminal or command line.
Finally, let’s click on create database button to create our database. Mind you this will take a few minutes to create. So, let’s wait patiently.
After the database is created the status should change to available.
Click on the identifier to get more information about the database.
You should be on the page shown in the image below.
So, we need some information from the image above. The endpoint and the port, we already have the username, password and, database instance identifier.
Go to your file and search for .env file that is where we will be using all the information we gathered.
Yay!!!! We made it through[sigh], that’s all [smiles]. We have successfully deployed our Laravel application and integrated it with RDS
Conclusion
In this article, we set up our dockerfile for Laravel, we also set up our buildspec.yml file for AWS deployment. We also created a Relational Database service and integrated it into our application.