Skip to content

Commit 18eba83

Browse files
committed
feat(docker): multi_container_application
1 parent 078c52a commit 18eba83

File tree

11 files changed

+501
-1
lines changed

11 files changed

+501
-1
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
name: Build and Deploy Todo API
2+
3+
on:
4+
push:
5+
branches: [main]
6+
workflow_dispatch:
7+
8+
env:
9+
REGISTRY: ghcr.io
10+
IMAGE_NAME: ${{ github.repository }}
11+
12+
jobs:
13+
build-and-push:
14+
runs-on: ubuntu-latest
15+
permissions:
16+
contents: read
17+
packages: write
18+
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v4
22+
23+
- name: Log in to GitHub Container Registry
24+
uses: docker/login-action@v3
25+
with:
26+
registry: ${{ env.REGISTRY }}
27+
username: ${{ github.actor }}
28+
password: ${{ secrets.GITHUB_TOKEN }}
29+
30+
- name: Extract metadata for Docker
31+
id: meta
32+
uses: docker/metadata-action@v5
33+
with:
34+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
35+
tags: |
36+
type=sha,prefix={{branch}}-
37+
type=raw,value=latest,enable={{is_default_branch}}
38+
39+
- name: Build and push Docker image
40+
uses: docker/build-push-action@v5
41+
with:
42+
context: .
43+
push: true
44+
tags: ${{ steps.meta.outputs.tags }}
45+
labels: ${{ steps.meta.outputs.labels }}
46+
47+
deploy:
48+
needs: build-and-push
49+
runs-on: ubuntu-latest
50+
51+
steps:
52+
- name: Checkout code
53+
uses: actions/checkout@v4
54+
55+
- name: Deploy to remote server
56+
uses: appleboy/ssh-action@v1.0.3
57+
with:
58+
host: ${{ secrets.SERVER_HOST }}
59+
username: ${{ secrets.SERVER_USER }}
60+
key: ${{ secrets.SSH_PRIVATE_KEY }}
61+
script: |
62+
# Create project directory
63+
mkdir -p ~/todo-app
64+
cd ~/todo-app
65+
66+
# Login to GitHub Container Registry
67+
echo ${{ secrets.GITHUB_TOKEN }} | docker login ${{ env.REGISTRY }} -u ${{ github.actor }} --password-stdin
68+
69+
# Stop and remove existing containers
70+
docker-compose down || true
71+
72+
# Pull latest image
73+
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
74+
75+
# Create docker-compose.yml
76+
cat > docker-compose.yml << 'EOF'
77+
version: '3.8'
78+
services:
79+
mongodb:
80+
image: mongo:7-jammy
81+
container_name: todo-mongodb
82+
restart: unless-stopped
83+
environment:
84+
- MONGO_INITDB_DATABASE=todos
85+
volumes:
86+
- mongodb_data:/data/db
87+
networks:
88+
- todo-network
89+
healthcheck:
90+
test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
91+
interval: 10s
92+
timeout: 5s
93+
retries: 5
94+
95+
api:
96+
image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
97+
container_name: todo-api
98+
restart: unless-stopped
99+
ports:
100+
- "3000:3000"
101+
environment:
102+
- PORT=3000
103+
- MONGODB_URI=mongodb://mongodb:27017/todos
104+
depends_on:
105+
mongodb:
106+
condition: service_healthy
107+
networks:
108+
- todo-network
109+
110+
nginx:
111+
image: nginx:alpine
112+
container_name: todo-nginx
113+
restart: unless-stopped
114+
ports:
115+
- "80:80"
116+
volumes:
117+
- ./nginx.conf:/etc/nginx/nginx.conf:ro
118+
depends_on:
119+
- api
120+
networks:
121+
- todo-network
122+
123+
volumes:
124+
mongodb_data:
125+
driver: local
126+
127+
networks:
128+
todo-network:
129+
driver: bridge
130+
EOF
131+
132+
# Create nginx.conf
133+
cat > nginx.conf << 'EOF'
134+
events {
135+
worker_connections 1024;
136+
}
137+
138+
http {
139+
upstream api {
140+
server api:3000;
141+
}
142+
143+
server {
144+
listen 80;
145+
server_name _;
146+
147+
client_max_body_size 10M;
148+
149+
location / {
150+
proxy_pass http://api;
151+
proxy_http_version 1.1;
152+
proxy_set_header Upgrade $http_upgrade;
153+
proxy_set_header Connection 'upgrade';
154+
proxy_set_header Host $host;
155+
proxy_set_header X-Real-IP $remote_addr;
156+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
157+
proxy_set_header X-Forwarded-Proto $scheme;
158+
proxy_cache_bypass $http_upgrade;
159+
}
160+
161+
location /health {
162+
access_log off;
163+
return 200 "healthy\n";
164+
add_header Content-Type text/plain;
165+
}
166+
}
167+
}
168+
EOF
169+
170+
# Start containers
171+
docker-compose up -d
172+
173+
# Clean up old images
174+
docker image prune -af

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ terraform.log
1717
*.log
1818

