Sample app in Docker Containers Assignment
Sample app in Docker Containers Assignment
Installing Docker
The first step was installing Docker on my personal computer. I downloaded Docker Desktop
from the official Docker website and installed it.
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
COPY package.json ./
4. Install Dependencies
I installed all the Node.js dependencies using the npm install command:
COPY . .
EXPOSE 5000
EXPOSE 3000
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.
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.
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.
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 :
To exit from the container's interactive shell, I simply typed exit. This brought me back to my
local terminal.
Troubleshooting Issues
Initially, I faced some challenges, such as the app not loading properly. To troubleshoot:
I successfully built, deployed, and ran a containerized application. I also access logs,
troubleshoot issues, and work with various Docker commands.