ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ณธ ํฌ์ŠคํŒ…์€ Udemy 'Docker&Kubernetes:์‹ค์ „๊ฐ€์ด๋“œ-2022๋…„ํŒ' ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

 

 

Utility Container๋ž€?

  • Application Container(Environment, My App) → docker run myapp→
  • Utility Container(Environment) → docker run mynpm init →

์ง€๊ธˆ๊นŒ์ง€ Application์„ ๋„์ปค๋กœ ์‹คํ–‰์‹œ์ผฐ๋‹ค๋ฉด, Utility Container๋Š” ํŠน์ • ํ™˜๊ฒฝ๋งŒ ํฌํ•จํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ์ด๋‹ค.

 

Utility Container ์‚ฌ์šฉ ์ด์œ 

๋กœ์ปฌ ์‹œ์Šคํ…œ์— ๋…ธ๋“œ๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ

๋…ธ๋“œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค๋ ค๋ฉด package.json ์ข…์†์„ฑ ํŒŒ์ผ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

  • ๋ฐฉ๋ฒ•1 : ์ง์ ‘ ์ž‘์„ฑ
  • ๋ฐฉ๋ฒ•2 : npm init → package.json ํŒŒ์ผ ์ƒ์„ฑ
    • npm init์€ node.js๊ฐ€ ๋กœ์ปฌ์— ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ๋ช…๋ น์–ด ์‹คํ–‰ ๊ฐ€๋Šฅ

 

Container์—์„œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•

docker run -it node

  • -it : interactive mode

docker run -it -d node

  • -d : detach mode

docker exec -it (์ปจํ…Œ์ด๋„ˆ๋ช…) npm init : ์‹คํ–‰ ์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ์— ํŠน์ •(์ถ”๊ฐ€) ๋ช…๋ น ์‹คํ–‰ ๊ฐ€๋Šฅ

  • exec๋Š” ์ธํ„ฐ๋ ‰ํ‹ฐ๋ธŒ ๋ชจ๋“œ(-it)์—์„œ๋งŒ ์‹œ์ž‘ ๊ฐ€๋Šฅ
  • ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์— npm init ์‹คํ–‰
  • ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ค‘์ง€ํ•˜์ง€ ์•Š๊ณ  ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์— ์ž‘์„ฑ๋œ ํŒŒ์ผ์„ ์ฝ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์œ ์šฉ

 

docker run -it node npm init

  • npm init ์‹คํ–‰
  • npm init ์™„๋ฃŒ๋˜๋ฉด ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ค‘์ง€๋˜๋ฏ€๋กœ ๋ณ„๋กœ ์•ˆ์œ ์šฉ

 

Utility Container ๊ตฌ์ถ•

Utility Container ๊ตฌ์ถ• ์‹œ, ์ž์ฒด ์ด๋ฏธ์ง€๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ Dockerfile ์ถ”๊ฐ€ํ•˜๊ธฐ.

FROM node:14-alpine
# alpine : ๊ฒฝ๋Ÿ‰ ๋ฒ„์ „, slim

WORKDIR /app

docker build -t node-util .

  • -t : ํƒœ๊ทธ, default:latest

docker run -it -v (๋ฐ”์ธ๋“œ๋งˆ์šดํŠธ ์ ˆ๋Œ€๊ฒฝ๋กœ):/app node-util npm init

  • ๋ถ€๊ฐ€์ ์ธ ๋„๊ตฌ(node.js) ์„ค์น˜ํ•  ํ•„์š” ์—†๋‹ค๋Š” ์žฅ์ 
  • ์ปจํ…Œ์ด๋„ˆ์—์„œ ์ƒ์„ฑํ•œ WORKDIR ์ธ app ํด๋”๋ฅผ ๋กœ์ปฌ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
  • ์ฆ‰, ๋กœ์ปฌ ์‹œ์Šคํ…œ์— ๋…ธ๋“œ๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ

 

ENTRYPOINT ํ™œ์šฉ

FROM node:14-alpine
# alpine : ๊ฒฝ๋Ÿ‰ ๋ฒ„์ „, slim

WORKDIR /app

ENTRYPOINT [ 'npm' ]

docker run -it -v (๋ฐ”์ธ๋“œ๋งˆ์šดํŠธ ์ ˆ๋Œ€๊ฒฝ๋กœ):/app node-util npm intall

 

ENTRYPOINT๋Š” CMD ๋ช…๋ น์–ด์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ ๋‹ค๋ฅด๋‹ค.

  • CMD ์‚ฌ์šฉ ๊ฒฝ์šฐ : docker run ๋’ค์— ์ด๋ฏธ์ง€ ์ด๋ฆ„ ๋’ค์—์˜ ๋ช…๋ น์–ด(npm install)๋งŒ ๋ฎ์–ด์“ด๋‹ค.
  • ENTRYPOINT ์‚ฌ์šฉ ๊ฒฝ์šฐ : docker run ๋’ค์˜ ๋ช…๋ น์–ด(-it -v (๋ฐ”์ธ๋“œ๋งˆ์šดํŠธ ์ ˆ๋Œ€๊ฒฝ๋กœ):/app node-util npm intall)๋ฅผ ๋ฎ์–ด์“ด๋‹ค.

docker build -t mynpm .

 

docker run -it -v (๋ฐ”์ธ๋“œ๋งˆ์šดํŠธ ์ ˆ๋Œ€๊ฒฝ๋กœ):/app mynpm init

  • npm init ์„ค์ • ์งˆ๋ฌธ์ด ๋‚˜์˜ค๊ณ  package.json ํŒŒ์ผ ์ƒ์„ฑ๋จ
  • ๋ฐ”๋กœ ์ข…๋ฃŒ๋จ

 

docker run -it -v (๋ฐ”์ธ๋“œ๋งˆ์šดํŠธ ์ ˆ๋Œ€๊ฒฝ๋กœ):/app mynpm install express —save

  • express install ํ›„ package-lock.json ํŒŒ์ผ ์ƒ์„ฑ. ๋กœ์ปฌ์— ์ €์žฅ๋จ. ๋ฐ”๋กœ ์ข…๋ฃŒ๋จ

 

๋‹จ์ ์€? ๋ช…๋ น์–ด๊ฐ€ ๋„ˆ๋ฌด ๊น€

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•? docker-compose

 

 

Docker Compose ์‚ฌ์šฉ

docker-compose.yaml ์ถ”๊ฐ€

version: "3.8"
services:
	npm:
		build: ./
		stdin_open: true
		tty: true
		volumes:
			- ./:/app

docker-compose run —rm npm init

  • package.json ์ƒ์„ฑ
  • —rm : ์ปจํ…Œ์ด๋„ˆ ์ค‘์ง€๋˜๋ฉด ์ž๋™ ์‚ญ์ œ. ์‚ฌ์šฉ ์•ˆํ•˜๋ฉด ์ค‘์ง€๋œ์ฑ„๋กœ ์Œ“์ด๊ณ  ์‚ญ์ œ๋Š” ์•ˆ๋œ๋‹ค.
๋Œ“๊ธ€