This project is a simple demonstration of creating and running a Dockerized Node.js application. The steps include creating a Dockerfile and docker-compose.yml, building a Docker image, and running the application using Docker Compose. This application responds with a JSON message "Docker is easy π³" when accessed via HTTP.
- Prerequisites π
- Architecture
- Architecture πΊοΈ
- Docker Setup π³
- Docker Compose Setup π
- Accessing the Application π
- Cleaning Up Resources π§Ή
- Conclusion β
Before you start, ensure you have the following:
- An AWS account π
- Node.js and npm
- Docker and Docker Compose installed for building and pushing Docker images
- Basic understanding of JavaScript and Docker π§βπ»
The application consists of two main components:
- Node.js Application: A simple Express server serving an API.
- MySQL Database: A containerized MySQL instance managed using Docker Compose.
- The Node.js application runs inside a Docker container.
- The application connects to a MySQL database running in another container.
- Both containers are managed using Docker Compose.
- Initialize a Node.js project:
npm init -y
- Install dependencies and start the project:
npm install npm start
- Create an
index.js
file with the following content:const app = require('express')(); app.get('/', (req, res) => res.json({ message: 'Docker is easy π³' }) ); const port = process.env.PORT || 5000; app.listen(port, () => console.log(`app listening on http://localhost:${port}`));
Create a file named Dockerfile
in your project directory:
FROM node:12
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
ENV PORT=5000
EXPOSE 5000
CMD [ "npm", "start" ]
- Build the Docker image:
docker build -t iambot/myfirstnodeapp:1.0 .
- List Docker images:
docker image ls
- Run the Docker container:
docker run -p 5000:8080 <imageid>
- Check running containers:
docker ps -a
The application will now be accessible at http://localhost:5000.
Create a file named docker-compose.yml
with the following content:
version: '3'
services:
web:
build: .
ports:
- "3000:5000"
db:
image: "mysql"
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- db-data:/foo
volumes:
db-data:
- Start the services:
docker compose up
- Stop and remove the services:
docker compose down
- Access the Node.js application at: http://localhost:3000.
- The application responds with:
{ "message": "Docker is easy π³" }
To clean up all resources:
- Stop and remove containers:
docker compose down
- Remove unused Docker images:
docker image prune
This project demonstrates how to containerize a simple Node.js application and manage it using Docker and Docker Compose. The setup ensures an isolated and portable development environment for your application.
This project was guided by Navin reddy and Telusko, who provided valuable mentorship throughout the process.