๐Ÿณ Docker๋ฅผ ์ด์šฉํ•œ ๊ฐ„๋‹จํ•œ node app ๋งŒ๋“ค๊ธฐ ๐Ÿณ

Kim Dae Hyunยท2021๋…„ 6์›” 17์ผ
2

Docker

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

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

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

1. ๊ฐ„๋‹จํ•œ node app ์ž‘์„ฑ

2. ์ž‘์„ฑํ•œ node app์„ docker image๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ Dockerfile ์ž‘์„ฑ

3. Docker Image ์ƒ์„ฑ

4. ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€์—์„œ ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ ๋ฐ ๊ตฌ๋™ ํ›„ ์ ‘์†

๊ฐ„๋‹จํ•œ Docker ๋ช…๋ น์–ด๋Š” ์ด์ „ ํฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š” !


1. ๊ฐ„๋‹จํ•œ node app ์ž‘์„ฑ

node.js๋ฅผ ์‹ค์Šตํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•œ๊ฒŒ ์•„๋‹ˆ๋‹ˆ ์ •๋ง ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค์–ด๋ณผ๊ฒŒ์š”.
(node์™€ npm์ด ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค!)

  • npm init (node ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ)
  • express ์„ค์น˜ (npm install express --save)
    • dockerํ™˜๊ฒฝ์—์„œ ๊ตฌ๋™ํ•˜๊ธฐ ์œ„ํ•ด package.json์— dependency์ •๋ณด๊ฐ€ ์ €์žฅ๋˜์–ด์•ผ ํ•ด์š”. ๊ผญ --save ์˜ต์…˜์„ ์จ์ฃผ์„ธ์š”.
[ index.js ]

const express = require("express");

const port = "8080";

const app = express();

app.get("/", (req, res) => {
  res.send("hello world");
});

app.listen(port);
console.log(`server running on ${port}`);
  • ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ํ•„์š”๋„ ์—†๊ฒ ์ง€๋งŒ ์ผ๋‹จ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค.
    • ์„œ๋ฒ„ On : node index.js

2. ์ž‘์„ฑํ•œ node app์„ docker image๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ Dockerfile ์ž‘์„ฑ

Docker image ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด Docker image ์˜ ์„ค์ •์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” Dockerfile ์„ ์ž‘์„ฑํ•ด์ค๋‹ˆ๋‹ค.

  • npm init์„ ํ†ตํ•ด ์ž‘์„ฑํ•œ ํ”„๋กœ์ ํŠธ์˜ root๊ฒฝ๋กœ์— Dockerfile ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
[ Dockerfile ]

# base image (npm์„ ๊ฐ–๊ณ  ์žˆ๋Š” baseimage ์ค‘ ํ•˜๋‚˜)
FROM node:10  

# Working Directory ์ง€์ •
WORKDIR /usr/src/app

# COPY package.json ./
COPY ./ ./

# node์˜ ์ข…์†์„ฑ ๋‹ค์šด๋กœ๋“œ
RUN npm install

