Created
June 20, 2021 07:03
-
-
Save pjchender/b08bd8e83c9ad4780e89580388072125 to your computer and use it in GitHub Desktop.
Docker Compose Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 定義 docker-compose 使用的版本 | |
version: '3' | |
# 定義兩個 services,一個是 redis-server,另一個是 node-app | |
# 這兩個 serivces 不需要額外設定,預設就會在同一個 network 內,彼此就能溝通 | |
services: | |
redis-server: | |
image: 'redis' | |
node-app: | |
restart: always | |
build: . # 根據當前根目錄的 Dockerfile 執行 build | |
ports: | |
- '8081:8081' # 將 local 的 8081 對應到 container 內的 8081 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM node:14-alpine | |
WORKDIR /app | |
COPY package.json . | |
RUN npm install | |
COPY . . | |
CMD ["npm", "start"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const redis = require('redis'); | |
const process = require('process'); | |
const app = express(); | |
const client = redis.createClient({ | |
host: 'redis-server', // 原本可能會填的是 localhost:6379 | |
port: 6379, | |
}); | |
client.set('visits', 0); | |
app.get('/', (req, res) => { | |
process.exit(0); | |
client.get('visits', (err, visits) => { | |
res.send(`Number of visits is ${visits}`); | |
client.set('visits', parseInt(visits, 10) + 1); | |
}); | |
}); | |
app.listen(8081, () => { | |
console.log('Listening on port 8081'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is learned from the course Docker and Kubernetes: The Complete Guide on Udemy.