Skip to content

Commit 00de3b2

Browse files
authored
Merge pull request #5 from YosefKahlon/week5
Add Dockerized Node.js application with PostgreSQL and Slack notifier
2 parents 39cb276 + c0c1553 commit 00de3b2

12 files changed

Lines changed: 1077 additions & 0 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@
1616
# Part 5
1717
* [CI/CD with GitHub Action](./week5/CICD_with_GitHub_Actions.md)
1818

19+
20+
# Prat 6
21+
* [Docker-Containers](./week6/Docker-Containers.md)

week6/Docker-Containers.md

Lines changed: 805 additions & 0 deletions
Large diffs are not rendered by default.

week6/app/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use the latest Node.js image as the base image
2+
FROM node:latest
3+
4+
# Set the working directory inside the container
5+
WORKDIR /app
6+
7+
# Copy package.json and package-lock.json (if present)
8+
COPY package*.json ./
9+
10+
# Install the dependencies
11+
RUN npm install
12+
13+
# Copy the rest of the application code
14+
COPY . .
15+
16+
# Command to run the application
17+
CMD ["npm", "start"]
18+
19+
# HEALTHCHECK instruction to monitor container health
20+
HEALTHCHECK --interval=30s --timeout=10s --retries=3 CMD curl -f http://localhost:3000 || exit 1

week6/app/index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const { Client } = require('pg');
2+
const express = require('express');
3+
const morgan = require('morgan');
4+
5+
const app = express();
6+
7+
const connectionString = process.env.DATABASE_URL || 'postgres://myuser:mypassword@db:5432/mydb';
8+
9+
const client = new Client({
10+
connectionString: connectionString,
11+
});
12+
13+
// Use morgan for HTTP request logging
14+
app.use(morgan('combined'));
15+
16+
// Example route
17+
app.get('/', (req, res) => {
18+
res.send('Hello, World!');
19+
});
20+
21+
// Start the server
22+
const PORT = process.env.PORT || 3000;
23+
app.listen(PORT, () => {
24+
console.log(`Server is running on port ${PORT}`);
25+
});
26+
27+
async function checkDatabaseConnection() {
28+
console.log(`Attempting to connect to database...`);
29+
try {
30+
await client.connect();
31+
console.log('Successfully connected to PostgreSQL database!');
32+
// const res = await client.query('SELECT NOW()');
33+
// console.log('Current time from DB:', res.rows[0].now);
34+
} catch (err) {
35+
console.error('Error connecting to PostgreSQL database:', err.stack);
36+
process.exit(1);
37+
}
38+
// finally {
39+
// await client.end();
40+
// console.log('Database client disconnected.');
41+
// }
42+
}
43+
44+
checkDatabaseConnection();

week6/app/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "node-postgres-app",
3+
"version": "1.0.0",
4+
"description": "Node.js app connecting to PostgreSQL in Docker",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js"
8+
},
9+
"dependencies": {
10+
"pg": "^8.0.0",
11+
"express": "^4.18.2",
12+
"morgan": "^1.10.0"
13+
}
14+
}

week6/docker-compose.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
version: '3.8'
2+
3+
services:
4+
web:
5+
build: ./app
6+
ports:
7+
- "8082:3000" # Changed host port from 8080 to 8081
8+
environment:
9+
- DATABASE_URL=postgres://myuser:mypassword@db:5432/mydatabase
10+
# NODE_ENV: development # Example: if your app uses NODE_ENV
11+
depends_on:
12+
- db
13+
networks:
14+
- app-net
15+
16+
db:
17+
image: postgres:latest
18+
restart: always # Ensures the DB service attempts to restart if it fails
19+
environment:
20+
- POSTGRES_USER=myuser
21+
- POSTGRES_PASSWORD=mypassword
22+
- POSTGRES_DB=mydatabase
23+
volumes:
24+
- pgdata:/var/lib/postgresql/data # Persist database data in a named volume
25+
networks:
26+
- app-net
27+
ports: # Optional: Expose PostgreSQL port to host for external tools (e.g., pgAdmin)
28+
- "5432:5432" # Be cautious with exposing DB ports directly in production
29+
30+
slack-notifier:
31+
build: ./slack-notifier
32+
restart: always
33+
volumes:
34+
- /var/run/docker.sock:/var/run/docker.sock:ro # Mount Docker socket read-only
35+
environment:
36+
SLACK_WEBHOOK_URL: "https://hooks.slack.com/services/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"
37+
logging: # Optional: configure logging for the notifier itself
38+
driver: "json-file"
39+
options:
40+
max-size: "10m"
41+
max-file: "3"
42+
43+
networks:
44+
app-net:
45+
driver: bridge
46+
47+
volumes:
48+
pgdata: # Defines the named volume for data persistence
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
# Exclude node_modules, as dependencies will be installed inside the container
3+
node_modules
4+
npm-debug.log
5+
6+
# Exclude build artifacts or local development files
7+
build
8+
dist
9+
.env
10+
11+
# Exclude version control directories and files
12+
.git
13+
.gitignore
14+
.gitattributes
15+
16+
# Exclude Docker related files if they are in the context but not needed in image
17+
Dockerfile
18+
.dockerignore
19+
20+
# Exclude OS-specific files
21+
.DS_Store
22+
Thumbs.db

week6/hello-docker-app/Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use the latest Node.js image as the base image
2+
FROM node:latest
3+
4+
5+
# Set the working directory inside the container
6+
WORKDIR /app
7+
8+
9+
# Copy package.json and package-lock.json to the working directory
10+
COPY package*.json ./
11+
12+
13+
# Install the dependencies
14+
RUN npm install
15+
16+
17+
# Copy the rest of the application code to the working directory
18+
COPY . .
19+
20+
21+
# Expose the port the app runs on
22+
EXPOSE 3000
23+
24+
25+
26+
# Command to run the application
27+
CMD ["npm", "start"]

week6/hello-docker-app/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
4+
console.log("Hello from Docker");
5+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "hello-docker-app",
3+
"version": "1.0.0",
4+
"description": "Simple Node.js app for Docker",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js"
8+
},
9+
"author": "",
10+
"license": "ISC"
11+
}

0 commit comments

Comments
 (0)