CMD ["node", "index.js"]
  • FROM node:10
    • ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๊ธฐ ์ „ base image๋ฅผ ํ†ตํ•ด ์ž„์‹œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ƒ์„ฑ๋œ ์ž„์‹œ ์ปจํ…Œ์ด๋„ˆ๋กœ ๋ถ€ํ„ฐ ์ƒ์„ฑํ•˜๊ณ ์ž ํ•˜๋Š” ์ด๋ฏธ์ง€๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.
    • ์šฐ๋ฆฌ๋Š” npm ๋ช…๋ น์–ด๋ฅผ ํ•„์š”๋กœ ํ•˜๊ธฐ์— npm๋ช…๋ น์–ด๋ฅผ ํฌํ•จํ•˜๋Š” node:10 ์ด๋ฏธ์ง€๋ฅผ base image๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • WORKDIR /usr/src/app
    • working directory๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.
    • ์•„๋ž˜ COPY ๋ถ€๋ถ„์—์„œ ํ•„์š”๋กœ ํ•˜๋Š” ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ปจํ…Œ์ด๋„ˆ๋กœ ๋ณต์‚ฌํ•ด์˜ค๋Š”๋ฐ working directory๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์ปจํ…Œ์ด๋„ˆ์˜ ๋ฃจํŠธ ๊ฒฝ๋กœ์— ํด๋” ์—†์ด ์ง€์ €๋ถ„ํ•˜๊ฒŒ ๋ณต์‚ฌ๋˜์–ด ์ข‹์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • ๋ณด๊ธฐ ์‹ซ์€ ๊ฒƒ๋„ ์žˆ์ง€๋งŒ COPY์‹œ ๋ฃจํŠธ ๊ฒฝ๋กœ์— COPY๋˜๋Š” ํŒŒ์ผ๊ณผ ๋™์ผํ•œ ํด๋” ํ˜น์ธ ํŒŒ์ผ์ด ์žˆ๋‹ค๋ฉด ๋ฎ์–ด์“ฐ๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.. ์•„๋ฌดํŠผ ์œ„ํ—˜ํ•˜๋‹ˆ working directory์— ์›ํ•˜๋Š” ํŒŒ์ผ์ด ๋ณต์‚ฌ๋  ์ˆ˜ ์žˆ๋„๋ก ์ง€์ •ํ•ด์ค์‹œ๋‹ค.
  • COPY ./ ./
    • COPY์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” ์ปจํ…Œ์ด๋„ˆ๋กœ ๋ณต์‚ฌํ•˜๊ณ ์ž ํ•˜๋Š” ํŒŒ์ผ ํ˜น์€ ํด๋”์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ./๋Š” ํ”„๋กœ์ ํŠธ์˜ ๋ฃจํŠธ ๊ฒฝ๋กœ์— ์žˆ๋Š” ๋ชจ๋“  ํด๋” ๋ฐ ํŒŒ์ผ์„ ๋ณต์‚ฌํ•œ๋‹ค๋Š” ์˜๋ฏธ๊ฒ ์ฃ ?
    • COPY์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ์ปจํ…Œ์ด๋„ˆ์— ๋ณต์‚ฌ๋˜๋Š” ๊ฒฝ๋กœ๋กœ ์•Œ๊ณ  ์žˆ๋Š”๋ฐ, ์‹คํ–‰ํ•ด๋ณธ ๊ฒฐ๊ณผ working directory๋ฅผ ์ง€์ •ํ•˜๋ฉด ์ง€์ •ํ•œ ๊ฒฝ๋กœ๋กœ ๋ณต์‚ฌ๋ฉ๋‹ˆ๋‹ค.
  • RUN npm install
    • ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ ์ „ ์ˆ˜ํ–‰๋  ๋ช…๋ น์–ด๋ฅผ ์ ์–ด์ค๋‹ˆ๋‹ค.
    • ์šฐ๋ฆฌ๋Š” ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ ์ „์— ํ•„์š”ํ•œ ์ข…์†์„ฑ์„ ๋ฐ›์•„์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • ํ˜„์žฌ๋Š” express ๋ฅผ ๋ฐ›์•„์™€์•ผ๊ฒ ์ฃ ??
    • node ๊ธฐ๋ฐ˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ npm install ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด pacakage.json์— ๋ช…์‹œ๋œ ์ข…์†์„ฑ์„ ๋‹ค์šด๋กœ๋“œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • CMD ["node", "index.js"]
    • ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ ํ›„์— ์ˆ˜ํ–‰ํ•  ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.
    • ์šฐ๋ฆฌ๋Š” index.js๋ฅผ ์‹คํ–‰์‹œ์ผœ ์„œ๋ฒ„๋ฅผ ๊ตฌ๋™์‹œํ‚ฌ ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

3. Docker Image ์ƒ์„ฑ

์ž‘์„ฑํ•œ Dockerfile ์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ docker build ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

  • docker build -t zbeld123/node-app ./
    • -t ์˜ต์…˜: ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€์— ์ด๋ฆ„์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ๊ด€๋ก€๋กœ "dockerID/ํ”„๋กœ์ ํŠธ์ด๋ฆ„" ๋กœ ์ง€์ •ํ•ด์ค€๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
    • -t ์˜ต์…˜์„ ๋นผ๊ณ  ํ•œ ๋ฒˆ ๋ณด์ฃ 
      - docker build ./ ํ˜„์žฌ ํ”„๋กœ์ ํŠธ๋ฅผ docker์˜ ์ด๋ฏธ์ง€๋กœ ๋นŒ๋“œํ•œ๋‹ค ? ์ •๋„๋กœ ์ดํ•ดํ•ด๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€๋ฅผ ํ™•์ธ
    • -t ์˜ต์…˜์œผ๋กœ ์ง€์ •ํ•œ ์ด๋ฆ„์œผ๋กœ ์ด๋ฏธ์ง€๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ๊ณ  base image๋กœ ์ง€์ •ํ•œ node๋„ ์ด๋ฏธ์ง€๋กœ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

4. ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€์—์„œ ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ ๋ฐ ๊ตฌ๋™ ํ›„ ์ ‘์†

์ด์ œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•ด์„œ ์„œ๋ฒ„๋ฅผ ๊ตฌ๋™์‹œํ‚ค๋ฉด ๋˜๋Š”๋ฐ ํ•œ๊ฐ€์ง€ ์„ค์ •ํ•ด์ค˜์•ผ ํ•˜๋Š”๊ฒŒ ์žˆ์Šต๋‹ˆ๋‹ค.
์™œ ์„ค์ •์ด ๋” ํ•„์š”ํ•œ ๊ฒƒ์ธ์ง€ ์ผ๋‹จ ์‹คํ–‰์‹œ์ผœ๋ณด๊ณ  ์•Œ์•„๋ด…์‹œ๋‹ค.

  • docker run zbeld123/node-app ์œผ๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ตฌ๋™์‹œํ‚จ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.
    • ์ฝ˜์†” ๋กœ๊ทธ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ฐํžŒ ๊ฒƒ์„ ๋ณด์•„ ์ œ๋Œ€๋กœ ์‹คํ–‰๋œ ๊ฒƒ ๊ฐ™์ง€๋งŒ ์‚ฌ์‹ค ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.. 8080 ํฌํŠธ๋กœ ํ•œ ๋ฒˆ ์ ‘์†ํ•ด๋ณด์ฃ .
    • ์ ‘์†์ด ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๊ฐ€์ง€ ์„ค์ •์„ ๋” ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

ํฌํŠธ ์„ค์ •

์ปจํ…Œ์ด๋„ˆ ํ™˜๊ฒฝ์—์„œ์˜ port์™€ ์ปจํ…Œ์ด๋„ˆ ์™ธ๋ถ€์—์„œ ์ ‘์†ํ•˜๋Š” port๋ฅผ ๋งคํ•‘์‹œ์ผœ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • ์šฐ๋ฆฌ๊ฐ€ node app์—์„œ ์„ค์ •ํ•œ port๋Š” ์ปจํ…Œ์ด๋„ˆ ํ™˜๊ฒฝ์—์„œ์˜ port์ž…๋‹ˆ๋‹ค.
  • ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ํ†ตํ•ด ์ ‘์†ํ•˜๋Š” ๊ฒƒ์€ ์ปจํ…Œ์ด๋„ˆ ์™ธ๋ถ€์—์„œ ์ ‘์†ํ•˜๋Š” ๊ฒƒ์ด๊ธฐ์— port ์ •๋ณด๋ฅผ ๋งคํ•‘ ์‹œ์ผœ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด ์šฐ๋ฆฌ๊ฐ€ ๋ธŒ๋ผ์šฐ์ €์—์„œ 5000๋ฒˆ ํฌํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ ‘์†ํ•œ๋‹ค๋ฉด 5000๋ฒˆ ํฌํŠธ๋ฅผ ์ปจํ…Œ์ด๋„ˆ์˜ ํฌํŠธ๋ฒˆํ˜ธ์ธ 8080ํฌํŠธ๋กœ ๋งคํ•‘์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ๋งคํ•‘ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • docker run -p [์ ‘์†ํ•  ํฌํŠธ]:[์ปจํ…Œ์ด๋„ˆ์˜ ํฌํŠธ] zbeld123/node-app
    • -p ์˜ต์…˜์„ ์ด์šฉํ•˜์—ฌ ๋งคํ•‘์ •๋ณด๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • -p ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ตฌ๋™์‹œ์ผœ๋ด…์‹œ๋‹ค!
    • -p 5000:8000 ์œผ๋กœ ์„ค์ •ํ•˜์˜€์œผ๋ฏ€๋กœ 5000๋ฒˆ ํฌํŠธ๋กœ ์ ‘๊ทผ์ด ์„ฑ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค !!!
  • -p ์˜ต์…˜์„ ์‚ฌ์šฉํ•  ๋•Œ : ์„ธ๋ฏธ์ฝœ๋ก ์˜ ์•ž ๋’ค๋ฅผ ๊ผญ ๋ถ™์—ฌ์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค ใ…Ž

Docker์˜ Hello world ํ€˜์ŠคํŠธ๋ฅผ ๊ฒจ์šฐ ์™„์ˆ˜ํ•œ ๋Š๋‚Œ..? ์ด๋„ค์š” ..! :)

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

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