1919
node_modules
20+
npm-debug.log
2021
.env
22+
.DS_Store

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Devops Projects
1414
- ✅ 13. IaC on DigitalOcean Task from https://roadmap.sh/projects/iac-digitalocean
1515
- ✅ 14. Node.js Service Deployment Task from https://roadmap.sh/projects/nodejs-service-deployment
1616
- ✅ 15. Dockerized Service Task from https://roadmap.sh/projects/dockerized-service-deployment
17-
- 16. Multi-Container Application Task from https://roadmap.sh/projects/multi-container-service
17+
- 16. Multi-Container Application Task from https://roadmap.sh/projects/multi-container-service
1818
- 17. Automated DB Backups Task from https://roadmap.sh/projects/automated-backups
1919
- 18. Bastion Host Task from https://roadmap.sh/projects/bastion-host
2020
- 19. File Integrity Checker Task from https://roadmap.sh/projects/file-integrity-checker
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
node_modules
2+
npm-debug.log
3+
.env
4+
.env.*
5+
.git
6+
.gitignore
7+
README.md
8+
.DS_Store
9+
*.md
10+
docker-compose.yml
11+
nginx.conf
12+
test-api.sh
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM node:20-alpine
2+
3+
WORKDIR /app
4+
5+
# Copy package files
6+
COPY package*.json ./
7+
8+
# Install dependencies
9+
RUN npm install
10+
11+
# Copy application code
12+
COPY index.js ./
13+
14+
# Expose port
15+
EXPOSE 3000
16+
17+
# Run as non-root user
18+
USER node
19+
20+
CMD ["node", "index.js"]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- ✅ 16. Multi-Container Application Task from https://roadmap.sh/projects/multi-container-service
2+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
version: '3.8'
2+
3+
services:
4+
# MongoDB Database
5+
mongodb:
6+
image: mongo:7-jammy
7+
container_name: todo-mongodb
8+
restart: unless-stopped
9+
environment:
10+
- MONGO_INITDB_DATABASE=todos
11+
volumes:
12+
- mongodb_data:/data/db
13+
networks:
14+
- todo-network
15+
healthcheck:
16+
test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
17+
interval: 10s
18+
timeout: 5s
19+
retries: 5
20+
21+
# Node.js API
22+
api:
23+
build: .
24+
container_name: todo-api
25+
restart: unless-stopped
26+
ports:
27+
- "3000:3000"
28+
environment:
29+
- PORT=3000
30+
- MONGODB_URI=mongodb://mongodb:27017/todos
31+
depends_on:
32+
mongodb:
33+
condition: service_healthy
34+
networks:
35+
- todo-network
36+
37+
# Nginx Reverse Proxy
38+
nginx:
39+
image: nginx:alpine
40+
container_name: todo-nginx
41+
restart: unless-stopped
42+
ports:
43+
- "80:80"
44+
volumes:
45+
- ./nginx.conf:/etc/nginx/nginx.conf:ro
46+
depends_on:
47+
- api
48+
networks:
49+
- todo-network
50+
51+
volumes:
52+
mongodb_data:
53+
driver: local
54+
55+
networks:
56+
todo-network:
57+
driver: bridge

0 commit comments

Comments
 (0)