๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
DevOps/Docker

[Docker] ๋„์ปค ์ด๋ฏธ์ง€ ๋งŒ๋“ค๊ธฐ2 (์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜)

by ํƒœ์˜น 2022. 1. 20.

๐Ÿ‘‡ ํ•ด๋‹น ํฌ์ŠคํŒ…์€ ์•„๋ž˜์˜ ๊ฐ•์˜๋ฅผ ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ๊ธฐ๋กํ•˜์˜€์Šต๋‹ˆ๋‹ค.

https://www.inflearn.com/course/%EB%8F%84%EC%BB%A4-%EC%9E%85%EB%AC%B8/dashboard

 

์ดˆ๋ณด๋ฅผ ์œ„ํ•œ ๋„์ปค ์•ˆ๋‚ด์„œ - ์ธํ”„๋Ÿฐ | ๊ฐ•์˜

๋„์ปค๋ฅผ 1๋„ ๋ชจ๋ฅด๋Š” ์ž…๋ฌธ์ž, ์ดˆ๋ณด์ž๋ถ„๋“ค์„ ์œ„ํ•œ ๋„์ปค ์•ˆ๋‚ด์„œ ์ž…๋‹ˆ๋‹ค. ๋ณต์žกํ•œ ๋‚ด์šฉ์„ ์ œ์™ธํ•˜๊ณ  ๋„์ปค๊ฐ€ ์™œ ์ธ๊ธฐ๊ฐ€ ๋งŽ๊ณ  ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ๋น ๋ฅด๊ฒŒ ์ตํž ์ˆ˜ ์žˆ๋„๋ก ์ง‘์ค‘ํ•˜์˜€์Šต๋‹ˆ๋‹ค., ๋ฐ๋ธŒ์˜ต์Šค ์ž…๋ฌธ

www.inflearn.com


 

1. node ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งŒ๋“ค๊ธฐ

 

์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” Nodejs ํ”„๋กœ๊ทธ๋žจ๊ณผ fastify ์›น ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ด๋ณด์ž.

 

๋จผ์ € fastify ํ™ˆํŽ˜์ด์ง€๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

https://www.fastify.io/

 

Fastify, Fast and low overhead web framework, for Node.js

Fast and low overhead web framework, for Node.js

www.fastify.io

 

(1) fastify ์„ค์น˜

$ npm init 
$ npm install fastify

npm์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € npm ์ดˆ๊ธฐํ™” ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค.

์ดˆ๊ธฐํ™” ํ›„ npm install fastify ๋ช…๋ น์–ด๋กœ ์„ค์น˜ํ•ด์ค€๋‹ค.

 

 

(2) app.js ํŒŒ์ผ ์ƒ์„ฑ

fastify๋ฅผ ์„ค์น˜ํ•ด์ค€ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์—ด์–ด๋ณด๋ฉด jsonํŒŒ์ผ๋“ค๊ณผ ๋ชจ๋“ˆ ํด๋”๊ฐ€ ์ƒ๊ธด ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ์—์„œ app.jsํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์—ฌ ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค.

// Require the framework and instantiate it
const fastify = require('fastify')({ logger: true })

// Declare a route
fastify.get('/', async (request, reply) => {
  return { hello: 'world' }
})

