๐Ÿณ Docker compose๋ฅผ ์ด์šฉํ•œ multi-container ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๐Ÿณ

Kim Dae Hyunยท2021๋…„ 6์›” 22์ผ
0

Docker

๋ชฉ๋ก ๋ณด๊ธฐ
3/7
post-thumbnail
post-custom-banner

Github ์†Œ์Šค์ฝ”๋“œ

๐Ÿ”Ž ์ด๋Ÿฐ๊ฑฐ ํ•ด๋ณผ ๊ฑฐ์—์š”

1. redis DB๋ฅผ ์ด์šฉํ•œ ๊ฐ„๋‹จํ•œ node app ์ž‘์„ฑ

2. redis DB์™€ node app์„ ํ•œ ๊ฐœ ์ด๋ฏธ์ง€๋กœ ๋นŒ๋“œ


1. redis DB๋ฅผ ์ด์šฉํ•œ ๊ฐ„๋‹จํ•œ node app ์ž‘์„ฑ

๋งŒ๋“ค๊ณ ์ž ํ•˜๋Š” ๊ฐ„๋‹จํ•œ node app์€ ์ƒˆ๋กœ๊ณ ์นจ์„ ํ•  ๋•Œ๋งˆ๋‹ค ํ˜น์€ '/'(๋ฃจํŠธ) ๊ฒฝ๋กœ๋กœ ์ ‘๊ทผํ•  ๋•Œ๋งˆ๋‹ค ์ˆซ์ž๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋Š” app์ž…๋‹ˆ๋‹ค.

  • ์‹ค์Šต์„ ์œ„ํ•ด ์–ต์ง€๋กœ DB๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„ ๊ตฌ๋™์‹œ 0์„ DB์— ์ €์žฅ์‹œํ‚ค๊ณ  '/' ๊ฒฝ๋กœ๋กœ GET ์š”์ฒญ์ด ์˜ฌ ๋•Œ๋งˆ๋‹ค DB์— ์ €์žฅ๋œ ์ˆซ์ž๋ฅผ 1 ์ฆ๊ฐ€์‹œํ‚ค๊ณ  ๋‹ค์‹œ ์ €์žฅ์‹œ์ผœ์ค๋‹ˆ๋‹ค.
  • Docker compose๊ฐ€ ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•˜๋Š” ์ด์œ ๋Š” app์„ ๋ชจ๋‘ ์ž‘์„ฑํ•˜๊ณ  docker๋กœ ๊ตฌ๋™์‹œ์ผœ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ์„ ๊ฑฐ์—์š”.

express์™€ redis๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ ๋‘ ๊ฐœ ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•ด์ค๋‹ˆ๋‹ค.

  • express: npm install express --save
  • redis: npm install redis --save

dockerํ™˜๊ฒฝ์—์„œ node app๊ณผ redis๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ์„ค์ •์„ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

[ index.js ]

const express = require("express");
const redis = require("redis");
const app = express();

// redis client ์„ค์ •
const client = redis.createClient({
  host: "redis-service",
  ports: 6379,
});
  • host ๋ถ€๋ถ„์€ redis๋ฅผ docker containerํ™˜๊ฒฝ์—์„œ ๊ตฌ๋™์‹œํ‚ฌ ๋•Œ ์ง€์ •ํ•˜๋Š” ์ด๋ฆ„์ž…๋‹ˆ๋‹ค. ์ผ๋‹จ ์ž„์˜๋Œ€๋กœ ์ง€์ •ํ•ด์ฃผ์„ธ์š”.
  • port๋Š” redis์˜ ๊ธฐ๋ณธํฌํŠธ๊ฐ€ 6379์ด๋ฏ€๋กœ 6379๋กœ ์„ค์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.
[ index.js ]

const port = 8080;

// ์„œ๋ฒ„ ๊ตฌ๋™์‹œ ์ฆ๊ฐ€๋  ์ˆซ์ž๋Š” 0์œผ๋กœ ์ดˆ๊ธฐํ™” (redisDB์— ์ €์žฅ)
// redis๋Š” key-value ์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
// key=num, value=0
client.set("num", 0);

app.get("/", (req, res) => {
  client.get("num", (err, number) => {
    // redis๋กœ๋ถ€ํ„ฐ key๊ฐ’์„ num์œผ๋กœ ํ•˜๋Š” ๊ฐ’์„ get
    res.send("Incresed number from redis : " + number); // redis๋กœ ๋ถ€ํ„ฐ getํ•œ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๋ธŒ๋ผ์šฐ์ €์— ์‘๋‹ต
    client.set("num", parseInt(number) + 1); // num์„ key๊ฐ’์œผ๋กœ ํ•˜๋Š” value์— +1 ๋งŒํผ ์ฆ๊ฐ€์‹œ์ผœ์„œ set(์ €์žฅ)
  });
});

app.listen(port);
console.log(`running on ${port}`);
  • ์ˆซ์ž๋ฅผ ์ดˆ๊ธฐํ™”์‹œํ‚ค๊ณ  ๊ฐ€์ ธ์˜ค๊ณ  ์ฆ๊ฐ€์‹œํ‚ค๊ณ  ๋‹ค์‹œ ์ €์žฅํ•˜๊ณ  ํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค.
  • redis๋ฅผ ๋ชฐ๋ผ๋„ key-value ์Œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ get, setํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์ดํ•ดํ•˜๋Š”๋ฐ ์–ด๋ ค์›€์ด ์—†์„ ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์œผ๋กœ ์ž‘์„ฑํ•œ node app์„ docker image๋กœ buildํ•˜๊ธฐ ์œ„ํ•ด Dockerfile์„ ์ž‘์„ฑํ•ด์ค๋‹ˆ๋‹ค.

