0% found this document useful (0 votes)
4 views

Sample app in Docker Containers Assignment

Uploaded by

Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Sample app in Docker Containers Assignment

Uploaded by

Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Assignment: Application Deployment in Docker Containers

Installing Docker

The first step was installing Docker on my personal computer. I downloaded Docker Desktop
from the official Docker website and installed it.

This is my Docker Desktop application In my Personal Computer.


Once installed, I verified that Docker was running by using the docker --version command in
the terminal.

Creating the Application

I created a simple Node.js application. The application included:

 index.js to define the server logic using Express.js.


 A public/ directory to hold static files like index.html, style.css, and app.js.
 A package.json file for dependencies.

I made a server in index.js it was set up to serve static files and listen on port 4000.
CREATING THE DOCKERFILES FOR BACKEND AND FRONTEND:

This Dockerfiles are for setting up a Node.js application inside a Docker container.

For BackEnd:

For FrontEnd:

1. Base Image
I used the latest Node.js image as the base for my container:

dockerfile
FROM node:latest
2. Set Working Directory
Inside the container, I created and set a working directory named /app where all the
app files will go:

WORKDIR /app

3. Copy Dependency Files


I copied the package.json file into the container. This ensures only the dependency list
is copied in this step:

COPY package.json ./

4. Install Dependencies
I installed all the Node.js dependencies using the npm install command:

RUN npm install

5. Copy Application Files


After installing the dependencies, I copied all the remaining application files (like
index.js, other scripts, and folders) into the container:

COPY . .

6. Expose Port For BackEnd


I specified that the app will run on port 5000 and made it accessible from outside the
container:

EXPOSE 5000

7. Build the Frontend


I built the production-ready frontend files using:

RUN npm run build

This step generates optimized files that can be served in production.

8. Expose Port For FrontEnd


I exposed port 3000 since my app runs on this port. It makes the app accessible
outside the container:

EXPOSE 3000

9. Run the Application


Finally, I used this command to start the app in the container:

CMD ["npm", "start"]

This Dockerfile allowed me to containerize my backend app, install its dependencies, and run
it on port 5000. It’s ready to use with a simple docker build and docker run process.
BUILDING AND CREATING THE DOCKER CONTAINERS USING DOCKER-
COMPOSE FILE:

I used a docker-compose.yml file to set up and run multiple containers for my application.

Prepare the Docker Compose File

I created a docker-compose.yml file that defined all the services required for my app, like the
frontend, backend, and database. Each service had its own build context and port mappings.

 The frontend ran on port 3000.


 The backend ran on port 5000.
 The MongoDB database was mapped to port 27017.

This docker-compose.yml file sets up and manages three services: the frontend, backend, and
MongoDB database for my application:

1. Version

I’m using version 3.8 of Docker Compose, which supports the latest features.

2. Services

The services section defines the different parts of my application that will run in separate
containers.

 Frontend
 The frontend service builds the frontend app from the ./frontend directory
where the Dockerfile is located.
 It maps port 3000 on my local machine to port 3000 inside the container so I
can access the app in my browser.
 It depends on the backend service, meaning the backend starts first to ensure
the frontend has access to it.

 Backend
 The backend service builds the backend app from the ./backend directory
using its Dockerfile.
 It maps port 5000 on my local machine to port 5000 inside the container.
 It depends on the mongodb service, so the MongoDB database is ready before
the backend starts.
 I also set an environment variable MONGO_URI so the backend knows how
to connect to MongoDB.

 MongoDB
 The mongodb service uses the official MongoDB 6.0 image to set up the
database.
 I named the container mongo for easier identification.
 It maps port 27017 from my local machine to the container.
 I added a volume mongodb_data to ensure the database data is stored
persistently, even if the container is restarted or removed.

3. Volumes

 I created a named volume called mongodb_data to store the database data outside the
container. This way, the data isn’t lost when the container is stopped or removed.

This docker-compose.yml file automatically sets up the frontend, backend, and database
containers, connects them, and ensures they run in the right order. It also makes the setup
consistent and easy to manage.

Running and Building the Docker-Compose File:

I used below command to combines the building and starting process into a single step. To
run the containers in detached mode I added -d flag:

docker-compose up --build -d

 Run the above command in the directory where your docker-compose.yml file is
located.
 It will build all the necessary images, create the containers, and start them based on
the configuration in the compose file.
Check the containers

docker ps
Accessing the Application in my Browse:

After running the container, I accessed my application in my browser using this link
http://localhost:3000
Accessing Logs :

While the container was running, I viewed the logs using:

docker logs 4ef9299abdb0 # logs for mysql container


Accessing the Database Container :

1. Start an interactive shell inside the container:

docker exec -it 4ef9299abdb0 bash


The data that I entered in my application is stored in database the above images show the data
that I created.

Exiting the Container

To exit from the container's interactive shell, I simply typed exit. This brought me back to my
local terminal.

Stopping the Container

When I was done, I stopped the container using:


docker-compose down

Troubleshooting Issues

Initially, I faced some challenges, such as the app not loading properly. To troubleshoot:

 I checked the logs for errors using docker logs.


 Verified the container was running with docker ps.
 Used docker exec -it 4ef9299abdb0 bash to access the container and inspect the
application files directly.
 After that I gave the Port numbers properly.
 By doing these steps I troubleshoot the issue and made application running.

I successfully built, deployed, and ran a containerized application. I also access logs,
troubleshoot issues, and work with various Docker commands.

You might also like