Containerization and building images

Writing Dockerfiles and Building Images for Three-Tier Application

Overview

In this step, we will containerize each component of the three-tier application by writing Dockerfiles for the frontend, backend, and database. Docker enables packaging applications along with their dependencies, ensuring consistency across different environments. Once the Dockerfiles are created, we will build the respective images, which will later be used for deployment in Kubernetes.


A three-tier architecture consists of three separate components that work together:

  1. Frontend (Presentation Layer) – A React.js application served via a lightweight web server.

  2. Backend (Application Layer) – A Node.js/Express API that handles business logic.

  3. Database (Data Layer) – A MongoDB database that stores and retrieves data.

Each of these services needs its own Docker container to ensure modularity, scalability, and easy deployment.

Dockerizing the Frontend

Writing the Dockerfile

Create a Dockerfile inside the frontend/ directory:

FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
CMD [ "npm", "start" 

Building the Docker Image

Navigate to the frontend directory and execute the following command:

cd frontend
docker build -t frontend-app .

This will create a Docker image named frontend-app, which contains the built React application.


Dockerizing the Backend (Node.js/Express)

Writing the Dockerfile

Create a Dockerfile inside the backend/ directory:

# Use official Node.js runtime as base image
FROM node:18

# Set working directory inside the container
WORKDIR /app

# Copy package.json and install dependencies
COPY package.json package-lock.json ./
RUN npm install

# Copy the backend application source code
COPY . .

# Expose the backend port
EXPOSE 5000

# Start the backend server
CMD ["npm", "start"]

Building the Docker Image

Navigate to the backend directory and run:

cd backend
docker build -t backend-app .

This will generate a backend-app image, ready to be deployed.


Dockerizing the Database (MongoDB)

Writing the Dockerfile

Create a Dockerfile inside the database/ directory:

# Use the official MongoDB image as the base
FROM mongo:latest

# Expose MongoDB's default port
EXPOSE 27017

# Start MongoDB when the container runs
CMD ["mongod"]

Building the Docker Image

Navigate to the database directory and build the image:

cd database
docker build -t database-app .

This step creates a database-app image, which runs MongoDB inside a container.


Verifying Docker Images

After successfully building the images, verify them using:

docker images

Expected output:

REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
frontend-app      latest    <image_id>     <time ago>    <size>
backend-app       latest    <image_id>     <time ago>    <size>
database-app      latest    <image_id>     <time ago>    <size>

This confirms that all three images have been successfully created and are ready to be used.

Last updated