[ Dockerfile ]

FROM node:10

WORKDIR /usr/src/app

COPY ./ ./

RUN npm install

CMD ["node", "index.js"]
  • base image๋กœ๋Š” node:10์„ ์‚ฌ์šฉํ•˜๊ณ  working directory๋ฅผ ์ง€์ •ํ•˜์—ฌ ํ•ด๋‹น container์˜ directory๋กœ ์ „์ฒด ํŒŒ์ผ์„ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.
  • ๋ณต์‚ฌ ํ›„์— ํ•„์š”ํ•œ ์ข…์†์„ฑ์„ ๋ฐ›๊ธฐ์œ„ํ•ด npm install์„ ์ˆ˜ํ–‰ํ•˜๊ณ  node app ๊ตฌ๋™์„ ์œ„ํ•ด node index.js๋ฅผ ์ˆ˜ํ–‰ํ•˜๋„๋ก ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

์—ฌ๊ธฐ๊นŒ์ง€๊ธฐ ์ €๋ฒˆ์— ํ–ˆ๋˜ docke๋ฅผ ์ด์šฉํ•œ ๊ฐ„๋‹จํ•œ node app๋งŒ๋“ค๊ธฐ ์ž…๋‹ˆ๋‹ค.
์šฐ๋ฆฌ๋Š” redis๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ•œ๊ฐ€์ง€ ์„ค์ •์„ ๋” ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

2. redis DB์™€ node app์„ ํ•œ ๊ฐœ ์ด๋ฏธ์ง€๋กœ ๋นŒ๋“œ

Docker compose ์„ค์ •

ํ•ด๋‹น ์„ค์ •์€ docker-compose.yml ์— ์ž‘์„ฑํ•ด์ค๋‹ˆ๋‹ค.

  • docker compose๋Š” ๋‘ ๊ฐœ ์„œ๋น„์Šค๋ฅผ ํ•œ ๊ฐœ ์ด๋ฏธ์ง€๋กœ ๋ฌถ์–ด์„œ buildํ•˜๋Š” ๊ฒƒ์„ ์ง€์›ํ•ด์ค๋‹ˆ๋‹ค.
  • ์šฐ๋ฆฌ๋Š” ์ž‘์„ฑํ•œ node app๊ณผ redis๋ฅผ ๋ชจ๋‘ docker ํ™˜๊ฒฝ์—์„œ ๊ตฌ๋™ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋ ‡๊ฒŒ ๊ฐ๊ฐ ๋‘ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ปจํ…Œ์ด๋„ˆ๋กœ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ปจํ…Œ์ด๋„ˆ ๊ฐ„ ํ†ต์‹ ์— ์–ด๋ ค์›€์ด ์ƒ๊น๋‹ˆ๋‹ค.
  • ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ docker compose๋Š” ๋™์ผํ•œ ์ปจํ…Œ์ด๋„ˆ ํ™˜๊ฒฝ์— ์—ฌ๋Ÿฌ๊ฐœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ตฌ๋™์‹œ์ผœ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค. (multi container)
  • ์•ž์„œ redis client ์ƒ์„ฑ์‹œ host๋กœ ์ง€์ •ํ•œ ๋ถ€๋ถ„๊ณผ services ํ•˜์œ„์— ์ƒ์„ฑ๋  ์ปจํ…Œ์ด๋„ˆ์˜ ์ด๋ฆ„๊ณผ ๋™์ผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
version: "3" # Docker-compose version
services: # ๋ฉ€ํ‹ฐ ์ปจํ…Œ์ด๋„ˆ ํ™˜๊ฒฝ์œผ๋กœ ๋ฌถ์„ ์ด๋ฏธ์ง€๋ฅผ ๋ช…์‹œ
  redis-service: # ์ƒ์„ฑ๋  ์ปจํ…Œ์ด๋„ˆ์˜ ์ด๋ฆ„
    image: "redis" # ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ์— ์‚ฌ์šฉ๋  ์ด๋ฏธ์ง€
  compose-app:
    build: . # ์ด๋ฏธ์ง€๋กœ ๋นŒ๋“œ์‹œ ํ•„์š”ํ•œ Dockerfile์ด ์žˆ๋Š” ๊ฒฝ๋กœ
    ports: # ํฌํŠธ ๋งคํ•‘
      - "8080:8080"
  • docker-compose up ์œผ๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ตฌ๋™์‹œ์ผœ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
  • ์ปจํ…Œ์ด๋„ˆ ์ข…๋ฃŒ : docker-compose down
  • compose๋ฅผ ํ†ตํ•ด ์ปจํ…Œ์ด๋„ˆ ๊ตฌ๋™์‹œ image๋ฅผ ์žฌ๋นŒ๋“œํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด docker-compose up --build ์„ ์ž…๋ ฅํ•˜์„ธ์š”.

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ใ…Ž

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€