// Run the server!
const start = async () => {
  try {
    await fastify.listen(3000)
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

(์ด๊ฑฐ ๋‹ค fastify ํ™ˆํŽ˜์ด์ง€์— ๋‚˜์™€์žˆ๋Š” ์ฝ”๋“œ์ž„)

 

 

 

(3) app.js ํŒŒ์ผ ์‹คํ–‰

 

cmd์—์„œ app.jsํŒŒ์ผ์„ ์‹คํ–‰ํ•œ๋‹ค.

node app.js

๊ทธ๋Ÿผ ์•„๋ž˜์™€ ๊ฐ™์ด 3000๋ฒˆ ํฌํŠธ๋กœ ์›น ์„œ๋ฒ„๊ฐ€ ์—ฐ๊ฒฐ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

http://127.0.0.1:3000/ ์ ‘์†ํ•ด๋ณด๋ฉด ํŒŒ์ผ ๋‚ด์šฉ ๋ณด์ž„

 


2. ๋„์ปค ์ด๋ฏธ์ง€ ๋งŒ๋“ค๊ธฐ

 

(1) Dockerfile ๋งŒ๋“ค๊ธฐ

 

(๋„์ปค์—์„œ ์ž‘์—…์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์˜ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ด์•ผํ•œ๋‹ค.)

 

๊ฐ™์€ ๊ฒฝ๋กœ์— Dockerfile์„ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜์˜ ๋‚ด์šฉ์„ ๋ถ™์—ฌ์ค€๋‹ค.

# 1. node ์„ค์น˜
FROM    ubuntu:20.04
RUN     apt-get update
RUN     DEBIAN_FRONTEND=noninteractive apt-get install -y nodejs npm

# 2. Nodejs ํŒจํ‚ค์ง€ ์„ค์น˜
COPY    ./package* /usr/src/app/
WORKDIR /usr/src/app
RUN     npm install

# 3. ์†Œ์Šค ๋ณต์‚ฌ (๋‚ด ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” ๋ชจ๋“  ํŒŒ์ผ์„ ์•„๋ž˜์˜ ๊ฒฝ๋กœ์— ๋ณต์‚ฌ)
COPY    . /usr/src/app

# 4. WEB ์„œ๋ฒ„ ์‹คํ–‰ (Listen ํฌํŠธ ์ •์˜)
EXPOSE 3000
CMD    node app.js

์ตœ์ ํ™”!

๋”๋ณด๊ธฐ

1๋ฒˆ ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ตœ์ ํ™” ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ

# 1. node ์ด๋ฏธ์ง€ ์‚ฌ์šฉ
FROM    node:12-alpine

nodeํŒŒ์ผ ๋ง๊ณ  ์“ธ ๋ฐ ์—†๋Š” ํŒŒ์ผ๋“ค์€ ์ง€์›Œ๋ฒ„๋ฆฌ๋Š”? ๊ทธ๋Ÿฐ node์ด๋ฏธ์ง€๋ผ๊ณ  ํ•จ

์‹ค์ œ๋กœ ํ•ด๋‹น ์ฝ”๋“œ๋กœ ๋นŒ๋“œํ•˜๊ณ  docker images๋ช…๋ น์–ด๋กœ ํ™•์ธํ•ด๋ณด๋ฉด web ์ด๋ฏธ์ง€๊ฐ€ ์ฐจ์ง€ํ•˜๋Š” ์šฉ๋Ÿ‰์ด ํ™• ์ค„์–ด๋“œ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ

 

(2) .dockerignore ํŒŒ์ผ ์ƒ์„ฑ

๊ฐ™์€ ๋””๋ ‰ํ† ๋ฆฌ์— node_modules๋ผ๋Š” ํด๋”๊ฐ€ ๋ณด์ด๋Š”๋ฐ, ์ด ํด๋”๋Š” ์ด๋ฏธ์ง€์— ํฌํ•จ์‹œํ‚ค์ง€ ์•Š๋„๋ก ํ•ด๋ณด์ž.

.gitignoreํŒŒ์ผ์ฒ˜๋Ÿผ docker๋„ .dockerignore ํŒŒ์ผ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

์•„๋ž˜์˜ ๋‚ด์šฉ์„ ์ž‘์„ฑํ•œ๋‹ค.

node_modules/*

 

(3) build๋ช…๋ น์–ด๋กœ web ์ด๋ฏธ์ง€ ์ƒ์„ฑ

docker build -t web .

์ด๋ฆ„์ด web์ธ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ฒ˜์Œ ๋นŒ๋“œ๋ฅผ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์˜ค๋ž˜๊ฑธ๋ฆฌ์ง€๋งŒ ๋‘ ๋ฒˆ์งธ๋ถ€ํ„ฐ๋Š” ๋„์ปค๊ฐ€ ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธˆ๋ฐฉ ๋นŒ๋“œ๊ฐ€ ๋๋‚œ๋‹ค.

์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•จ

๊ทธ๋Ÿฌ๋‚˜ ๋งŒ์•ฝ ํŒŒ์ผ ๋‚ด์šฉ์ด ์ˆ˜์ •๋˜๊ณ  ๋‹ค์‹œ ๋นŒ๋“œ๋ฅผ ํ•˜๋Š” ๊ฒฝ์šฐ ์บ์‹œ๊ฐ€ ๊นจ์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ถ€๊ฐ€ ์ˆ˜์ •๋˜๋”๋ผ๋„ ๋‹ค์‹œ ๋นŒ๋“œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์บ์‹œ ๊นจ์ง

 

(4) web์ด๋ฏธ์ง€๋กœ ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ

docker run --rm -d -p 3000:3000 web

web ์ด๋ฏธ์ง€ ํฌํŠธ 3000๋ฒˆ๊ณผ ๋กœ์ปฌ์˜ 3000๋ฒˆ์„ ์—ฐ๊ฒฐํ•˜์—ฌ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค, http://127.0.0.1:3000/๋กœ ์ ‘์†ํ•ด๋ณด๋ฉด ๋…ธ๋“œ ์›น ํŽ˜์ด์ง€๊ฐ€ ์ž˜ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๋Œ“๊ธ€