Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/iambotcoder/my-first-docker-application
https://github.com/iambotcoder/my-first-docker-application
Last synced: 6 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/iambotcoder/my-first-docker-application
- Owner: iambotcoder
- Created: 2025-01-24T08:00:48.000Z (18 days ago)
- Default Branch: master
- Last Pushed: 2025-01-24T08:55:21.000Z (18 days ago)
- Last Synced: 2025-01-24T09:22:23.795Z (18 days ago)
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# π My First Docker Application
---## π Overview
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.
---
## π Table of Contents
- [Prerequisites](#prerequisites) π
- [Architecture](#Architecture)
- [Architecture ](#setup-and-installation) πΊοΈ
- [Docker Setup](#docker-setup) π³
- [Docker Compose Setup](#docker-compose-setup) π
- [Accessing the Application](#accessing-the-application) π
- [Cleaning Up Resources](#cleaning-up-resources) π§Ή
- [Conclusion](#conclusion) β---
## π Prerequisites
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 π§βπ»---
## πΊοΈ Architecture
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.### Workflow:
- 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.
## π οΈ Setup & Installation### 1β£ Setup Node Project
1. Initialize a Node.js project:
```bash
npm init -y
```
2. Install dependencies and start the project:
```bash
npm install
npm start
```
3. Create an `index.js` file with the following content:
```javascript
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}`));
```
## π³ Docker Setup### 2β£ Create a Dockerfile
Create a file named `Dockerfile` in your project directory:
```dockerfile
FROM node:12WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
ENV PORT=5000
EXPOSE 5000
CMD [ "npm", "start" ]
```### 3β£ Build and Run the Docker Image
1. Build the Docker image:
```bash
docker build -t iambot/myfirstnodeapp:1.0 .
```
2. List Docker images:
```bash
docker image ls
```
3. Run the Docker container:
```bash
docker run -p 5000:8080
```
4. Check running containers:
```bash
docker ps -a
```The application will now be accessible at [http://localhost:5000](http://localhost:5000).
### π Docker Compose Setup### 4β£ Create a `docker-compose.yml`
Create a file named `docker-compose.yml` with the following content:
```yaml
version: '3'
services:
web:
build: .
ports:
- "3000:5000"
db:
image: "mysql"
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- db-data:/foovolumes:
db-data:
```### 5β£ Run the Application with Docker Compose
1. Start the services:
```bash
docker compose up
```
2. Stop and remove the services:
```bash
docker compose down
```
### π‘οΈ Accessing the Application- Access the Node.js application at: [http://localhost:3000](http://localhost:3000).
- The application responds with:
```json
{ "message": "Docker is easy π³" }
```
### πΉ Cleaning Up ResourcesTo clean up all resources:
1. Stop and remove containers:
```bash
docker compose down
```
2. Remove unused Docker images:
```bash
docker image prune
```
---## β Conclusion
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.
---
## π¨βπ« Instructor
This project was guided by ***Navin reddy*** and ***Telusko***, who provided valuable mentorship throughout the process.