Skip to content

Instantly share code, notes, and snippets.

@pjchender
Created June 20, 2021 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjchender/b08bd8e83c9ad4780e89580388072125 to your computer and use it in GitHub Desktop.
Save pjchender/b08bd8e83c9ad4780e89580388072125 to your computer and use it in GitHub Desktop.
Docker Compose Example
# 定義 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
FROM node:14-alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
CMD ["npm", "start"]
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');
});
@pjchender
Copy link
Author

This gist is learned from the course Docker and Kubernetes: The Complete Guide on Udemy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment