Building and Deploying a Two-Tier Application with Docker Compose: A Complete Guide
Table of contents
In the last article, we learned about containerization a two-tier application using Dockerfile. Moving further, in this blog we will see how to containerize an application using docker compose.
Here in this project we have:
Frontend: The
nginx
service is acting as the frontend. It serves as a reverse proxy and may handle static files, such as images, CSS, or JavaScript, depending on its configuration in the./nginx
folder (not visible in the current configuration). Nginx is typically used to serve the frontend or act as a gateway to the backend services.Backend: The
django_app
service represents the backend. It runs the Django application, which is a web framework that handles requests, business logic, and serves dynamic content (e.g., HTML pages, API responses). This is the part of the application responsible for processing the user's requests and interacting with the database.Database: The
db
service, which uses MySQL, is the database. It stores and manages the application's data (e.g., user information, notes, etc.) and handles all database-related operations such as querying, inserting, and updating records.
Pre-requisites:
Docker installed on a server using AWS account.
Docker Hub account
To successfully run this project, the following steps should be followed
Clone a git-hub repository of the Django application.
Understand the requirements of the projects and write a Docker File to support all the dependencies required.
Write a ‘docker-compose.yml’ file.
Create and run containers for database, backend and frontend using docker-compose.
Check the port mappings properly (docker container: ec2 instance)
Open the port using the Public IP of the AWS server and you will find your application running.
Step 1: Cloning Code
As shown in the screenshot below, the git repository containing Django app named ‘django-notes-app’ is cloned to the local repository.
$ git clone <HTTPS link or path of GitHub repository>
Let's remove the Dockerfile already existing in the repo and try to write your own to run the application. To create Dockerfile, we must be aware of the requirements that can be found in 'requirements' or 'readme' file. If there is no such file exist and you fail to determine the requirements then you need to request the same from developer.
vim Dockerfile
Step 2: Creating Dockerfile
Write the Dockerfile as per the given structure.
FROM <image:tag>
Download the image required named as base image (from Docker Hub)
COPY . .
Code will be copied from the current directory to the current directory only.
RUN
Installs all required packages and dependencies as per the requirements given in the requirements file.
EXPOSE
Will expose the port required.
CMD [arguments]
Will run the whole script after the build stage.
save the file with esc+:wq.
Step 3: Create ‘docker-compose.yml’ file
$vim docker-compose.yml
Step 4: Containerizing Database, Backend and Frontend using docker-compose up -d
Step 5: Publish all ports on ec2 server
Now, go to your ec2 instance and open ports to establish a connection successfully if not already opened.
Step 6: Running application on local host
Step 7: Checkout Database in mysql
Moreover, you can check database ‘test_db‘ created in mysql by exec command:
Hope this project will be helpful to you and add value to your resume. Thanks for reading the article. Please do like and follow if you find my blogs informative and interesting. Let's learn together, Grow together :) - Neha Bhardwaj