Hello Docker

Docker term

What is Docker?

  • ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ containerization(์ปจํ…Œ์ด๋„ˆํ™”) ํ•œ ํ”Œ๋žซํผ. ( ์›น์„œ๋น„์Šค ์ปจํ…Œ์ด๋„ˆ , ๋””๋น„ ์ปจํ…Œ์ด๋„ˆ ๋”ฐ๋กœ ๋‹ด๋Š”๋‹ค๊ณ  ํ•˜๋ฉด ์„œ๋กœ isolation ํ™” ํ• ์ˆ˜ ์žˆ๊ณ  ์„œ๋กœ ๋…๋ฆฝ์ ์ธ ๊ณต๊ฐ„์„ ๊ฐ€์งˆ์ˆ˜ ์žˆ๋‹ค. ์„œ๋กœ์—๊ฒŒ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์„์ˆ˜ ์žˆ๋‹ค. )
  • containerization ๋ž€ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋งค์ฒด๋กœ ํ•œ ์ˆ˜์†ก์ฒด๊ณ„, ์ฆ‰. ์ปจํ…Œ์ด๋„ˆ ๊ทœ๊ฒฉ์„ ๋งž์ถฐ ๋†“์œผ๋ฉด ์ฐจ , ๋ฐฐ , ๋น„ํ–‰๊ธฐ๋“ฑ ๋” ์‰ฝ๊ณ  ๋น ๋ฅด๊ฒŒ ๋Œ€๋Ÿ‰ ์ˆ˜์†ก๊ณผ ์‹œ๊ฐ„์„ ๋‹จ์ถ• ์‹œํ‚ฌ์ˆ˜ ์žˆ๋‹ค. ๋ผ๋Š” ์žฅ์ ์„ ์ง€๋‹ˆ๊ณ  ์žˆ์Œ.
  • ์ด๊ฑธ๋กœ ์œ ์ถ”ํ•ด๋ณผ ๋•Œ ๋„์ปค๋Š” ๋‹ค์–‘ํ•œ SW ๋“ค์„ ์ปจํ…Œ์ด๋„ˆ์— ๋‹ด์•„ ์ด๋™ํ•˜๊ธฐ๊ฐ€ ์‰ฝ๊ฒŒํ•˜๊ณ  ํฌํ„ฐ๋ธ”ํ•˜๊ฒŒ ๋งŒ๋“ค์ˆ˜์žˆ๋Š” ํ”Œ๋žซํผ.
  • ๋ฆฌ๋ˆ…์Šค์˜ ์‘์šฉํ”„๋กœ๊ทธ๋žจ๋“ค์„ ์†Œํ”„ํŠธ์›จ์–ด ์ปจํ…Œ์ด๋„ˆ ์•ˆ์— ๋ฐฐ์น˜์‹œํ‚ค๋Š” ์ผ์„ ์ž๋™ํ™” ํ•˜๋Š” ์˜คํ”ˆ์†Œ์Šค

What is Container?

  • ๊ฐœ๋ฐœ , ๋ฐฐํฌ , ์šด์šฉ ๋“ฑ์„ ์œ„ํ•ด ํ‘œ์ค€ํ™”๋œ ๋‹จ์œ„๋กœ ๊ตฌ์„ฑ๋œ ์†Œํ”„ํŠธ์›จ์–ด ํŒจํ‚ค์ง€ํ™”
  • ํ˜ธ์ŠคํŠธ pc ์˜ ์ปค๋„์„ ๊ณต์œ ํ•˜๋ฉด์„œ SW ๊ฐ€ ๋Œ์•„๊ฐˆ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ตฌ์„ฑ๋“ค์ด ๋“ค์–ด๊ฐ€์žˆ๋‹ค. ๋Œ€์‹ , ๋ถˆํ•„์š”ํ•œ ๊ฒƒ๋“ค์€ ๋“ค์–ด์žˆ์ง€ ์•Š๋‹ค.
  • ํ•ด๋‹น ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ run ์‹œํ‚ค๋ฉด ํ•ด๋‹น SW ๊ฐ€ ๋Œ์•„๊ฐ€๋Š”๋ฐ ๊ทธ ์ƒํƒœ๋ฅผ ์ปจํ…Œ์ด๋„ˆ ๋ผ๊ณ  ํ•œ๋‹ค. ์ผ์ข…์˜ ํ”„๋กœ์„ธ์Šค์™€ ์œ ์‚ฌํ•œ ๊ฐœ๋…์ด๋‹ค.
  • ์ด ํ•ด๋‹น ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€ ํ™˜๊ฒฝ์„ ์ •์˜ํ•˜๋Š” ํŒŒ์ผ์€ dockerfile ์ด๋‹ค.
  • ๋‚˜๋จธ์ง€ ์‹œ์Šคํ…œ์œผ๋กœ ๋ถ€ํ„ฐ ์™„์ „ํžˆ ๋…๋ฆฝ๋œ ๊ณต๊ฐ„์„ ๊ฐ–๋Š”๋‹ค. ๊ทธ๋ž˜์„œ ๋ฐ–์œผ๋กœ ํ†ต์‹ ํ•˜๊ธฐ ์œ„ํ•ด์„  ํฌํŠธ๋ฅผ ๋งตํ•‘ํ•ด์•ผ ํ•œ๋‹ค.

What is Service?

  • ์„œ๋น„์Šค๋Š” app ์˜ ํ•œ ๋ถ€๋ถ„์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋น„๋””์˜ค๋ฅผ ๊ณต์œ ํ•˜๋Š” ์‚ฌ์ดํŠธ๋Š” ๋ฐ์ดํƒ€ ๋ฒ ์ด์Šค์— app data ๋ฅผ ์ €์žฅํ•˜๋Š” ์„œ๋น„์Šค, ๋น„๋””์˜ค ์—…๋กœ๋“œ ํ›„ transcoding ํ•˜๋Š” ์„œ๋น„์Šค ๋“ฑ๋“ฑ..
  • ์„œ๋น„์Šค๋“ค์€ ์‹ค์ œ๋กœ production ์˜ ์ปจํ…Œ์ด๋„ˆ๋“ค๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
  • ํ•˜๋‚˜์˜ ์„œ๋น„์Šค๋Š” ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๋งŒ ์‹คํ–‰ํ•˜์ง€๋งŒ ๊ทธ ์ด๋ฏธ์ง€๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ณฌ๊ณ„ํ™” ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ฒด๊ณ„ํ™”๋Š” docker-compose.yml ํŒŒ์ผ์—์„œ ์„ค์ • ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • docker-compose.yml ํŒŒ์ผ์—์„œ๋Š” ์–ด๋–ค ํฌํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š”์ง€, ์„œ๋น„์Šค๊ฐ€ ๊ฐ€์ ธ๊ฐ€์•ผ ํ•  ํ•„์š”ํ•œ ์šฉ๋Ÿ‰์— ๋”ฐ๋ผ ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ๋ณต์ œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰์‹œ์ผœ์•ผ ํ•˜๋Š”์ง€, ์„œ๋น„์Šค๋ฅผ ์œ„ํ•ด ์–ผ๋งˆ๋‚˜ ์ปดํ“จํŒ… ๋ฆฌ์†Œ์Šค๋ฅผ ํ• ๋‹นํ• ์ง€๋ฅผ ์„ค์ • ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • docker-compose.yml ์„ ์ด์šฉ ํ•˜๋ฉด ์ด๋ฏธ์ง€๋ฅผ ์ผ์ผ์ด ๋‹ค run ์‹œํ‚ค์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

What is Image?

  • ๋„์ปค ์ด๋ฏธ์ง€๋ž€ ์„œ๋น„์Šค์˜ ํ•„์š”ํ•œ ํ”„๋กœ๊ทธ๋žจ , ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, ์†Œ์Šค๋“ฑ์„ ์„ค์น˜ํ•œ ๋’ค์— ์ด๋ฅผ ํŒŒ์ผ๋กœ ๋งŒ๋“  ๊ฒƒ์ด๋‹ค. ์‰ฝ๊ฒŒ ์‹คํ–‰ํŒŒ์ผ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.
  • ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๊ธฐ์œ„ํ•ด dockerfile ์— ํ•„์š”ํ•œ ๋ช…๋ น๋“ค์„ ์„ค์ •ํ•ด ๋†“๋Š”๋‹ค.
  • ์ด๋ฏธ์ง€๋Š” base image ์™€ parent image ๋กœ ๋‚˜๋‰œ๋‹ค.
  • base image ๋Š” parent image ๊ฐ€ ์—†๋Š” ์ด๋ฏธ์ง€๋ฅผ ๋งํ•œ๋‹ค.
  • parent image ๋Š” ์‚ฌ์šฉํ•˜๋Š” ์ด๋ฏธ์ง€์˜ Dockerfile ๋‚ด์— FROM ์œผ๋กœ ์ง€์‹œ๋˜๋Š” ์ด๋ฏธ์ง€์ด๋‹ค. ๋งŒ์•ฝ Dockerfile ๋‚ด์— FROM ์ด ์ง€์‹œ๋˜์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด ๋ถ€๋ชจ ์ด๋ฏธ์ง€๊ฐ€ ์—†๋Š”๊ฒƒ์ด๊ณ  ์ด๋Š” ๊ณง base image ๊ฐ€ ๋œ๋‹ค.
  • Dockerfile ๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๋•Œ๋Š” ์ „์— ๋งŒ๋“ค์—ˆ๋˜ ์ด๋ฏธ์ง€ ์บ์‹œ๋ฅผ ์ž˜ ์ด์šฉํ•ด์•ผ ํ•œ๋‹ค. ๊ธฐ์กด ๋ฐฉ์‹์€ ํ•œ์ค„ ํ•œ์ค„ ์‹คํ–‰ํ•˜๋ฉด์„œ
    ์ž„์‹œ ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ > ๋ช…๋ น์–ด ์ˆ˜ํ–‰ > ์ด๋ฏธ์ง€ ์ €์žฅ > ์ž„์‹œ ์ปจํ…Œ์ด๋„ˆ ์‚ญ์ œ > ์ƒˆ๋กœ ๋งŒ๋“  ์ด๋ฏธ์ง€ ๊ธฐ๋ฐ˜ ์ž„์‹œ ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ > ๋ช…๋ น์–ด ์ˆ˜ํ–‰ > ์ด๋ฏธ์ง€๋กœ ์ €์žฅ > ์ž„์‹œ ์ปจํ…Œ์ด๋„ˆ ์‚ญ์ œ... ์ด๋Ÿฐ์‹์œผ๋กœ ์ˆ˜ํ–‰....

----> {ํ•ด์‰ฌ์•„์ด๋””} : ์ด๋ฏธ์ง€ ์ €์žฅ
----> Running in {ํ•ด์‰ฌ์•„์ด๋””} : ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๊ทธ ์ „ ์ด๋ฏธ์ง€ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž„์‹œ ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ

< ์ฒ˜์Œ Dockerfile ๋Œ๋ ธ์„๋•Œ >

์ฒ˜์Œ Dockerfile ๋Œ๋ ธ์„๋•Œ

< ๊ธฐ์กด Dockerfile ์„ 2 ๋ฒˆ์งธ ๋Œ๋ ธ์„๋•Œ >

๊ธฐ์กด Dockerfile์„ 2๋ฒˆ์งธ ๋Œ๋ ธ์„๋•Œ

< ๊ธฐ์กด Dockerfile ์—์„œ EXPOSE 80 ์—์„œ EXPOSE 81 ๋กœ ๋ณ€๊ฒฝํ–ˆ์„ ๋•Œ >

๊ธฐ์กด Dockerfile ์—์„œ EXPOSE 80 ์—์„œ EXPOSE 81๋กœ ๋ณ€๊ฒฝํ–ˆ์„ ๋•Œ

  • ์ด๋ฏธ์ง€ ์ƒ์„ฑ์‹œ FROM ์— ์ ์–ด๋‘” parent image ๊ธฐ๋ฐ˜์œผ๋กœ ๋ณ€๊ฒฝ์‚ฌํ•ญ๋งŒ ๋”ฐ๋กœ ์ €์žฅ๋˜๋Š”๊ฒŒ ์žฅ์ . vm ์ฒ˜๋Ÿผ ์šฉ๋Ÿ‰์„ ๋งŽ์ด ๋จน์ง€ ์•Š์Œ.
    ๋”ฐ๋ผ์„œ parent image ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๊ณ  parent image ๋ฅผ ์‚ญ์ œํ•˜๋ ค๊ณ  ํ–ˆ์„๋•Œ ์—๋Ÿฌ ๋œธ.

error

What is Stack?


Docker Structure

Docker Structure

์ถœ์ฒ˜ : http://www.leafcats.com/146


Docker Install

๊ณต์‹ ํ™ˆํŽ˜์ด์ง€ ์ฐธ์กฐ


Docker use

๋„์ปค ํ—ˆ๋ธŒ์—์„œ ์›ํ•˜๋Š” ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์šด ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ๋„์ปค ํ—ˆ๋ธŒ

$ docker --help

๋„์ปค์˜ ๋ช…๋ น์–ด๋ฅผ ์‚ดํŽด๋ณผ ์ˆ˜ ์žˆ๋‹ค.

$ docker pull nginx:latest
or
$ docker run -d -it \
--name merlin-nginx \
-p 8080:80 \
nginx:latest

pull ์„ ํ•˜๋ฉด ํ•ด๋‹น ์ด๋ฏธ์ง€๋งŒ ๋ฐ›๋Š”๊ฒƒ์ด๋‹ค
run ์„ ์‹คํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด local ์— nginx:latest ๋ผ๋Š” ์ด๋ฏธ์ง€๊ฐ€ ์—†์œผ๋ฉด ์ž๋™์œผ๋กœ hub ์—์„œ ๋‹ค์šด๋ฐ›์•„์„œ run ์„ ์‹œํ‚จ๋‹ค.

$ docker images
$ docker ps
$ docker ps [-a] [-l] [-n number]

images ๋Š” ๋„์ปค ์ด๋ฏธ์ง€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ดํŽด๋ณผ ์ˆ˜ ์žˆ๋‹ค.
ps ๋Š” ์‹คํ–‰์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
-a ์˜ต์…˜์„ ๋ถ™์ด๋ฉด ์ข…๋ฃŒ๋œ ๋ชฉ๋ก๊นŒ์ง€ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
-l ์˜ต์…˜์€ ๋งˆ์ง€๋ง‰ ์‹คํ–‰ํ–ˆ๋˜ ๋ชฉ๋ก , -n ์€ ๋ฆฌ๋ˆ…์Šค์˜ tail ๋ช…๋ น์–ด์™€ ๋™์ผํ•˜๋‹ค.

$ docker commit -a "merlin <merlin@kakaocrop.com>" -m olympic-search-pc naughty_torvalds search.daum.pc:olypic

ํ˜„์žฌ ๋Œ์•„๊ฐ€๊ณ  ์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ด๋ฏธ์ง€๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
-a ์˜ต์…˜ : author , -m : ์ปค๋ฐ‹ ๋ฉ”์„ธ์ง€ , <naughty_torvalds> ๋“ค์–ด๊ฐ€๋Š” ๋ถ€๋ถ„ : ์ง€๊ธˆ ๋™์ž‘ํ•˜๊ณ  ์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„ , <search.daum.pc:olypic> ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€ ๋„ค์ž„๊ณผ ํƒœ๊ทธ

$ docker rm <์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„>
$ docker rmi <์ด๋ฏธ์ง€>
# ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌ.
$ docker rm $(docker ps -a -q)

๋„์ปค ์ด๋ฏธ์ง€์™€ ์ปจํ…Œ์ด๋„ˆ ์‚ญ์ œ

$ docker start <์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„>
$ docker stop  <์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„>
$ docker restart <์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„>

๋„์ปค ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰ , ์ค‘์ง€ , ์žฌ๋ถ€ํŒ…

$ docker attach <์‹คํ–‰์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„>
$ docker exec -it <์‹คํ–‰์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„> <์ปจํ…Œ์ด๋„ˆ ์•ˆ์—์„œ ์‹คํ–‰ํ•  ๋ช…๋ น>
$ docker exec -it merlin-nginx /bin/bash

attach ๋Š” ์‹คํ–‰์ค‘์ธ ๋„์ปค ์ปจํ…Œ์ด๋„ˆ์— ์ ‘์†ํ•œ๋‹ค. ๋‹จ, ps ์ณค์„๋•Œ command ์— /bin/bash ๋ผ๊ณ  ์ณ์ ธ์žˆ์–ด์•ผ ์ž์œ ๋กญ๊ฒŒ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ž…๋ ฅ์€ ํ•  ์ˆ˜ ์—†๊ณ  ์ถœ๋ ฅ๋งŒ ๋ณด์ธ๋‹ค.
exec ๋Š” ์ปจํ…Œ์ด๋„ˆ ์•ˆ์—์„œ ์ˆ˜ํ–‰ํ•  ๋ช…๋ น์„ ๋‚ ๋ ค์ค๋‹ˆ๋‹ค. -it ์˜ต์…˜์„ ์ฃผ๋ฉด ์‹คํ–‰๋œ bash ์‰˜์—์„œ ์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ์„ ํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

$ docker inspect <์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„ or ์ด๋ฏธ์ง€:ํƒœ๊ทธ>

์ด๋ฏธ์ง€ ๋˜๋Š” ์ปจํ…Œ์ด๋„ˆ์˜ ์„ธ๋ถ€์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

$ docker history <์ด๋ฏธ์ง€:ํƒœ๊ทธ>

์ด๋ฏธ์ง€์˜ ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ์กฐํšŒ

$ docker diff <์‹คํ–‰์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„>

์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํ–‰๋˜๋ฉด์„œ ๋ณ€๊ฒฝ๋œ ํŒŒ์ผ ๋ชฉ๋ก์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋น„๊ต ๊ธฐ์ค€์€ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•œ ์ด๋ฏธ์ง€ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

$ docker cp <์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„>:<๊ฒฝ๋กœ> <ํ˜ธ์ŠคํŠธ ๊ฒฝ๋กœ>

์ปจํ…Œ์ด๋„ˆ์—์„œ ํŒŒ์ผ์„ ๊บผ๋‚ด๊ธฐ.


Docker build

$ docker [OPTIONS] PATH | URL | -
$ docker build -t app .

-t ์˜ต์…˜์€ ์ƒ์„ฑํ•  ์ด๋ฏธ์ง€ ์ด๋ฆ„์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
-f ์˜ต์…˜์€ ํŒŒ์ผ๋ช…์„ custom ํ•˜๊ฒŒ ์ง€์—ˆ์„๋•Œ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ Dockerfile ๊ฒฝ๋กœ๊ฐ€ ๋ฃจํŠธ์— ์—†์„๋•Œ ์ด์šฉ.
PATH ๋Š” ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ ํ• ๋•Œ ์ „์†ก๋˜๋Š” context ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

Dockerfile

# ์–ด๋–ค ์ด๋ฏธ์ง€๋กœ๋ถ€ํ„ฐ ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ• ์ง€๋ฅผ ์ž‘์„ฑ
# ๋ฒ ์ด์Šค๋ฅผ ์ง€์ •
FROM node:argon

# Dockerfile ์„ ๊ด€๋ฆฌํ•˜๋Š” ์‚ฌ๋žŒ์˜ ์ด๋ฆ„
MAINTAINER < merlin.ho > merlin@merlin.com
# /app directory ์ƒ์„ฑ
RUN mkdir /app

# /app directory๋ฅผ WORKDIR๋กœ ์„ค์ •
WORKDIR /app

# ํ˜„์žฌ ๊ฒฝ๋กœ์— ์žˆ๋Š” package.json์„ /app์— ๋ณต์‚ฌ
# ADD์™€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋™์ผํ•˜๋‚˜ ์ฐจ์ด์ ์€ URL์„ ์ง€์ •ํ• ์ˆ˜ ์—†๊ณ  ์••์ถ•ํŒŒ์ผ์„ ์ž๋™์œผ๋กœ ํ’€์–ด์ฃผ์ง€ ์•Š๋Š”๋‹ค. 
COPY package.json /app

# ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ํ˜ธ์ŠคํŠธ์—์„œ docker image๋กœ copyํ•œ๋‹ค.
# src์— ํŒŒ์ผ ๋Œ€์‹  url์„ ์ž…๋ ฅํ• ์ˆ˜ ์žˆ๊ณ  ์••์ถ• ํŒŒ์ผ์˜ ๊ฒฝ์šฐ ์••์ถ•์„ ํ•ด์ œํ•˜๋ฉด์„œ ๋ณต์‚ฌ
# ํ˜ธ์ŠคํŠธ์— ์žˆ๋Š” . ๊ฒฝ๋กœ์— ์žˆ๋Š”๊ฑธ /usr/src/app ์— ๋ณต์‚ฌ.
ADD < src > < dest >
ADD . /usr/src/app

# ๋‚ด๋ถ€์ ์œผ๋กœ /bin/sh -c ์‹คํ–‰ ๋’ค npm install ์„ ์‹คํ–‰
RUN npm install

# ํ˜„์žฌ Dockerfile ์ด ์žˆ๋Š” ๊ฒฝ๋กœ์˜ ๋ชจ๋“  ํŒŒ์ผ์„ /app ์— ๋ณต์‚ฌํ•จ
COPY . /app

# ๊ฐ€์ƒ๋จธ์‹ ์— ์˜คํ”ˆํ•  ํฌํŠธ๋„˜๋ฒ„
EXPOSE 3000

# ๋„์ปค ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํ–‰ ๋˜์—ˆ์„๋•Œ ์‹คํ–‰๋˜๋Š” ๋ช…๋ น ์ง€์ •, ๋นŒ๋“œํ• ๋•Œ๋Š” ์‹คํ–‰๋˜์ง€ ์•Š์œผ๋ฉฐ ์—ฌ๋Ÿฌ๊ฐœ์˜ CMD๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๊ฒƒ๋งŒ ์‹คํ–‰ ๋ฉ๋‹ˆ๋‹ค.
# ํ•œ๊บผ๋ฒˆ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์—๋Š” run.shํŒŒ์ผ์„ ์ž‘์„ฑํ•˜์—ฌ ๋ฐ๋ชฌ์œผ๋กœ ์‹คํ–‰ํ•˜๊ฑฐ๋‚˜ supervisord๋‚˜ forego์™€ ๊ฐ™์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
CMD ["npm", "start"]

recommendation Dockerfile

  1. Containers should be ephemeral

    • Ephemeral environments are also sometimes called โ€œDynamic environmentsโ€, โ€œTemporary environmentsโ€, โ€œon-demand environmentsโ€ or โ€œshort-lived environmentsโ€.
    • static environments ์™€ ๋ฐ˜๋Œ€๋˜๋Š” ๊ฐœ๋…์ด๋‹ค.
    • ์ด๊ฒƒ์ด ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” ์ด ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์šฐ๋ฆฌ๊ฐ€ stop and destroy ํ•  ์ˆ˜ ์žˆ๊ณ  ์ƒˆ๋กญ๊ฒŒ built ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ตœ์†Œํ•œ์˜ ์…‹ํŒ…๊ณผ ์„ค์ •์œผ๋กœ ์›ํ•˜๋Š” ์žฅ์†Œ์— ์˜ฌ๋ ค ๋†“์„์ˆ˜ ์žˆ๋‹ค ๋ผ๋Š”๊ฒƒ์„ ๋œปํ•จ.
    • ๊ทธ๋ž˜์„œ ํ•ญ์ƒ production ํ™˜๊ฒฝ๊ณผ ๊ฐ™์€ ํ™˜๊ฒฝ์—์„œ test ๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. test ๋„ ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๋ฅผ ๊ณต์œ  ํ•จ์œผ๋กœ์จ ๋ณ‘๋ ฌ๋กœ ์ง„ํ–‰ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  2. Use a .dockerignore file

    • ์šฐ๋ฆฌ๋Š” docker build ์ปค๋ฉ˜๋“œ๋ฅผ ์น˜๋Š” ์žฅ์†Œ๋ฅผ build context ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
    • ๋””ํดํŠธ๋กœ ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ๋ผ๊ณ  ๊ฐ€์ •ํ•˜์ง€๋งŒ ๊ตฌ์ฒด์ ์ธ ๋‹ค๋ฅธ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ ค๋ฉด -f flag ๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.
    • ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์˜ ํŒŒ์ผ๊ณผ ์—ฌ๋Ÿฌ ๋””๋ ‰ํ† ๋ฆฌ๋“ค์„ docker deamon ์—๊ฒŒ build context ๋กœ ๋ณด๋‚ด์ง€๊ฒŒ ๋˜๋Š”๋ฐ ์ด ์ด๊ฒƒ์€ ๋ฌด์‹ฌ์ฝ” ์ด๋ฏธ์ง€ ๊ฒฐ๊ณผ๋กœ ๋ณด๋‚ด์ง€๊ฒŒ ๋˜์–ด ์ด๋ฏธ์ง€ ํŒŒ์ผ์ด ์ปค์ง€๊ฒŒ ๋œ๋‹ค.
    • ๋”ฐ๋ผ์„œ .gitignore ์™€ ๋น„์Šทํ•˜๊ฒŒ .dockerignore ํŒŒ์ผ์„ ์‚ฌ์šฉํ•ด์„œ ๊ด€๊ณ„์—†๋Š” ํŒŒ์ผ๋“ค์„ exclude ์‹œํ‚ฌ์ˆ˜ ์žˆ๋‹ค.
  3. Use multi-stage builds

    • Docker 17.05 ๋ฒ„์ „ ์ด์ƒ์—์„œ๋Š” multi-stage builds ๋กœ ์ตœ์ข… ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
    • multi-stage builds ๋ž€ Dockerfile ์•ˆ์—์„œ multiple FROM ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ๊ฐ์˜ ์ƒˆ๋กœ์šด stage ๊ฐ€ ๋นŒ๋“œ ๋˜๊ณ  ํ•œ stage ์—์„œ ๋‚˜์˜จ ๊ฒฐ๊ณผ ์‚ฐ์ถœ๋ฌผ์„ ๋˜๋‹ค๋ฅธ stage ๋กœ ์นดํ”ผ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ค‘๊ฐ„ ์‚ฐ์ถœ๋ฌผ์„ ์œ„ํ•ด Dockerfile ์„ ์—ฌ๋Ÿฌ๊ฐœ ๋งŒ๋“ค์–ด์„œ ์ค‘๊ฐ„ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๊ณ  ํ•˜๋Š” ๋ณต์žกํ•œ ๊ณผ์ •์„ ๊ฑฐ์น˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
    • dockerfile ์„ ์ž‘์„ฑํ• ๋•Œ ์ž์ฃผ ๋ฐ”๋€Œ์ง€ ์•Š์€ ๊ฒƒ๋ถ€ํ„ฐ ์ž์ฃผ ๋ฐ”๋€Œ๋Š” ์ˆœ์œผ๋กœ ์ž‘์„ฑ์„ ํ•œ๋‹ค.
  4. Avoid installing unnecessary packages

    • ๋ถˆํ•„์š”ํ•œ ํŒจํ‚ค์ง€๋Š” ์ธ์Šคํ†จ์„ ํ”ผํ•ด๋ผ.
    • ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋ฐ์ดํƒ€๋ฒ ์ด์Šค ์ด๋ฏธ์ง€์— text editor ๋ฅผ ์ถ”๊ฐ€์‹œํ‚ฌ ํ•„์š”๋Š” ์—†๋‹ค.
  5. Each container should have only one concern

    • ํ•˜๋‚˜์˜ ์ปจํ…Œ์ด๋„ˆ๋Š” ์˜ค์ง ํ•˜๋‚˜์˜ ์ผ๋งŒํ•˜๊ฒŒ ์‹œ์ผœ๋ผ.
    • ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ์—ฌ๋Ÿฌ ์ปจํ…Œ์ด๋„ˆ๋กœ ๋ถ„๋ฆฌํ•˜๋ฉด ๊ฐ€๋กœ๋กœ ํ™•์žฅํ•˜๊ณ  ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋‹ค์‹œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ์‰ฌ์›Œ์ง‘๋‹ˆ๋‹ค.
    • ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊นจ๋—ํ•˜๊ณ  ๊ฐ€๋Šฅํ•œ ๋ชจ๋“ˆํ™” ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ์ตœ๊ณ ์˜ ํŒ๋‹จ์„ ํ•ด๋ผ.
    • ๋งŒ์•ฝ ์ปจํ…Œ์ด๋„ˆ ๊ฐ๊ฐ์ด ์˜์กด์„ฑ์„ ๋„๊ณ  ์žˆ๋‹ค๋ฉด Docker network ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ปจํ…Œ์ด๋„ˆ๋ผ๋ฆฌ์˜ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  6. Minimize the number of layers
  7. Sort multi-line arguments
  8. Build cache

10 things to avoid in docker containers

1. Donโ€™t store data in containers
2. Donโ€™t ship your application in two pieces
3. Donโ€™t create large images
4. Donโ€™t use a single layer image
5. Donโ€™t create images from running containers
6. Donโ€™t use only the โ€œlatestโ€ tag
7. Donโ€™t run more than one process in a single container
8. Donโ€™t store credentials in the image. Use environment variables
9. Donโ€™t run processes as a root user
10. Donโ€™t rely on IP addresses

์ถœ์ฒ˜ : https://developers.redhat.com/blog/2016/02/24/10-things-to-avoid-in-docker-containers/


Docker app data ๊ด€๋ฆฌ๋ฒ•

ํ˜ธ์ŠคํŠธ์˜ ์ง€์ •๋œ ํด๋”๋ฅผ ์ปจํ…Œ์ด๋„ˆ์— ๋‹ค์ด๋ ‰ํŠธ๋กœ ๋งˆ์šดํŠธ

$ docker run --name merlin_ubuntu -d -v <ํ˜ธ์ŠคํŠธ ํด๋”๊ฒฝ๋กœ>:<๋งˆ์šดํŠธ๋  ์ปจํ…Œ์ด๋„ˆ ํด๋”๊ฒฝ๋กœ> -it ubuntu:14.04

volume container ์‚ฌ์šฉ

# ๋ณผ๋ฅจ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋งŒ๋“ ๋‹ค. busybox ๊ธฐ๋ฐ˜ ์ •๋ง ๋ฆฌ๋ˆ…์Šค์˜ ์ตœ์†Œ ๊ธฐ๋ฐ˜๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. 1.14MB
$ docker run -v /dbdata --name dbdata busybox

# ๋ณผ๋ฅจ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์šฐ๋ถ„ํˆฌ ์ปจํ…Œ์ด๋„ˆ์— ์—ฐ๊ฒฐ
$ docker run --volumes-from=dbdata -it ubuntu:14.04 /bin/bash

# ๋นฝ์—…๋„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
$ docker run โ€”volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata

volume ์œผ๋กœ ์‚ฌ์šฉ

  • ๋ณดํ†ต standalone ์ปจํ…Œ์ด๋„ˆ์—๋Š” -v ๋˜๋Š” --volume ์˜ต์…˜์„ ์‚ฌ์šฉํ–ˆ๊ณ , swarm service ์—๋Š” --mount ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด ์™”์ง€๋งŒ 17.06 ์ดํ›„ ๋ฒ„์ ผ๋ถ€ํ„ฐ๋Š” standalone ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋ชจ๋‘ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ๋งŒ์•ฝ ๋ณผ๋ฅจ๋“œ๋ผ์ด๋ฒ„์˜ ๊ตฌ์ฒด์ ์ธ ์˜ต์…˜์„ ์…‹ํŒ…ํ•ด์•ผ ํ•œ๋‹ค๋ฉด --mount ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด๋ผ.
  • -v ๋˜๋Š” --volume ์˜ต์…˜์€ ๋„์ปค ํ˜ธ์ŠคํŠธ์— ํŒŒ์ผ ๋˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด์ฃผ์ง€๋งŒ --mount ์˜ต์…˜์€ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด์ฃผ์ง€ ์•Š๋Š”๋‹ค ๋Œ€์‹  ์—๋Ÿฌ๋ฅผ ๋ฟœ๋Š”๋‹ค.
  • -v ๋˜๋Š” --volume ์€ ์„ธ๊ฐ€์ง€ field ๋ฅผ ๊ฐ€์งˆ์ˆ˜ ์žˆ๋‹ค ์ฒซ๋ฒˆ์งธ๋Š” ํ˜ธ์ŠคํŠธ ๋จธ์‹ ์˜ ๋ณผ๋ฅจ ์ด๋ฆ„์ด๊ณ  ๋žœ๋คํ•œ ๋ณผ๋ฅจ์„ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ƒ๋žตํ•ด๋„ ๋œ๋‹ค. ๋‘๋ฒˆ์งธ๋Š” ์ปจํ…Œ์ด๋„ˆ ์•ˆ์— ์ƒ์„ฑ๋  ๋””๋ ‰ํ† ๋ฆฌ ๋˜๋Š” ํŒŒ์ผ path ์ด๋‹ค. ์„ธ๋ฒˆ์งธ๋Š” ์˜ต์…˜์œผ๋กœ ์ฝค๋งˆ(:)๋กœ ์˜ต์…˜์„ ๋‚˜์—ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ro(readonly) ์˜ต์…˜๋“ฑ์ด ์žˆ๋‹ค.
  • --mount ๋Š” key=value ๋กœ ๊ตฌ์„ฑ์„ ํ•˜๊ณ  key ์—๋Š” type, src or source , dst or target , volume-opt ๋“ฑ์ด ์žˆ๋‹ค.
  • type ์˜ ๊ฒฝ์šฐ์—๋Š” bind , volume , tmpfs ๊ฐ€ ์žˆ๋‹ค. ์˜ฌ๋ฐ”๋ฅธ ํƒ€์ž…์œผ๋กœ ์„ ํƒํ•˜์ž.
  • Voloumn ํƒ€์ž…, volumn ์€ ๋„์ปค์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๋Š” host ์ชฝ์— ์žˆ๋Š” ํŒŒ์ผ์‹œ์Šคํ…œ์œผ๋กœ ์ €์žฅ์ด ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ volumn ์€ "~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty" ํ•ด๋‹น ๊ฒฝ๋กœ์— ์žˆ๋Š” ๋ฆฌ๋ˆ…์Šค ์•ˆ์— "/var/lib/docker/volumes/" ์ชฝ์— ์ €์žฅ์ด ๋œ๋‹ค. ๋„์ปค๊ฐ€ ์•„๋‹Œ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ด ํŒŒ์ผ์€ ์ˆ˜์ •ํ•˜์ง€ ๋ชปํ•˜๊ณ  volumnes ๋Š” ๋„์ปค ์•ˆ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์ตœ๊ณ ์˜ ๋ฐฉ๋ฒ•์ด๋‹ค.
  • bind ํƒ€์ž…, ์ด bind ํƒ€์ž…์€ host ์–ด๋””์—๋‚˜ ์ €์žฅ์ด ๋  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์‹œ์Šคํ…œ ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ† ๋ฆฌ๋“ค์— ์‚ฌ์šฉ์ด ๋ ์ˆ˜์žˆ๊ณ  ์–ธ์ œ๋“ ์ง€ ๋„์ปค๊ฐ€ ์•„๋‹Œ ํ”„๋กœ์„ธ์Šค๋‚˜ ๋„์ปค ์ปจํ…Œ์ด๋„ˆ์—์„œ ์ˆ˜์ •๋ ์ˆ˜ ์žˆ๋‹ค.
  • tmpfs ํƒ€์ž…, ์ด ํƒ€์ž…์€ hosts ์˜ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ์ด ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  host ํŒŒ์ผ์‹œ์Šคํ…œ์— ์“ธ์ˆ˜๊ฐ€ ์—†๋‹ค. ํœ˜๋ฐœ์„ฑ์ด ๊ฐ•ํ•˜๋‹ค.
$ docker volume create my-vol
$ docker volume ls
## Remove a volume:
$ docker volume rm my-vol
## ์ปจํ…Œ์ด๋„ˆ ์—ฐ๊ฒฐ
## mount ๋ฐฉ๋ฒ•
$ docker run -d \
  -it \
  --name devtest \
  --mount source=myvol2,target=/app \
  nginx:latest

  $ docker run -d \
  -it \
  --name=nginxtest \
  --mount source=nginx-vol,destination=/usr/share/nginx/html \
  nginx:latest

# -v ๋ฐฉ๋ฒ•
 $ docker run -d \
  -it \
  --name devtest \
  -v myvol2:/app \
  nginx:latest


  $ docker run -d \
  -it \
  --name=nginxtest \
  -v nginx-vol:/usr/share/nginx/html \
  nginx:latest

img_busybox ์šฉ๋Ÿ‰

๋งˆ์šดํŠธ ๋œ ๊ฒฝ๋กœ๋Š” inspect ๋ช…๋ น์–ด๋กœ ์•Œ์•„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋งˆ์šดํŠธ ๊ฒฝ๋กœ

๊ฒฐ๋ก ์€ ์–ด์ฐŒ๋˜์—ˆ๊ฑด ํ˜ธ์ŠคํŠธ์— ๋ฐ์ดํƒ€๋ฅผ ์—ฐ๋™ ์‹œํ‚จ๋‹ค.
volume container ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋–„ ๋ฐ์ดํ„ฐ๋„ ์ปจํ…Œ์ด๋„ˆ๋กœ ๊ด€๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿผ..์ปจํ…Œ์ด๋„ˆ์˜ ์žฅ์ ์„ ๋‹ค ์ด์šฉํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ...


Docker compose

์—ฌ๋Ÿฌ ์ปจํ…Œ์ด๋„ˆ์˜ Docker application ์„ running ํ•˜๊ณ  defining ํ•˜๊ธฐ ์œ„ํ•œ ํˆด์ด Compose ์ด๋‹ค. Compose ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ YAML ํŒŒ์ผ์— config ๋ฅผ app service ๋ณ„๋กœ ์„ค์ •ํ•˜๋ฉด ๋˜๊ณ  ํ•œ ์ปค๋งจ๋“œ ๋ผ์ธ์œผ๋กœ ์„œ๋น„์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ์‹œ์ž‘ํ• ์ˆ˜ ์žˆ๋‹ค. ํ•ด๋‹น ์„œ๋น„์Šค๋Š” YAML ํŒŒ์ผ์— ์ ํ˜€์žˆ๋Š” ๊ทธ๋Œ€๋กœ๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค.

docker compose install docker compose install

๊ณต์‹๋ฌธ์„œ https://docs.docker.com/compose/compose-file/

Docker compose ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

  1. Define your appโ€™s environment with a Dockerfile so it can be reproduced anywhere.
  2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
  3. Run docker-compose up and Compose starts and runs your entire app.

Docker compose config

version 3

# compose ๋ฒ„์ ผ 3์„ ์ด์šฉํ•˜๊ฒ ๋‹ค. ๋Œ€์†Œ๋ฌธ์ž ์ฃผ์˜
version: '3'

services:
    webapp:
        build: ./dir # ๊ตฌ์ฒด์ ์ธ ๋นŒ๋“œ ์ปจํ…์ŠคํŠธ์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋„์ปค ํŒŒ์ผ์„ ํฌํ•จํ•˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋‚˜ ๊นƒ ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ url,
        build: # ๋˜๋Š” ๊ตฌ์ฒด์ ์ธ ์˜ต์…˜๋“ค์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
            context: ./dir
            dockerfile: Dockerfile-alternate
            args: # ๋นŒ๋“œ ํ”„๋กœ์„ธ์Šค ์ง„ํ–‰์ค‘์— ์‚ฌ์šฉํ•  ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋“ค์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
                buildno: 1
            cache_from: # v3.2์ง€์› ์—”์ง„์ด ์บ์‹œ๋กœ ์‚ฌ์šฉํ•  ์ด๋ฏธ์ง€๋“ค
                - alpine:latest
                - corp/web_app:3.14
        command: bundle exec thin -p 3000  # ๋””ํดํŠธ ์ปค๋ฉ˜๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ
        container_name: my-web-container # ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„
        # ๋ฒ„์ „ 3๋ถ€ํ„ฐ๋Š” depend_on ์•ˆ์— condition์€ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค.
        depends_on: # ์„œ๋น„์Šค๋“ค ๊ฐ„์— ์˜์กด์„ฑ์„ ํ‘œํ˜„, ์ด๊ฒƒ์€ db, redis ์ดํ›„์— webapp ์ด ์‹คํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.(compose up์‹œ)
            - db
            - redis
        tmpfs: /run # ์ปจํ…Œ์ด๋„ˆ์— ์ž„์‹œ ํŒŒ์ผ ์‹œ์Šคํ…œ์„ ๋งˆ์šดํŠธ , ๋ฆฌ์ŠคํŠธ๋กœ ์ž‘์„ฑํ•ด๋„ ๋œ๋‹ค.
        entrypoint: /code/entrypoint.sh # ๋„์ปค ํŒŒ์ผ์˜ ์—”ํŠธ๋ฆฌ ํฌ์ธํŠธ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ , ๋ฆฌ์ŠคํŠธ๋กœ ์ž‘์„ฑํ•ด๋„ ๋œ๋‹ค.
        env_file:  # ํ™˜๊ฒฝ๋ณ€์ˆ˜๋“ค์„ ํŒŒ์ผ๋กœ ๋ฐ›๋Š”๋‹ค. ์ด ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ํŒŒ์ผ๋“ค์€ ๊ฐ๊ฐ ๋ผ์ธ๋งˆ๋‹ค VAR=VAL ํฌ๋ฉง์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค. #์€ ์ฃผ์„
            - ./common.env
            - ./apps/web.env
            - /opt/secrets.env
        environment: # ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์ถ”๊ฐ€. ์–ด๋–ค boolen ๊ฐ’๋“ค์€ quotes๋กœ ๊ฐ์‹ผ๋‹ค.
            RACK_ENV: development
            SHOW: 'true'
            SESSION_SECRET:
        expose: # ํฌํŠธ ๋…ธ์ถœ
            - "3000"
            - "8000"
        external_links: # ๋งํฌ ์ปจํ…Œ์ด๋„ˆ docker-compose.yml ์™ธ๋ถ€์— ์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ , CONTAINER:ALIAS
            - redis_1
            - project_db_1:mysql
            - project_db_1:postgresql
        extra_hosts: # /etc/hosts ์— ์ถ”๊ฐ€
            - "somehost:162.242.195.82"
            - "otherhost:50.31.209.229"
        image: redis # ํŠน๋ณ„ํ•œ ์ด๋ฏธ์ง€๋กœ ๋ถ€ํ„ฐ ๋Ÿฐ , ์ด๋ฏธ์ง€๊ฐ€ ์—†์œผ๋ฉด pull ํ•œ๋‹ค.
        healthcheck: # ์ด ์„œ๋น„์Šค๋ฅผ ์œ„ํ•ด ๊ฑด๊ฐ•ํ•œ์ง€ ์ฒดํฌ , ์ด๋ฏธ์ง€๋กœ ๋ถ€ํ„ฐ ์–ด๋–ค ๋””ํดํŠธ ํ—ฌ์Šค์ฒดํฌ๋ฅผ disable ํ•˜๋ ค๋ฉด disable: true ์ถ”๊ฐ€
            test: ["CMD", "curl", "-f", "http://localhost"]
            interval: 1m30s
            timeout: 10s
            retries: 3
        labels: # ๋ผ๋ฒจ ์ถ”๊ฐ€.
            com.example.description: "Accounting webapp"
            com.example.department: "Finance"
            com.example.label-with-empty-value: ""
        links: # ๋‹ค๋ฅธ ์„œ๋น„์Šค๋“ค์˜ ์ปจํ…Œ์ด๋„ˆ ๋“ค๊ณผ ์—ฐ๊ฒฐ๊ณ ๋ฆฌ๋ฅผ ํ˜•์„ฑ, SERVICE:ALIAS , ์ฃผ์˜!! --links ํ”Œ๋ž˜๊ทธ๋Š” ๋ ˆ๊ฑฐ์‹œ ์ด๋‹ค. --links ์‚ฌ์šฉ์„ ๋Œ€์‹ ํ•ด์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•˜๋Š” ๋„คํŠธ์›Œํฌ ์„ค์ •์„ ์ด์šฉํ•˜๊ธธ ๊ถŒ์žฅํ•œ๋‹ค.
            - db
            - db:database
            - redis
        logging: # ์„œ๋น„์Šค์˜ ๋กœ๊น… config
            driver: syslog # json-file , syslog , none
            options:
                syslog-address: "tcp://192.168.0.42:123",
                max-size: "200k" # json-file ์˜ ๊ฒฝ์šฐ ๋กœ๊ทธ ์ €์žฅ์„ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ค๋ž˜๋œ ํŒŒ์ผ์€ ์ง€์›Œ์ง„๋‹ค.
                max-file: "10"
        network_mode: bridge # ๋„คํŠธ์›Œํฌ ๋ชจ๋“œ ์„ค์ • , host , none , service:[service name] , container:[container name/id] //// host ๋ชจ๋“œ๋Š” links๋ž‘ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
        networks: # top level ์—์„œ ์ง€์ •ํ•œ ๋„คํŠธ์›Œํฌ ์„ค์ • , ์•Œ๋ฆฌ์•„์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค. ์ด ์•Œ๋ฆฌ์•„์Šค ๋„ค์ž„์œผ๋กœ ์„œ๋น„์Šค๋ฅผ ์—ฐ๊ฒฐ ํ•  ์ˆ˜ ์žˆ๋‹ค.
            - some-network
            - other-network
            new:
                aliases:
                    - database
        ports: # HOST:CONTAINER
            - "8000:8000"
        volumes: # ํ˜ธ์ŠคํŠธ ๊ฒฝ๋กœ ๋˜๋Š” ์ƒ์„ฑ๋œ ๋ณผ๋ฅจ์„ ๋งˆ์šดํŠธ ์‹œํ‚จ๋‹ค.
            - type: volume
            source: mydata
            target: /data
            volume:
                nocopy: true
            - type: bind
            source: ./static
            target: /opt/app/static


volumes:
  mydata:
    external : true # ์ปดํฌ์ฆˆ ๋ฐ–์— volume์ด ์กด์žฌํ–ˆ์„๋•Œ, ์—†์œผ๋ฉด 'docker volume create --name=' ์œผ๋กœ ๋งŒ๋“ค๋ผ๊ณ  ์—๋Ÿฌ๋œธ.
    name : actual-name-of-volume # ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
  dbdata:

networks:
  • ์ง์ ‘ ํ–ˆ๋˜ ์˜ˆ์ œ
version: '3.2'

services:
  app_blue:
    image: blog_express:v1
    depends_on:
      - nginx-proxy
      - mongo-db
    working_dir: /app/
    networks:
      - dbconnection
    environment:
      - VIRTUAL_HOST=nodeapp.local
    volumes:
      - type: bind
        source: ./server
        target: /app/server
    command: npm start

  app_green:
    image: blog_express:v1
    depends_on:
      - nginx-proxy
      - mongo-db
    working_dir: /app/
    networks:
      - dbconnection
    environment:
      - VIRTUAL_HOST=nodeapp.local
    volumes:
      - type: bind
        source: ./server
        target: /app/server
    command: npm start

  nginx-proxy:
    image: jwilder/nginx-proxy
    ports:
      - 8081:80
    networks:
      - dbconnection
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro

  mongo-db:
    image: mongo
    depends_on:
      - nginx-proxy
    volumes:
      - db-data:/data/db
    networks:
      - dbconnection
    command: mongod --dbpath "/data/db"

volumes:
  db-data:

networks:
  dbconnection:

Docker network

์ถœ์ฒ˜ : http://bluese05.tistory.com/15

network ๊ตฌ์„ฑ

  • ๋„์ปค๋ฅผ ์„ค์น˜ ํ•˜๋ฉด docker host ์ชฝ์— ๋ณด๋ฉด docker0 ์ด๋ผ๋Š” virtual ethernet bridge ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ( $ ifconfig ๋กœ ํ™•์ธ๊ฐ€๋Šฅ, ๋‚œ 172.17.0.1 ๋กœ ์…‹ํŒ…๋˜์–ด ์žˆ๋‹ค.)
  • docker0 ๋Š” container ๊ฐ€ ํ†ต์‹ ํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ€์ƒ linux bridge ์ด๋‹ค. bridge ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ L2 ํ†ต์‹  ๊ธฐ๋ฐ˜์ด๋‹ค.
  • ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํ•˜๋‚˜์”ฉ ์ƒ์„ฑ๋˜๋ฉด ์ด bridge ์— ํ•˜๋‚˜์”ฉ ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค. ( $ brctl show docker0 )
  • ์ด๋ ‡๊ฒŒ ์—ฐ๊ฒฐ๋œ ์ปจํ…Œ์ด๋„ˆ์™€ host ๋Š” ์ผ€์ด๋ธ”๋กœ ์—ฐ๊ฒฐํ•œ ๋‘๋Œ€์˜ pc ๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.
  • ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์˜ eth0 ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋‚ด๋ถ€๋กœ ๊ฒฉ๋ฆฌ๋˜์–ด์žˆ์–ด์„œ ์ด ๋‚ด๋ถ€์™€ host ๋ฅผ ์ด์–ด์ค„ vethxxxx ํ˜•ํƒœ์˜ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ํ•˜๋‚˜ ๋” ์กด์žฌํ•œ๋‹ค. ์ด ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ host ์™€ ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์ด๋ฅผ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.
  • ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์˜ ์•„์ดํ”ผ๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด ifconfig eth0 ๋ผ๊ณ  ์น˜๋ฉด ๋˜๋Š”๋ฐ ifconfig ๊ฐ€ ์ž˜ ์•ˆ๋จน๋Š”๋‹ค. ๊ทธ๋Ÿด๋• ๋„์ปค inspect ๋กœ ํ™•์ธํ•˜์ž.

network ํ™•์ธ

$ ip addr show
or
$ ifconfig
$ docker attach nonenetcontainer
# ์ ‘์†ํ›„
$ cat /etc/hosts
$ ip -4 addr

attach ํ–ˆ๋˜๊ฑธ detach ํ•˜๊ธฐ ์œ„ํ•ด์„  CTRL-p CTRL-q ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

network ๋ฐฉ์‹

# ๋„คํŠธ์›Œํฌ ๋ฐฉ์‹ ํ™•์ธํ•˜๊ธฐ ( ๋“œ๋ผ์ด๋ฒ„ ) bridge , host , none ( ๋„ค์ž„ ๊ธฐ์ค€ )
$ docker network ls
# ๊ธฐ๋ณธ ๋„คํŠธ์›Œํฌ๋Š” bridge ๋ฐฉ์‹์ด๋‹ค.
# ๋‹ค๋ฅธ๋ฐฉ์‹์œผ๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด
$ docker run --new={๋„คํŠธ์›Œํฌ ํƒ€์ž…}
  1. birdge

    • ๋„คํŠธ์›Œํฌ์—์„œ ๋ธŒ๋ฆฟ์ง€๋ž€ ํ˜ธ์ŠคํŠธ ์„œ๋ฒ„์˜ ๋„คํŠธ์›Œํฌ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ๊ฐ€์ƒํ™”๋œ ๋จธ์‹ ๋“ค๋„ ๋™์ผํ•œ ๋„คํŠธ์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
    • docker network inspect bridge ๋กœ ๋ธŒ๋ฆฟ์ง€ ๋ชจ๋“œ์˜ ์ž์„ธํ•œ ๋‚ด์šฉ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  2. host

    • ์ด ๋ฐฉ์‹์€ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋…๋ฆฝ์ ์ธ ๋„คํŠธ์›Œํฌ ์˜์—ญ์„ ๊ฐ–์ง€ ์•Š๊ณ  host ์™€ ๋„คํŠธ์›Œํฌ๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•œ๋‹ค.
    • ๋”ฐ๋ผ์„œ ํ•ด๋‹น ์ปจํ…Œ์ด๋„ˆ๋Š” host ์˜ ๋„คํŠธ์›Œํฌ ์ •๋ณด์™€ ๋™์ผ์‹œ ํ•˜๊ฒŒ ๊ฐ€์ ธ๊ฐ€๊ฒŒ ๋œ๋‹ค.
    • ๋”ฐ๋ผ์„œ docker0 ์— ๋ฐ”์ธ๋”ฉ ๋˜์ง€ ์•Š๋Š”๋‹ค.
  3. container

    • ๋‹ค๋ฅธ ์ปจํ…Œ์ด๋„ˆ์˜ ๋„คํŠธ์›Œํฌ ํ™˜๊ฒฝ์„ ๊ฐ€์ ธ์˜จ๋‹ค. ์ฆ‰, ip ์™€ mac ์ฃผ์†Œ๋ฅผ ๋™์ผ์‹œ ํ•˜๊ฒŒ ๊ตฌ์„ฑํ•œ๋‹ค.
    • docker run --name web03 --net=container:{์ปจํ…Œ์ด๋„ˆ id} -d nginx:alpine
  4. none

    • ์ด ์˜ต์…˜์€ ๊ฒฉ๋ฆฌ๋œ ๋„คํŠธ์›Œํฌ ํ™˜๊ฒฝ์€ ๊ฐ–์ง€๋งŒ, ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์—†๋Š” ์ƒํƒœ๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•˜๊ฒŒ ๋œ๋‹ค.

container ์™ธ๋ถ€ ํ†ต์‹ 

  • ๊ฐ ์ปจํ…Œ์ด๋„ˆ๋Š” ๊ฒฉ๋ฆฌ๋œ ํ™˜๊ฒฝ์—์„œ ๊ฐ๊ฐ์˜ mac ์ฃผ์†Œ์™€ ip ์ฃผ์†Œ๋ฅผ ๋ถ€์—ฌ ๋ฐ›๋Š”๋‹ค.
  • ๊ฐ ์ปจํ…Œ์ด๋„ˆ๋“ค์€ host ์™€ ํ†ต์‹ ํ•˜๊ธฐ ์œ„ํ•ด bridge ๋ฐฉ์‹์œผ๋กœ ๋ฐ”์ธ๋”ฉ ๋˜์–ด์žˆ๋Š”๋ฐ ๊ฐ ๋ถ€์—ฌ๋ฐ›์€ ip ๋กœ ์ž์œ ๋กญ๊ฒŒ ํ†ต์‹  ๊ฐ€๋Šฅ.
  • ์˜ˆ๋ฅผ ๋“ค๋ฉด web ์„œ๋น„์Šค๋ฅผ ํ•˜๋Š” container ๋Š” 80 ๋ฒˆ ํฌํŠธ์—์„œ ๋ฆฌ์Šค๋‹์„ ํ•˜๊ณ ์žˆ๋‹ค. ์ด ํฌํŠธ๋Š” ์™ธ๋ถ€์™€ ํ†ต์‹ ์ด ๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ ์ด๋•Œ ํฌํŠธ๋ฅผ ์™ธ๋ถ€๋กœ ์ง€์ •ํ•œ๋‹ค. -p 8080:80 ์œผ๋กœ ์˜ต์…˜์„ ์ฃผ๊ฒŒ ๋˜๋ฉด docker host ์˜ 8080 ํฌํŠธ๋กœ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ์ปจํ…Œ์ด๋„ˆ์˜ 80 ํฌํŠธ๋กœ ํ•ด๋‹น ์š”์ฒญ์„ forwarding ํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.
  • docker ps -a or netstat -nlp | grep 8080 ๋กœ ํ™•์ธ
  • link ๋ฐฉ์‹์€ ํ•˜๋‚˜์˜ ํ˜ธ์ŠคํŠธ ์‚ฌ์ด์—์„œ ์‹คํ–‰๋˜๋Š” ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์ด์—์„œ๋งŒ ์—ฐ๊ฒฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ๋‹ค์ˆ˜์˜ host ๊ฐ€ ์กด์žฌํ–ˆ์„๋•Œ, ๋‹ค๋ฅธ host ์˜ ์ปจํ…Œ์ด๋„ˆ์—๋Š” ์ ‘๊ทผํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค.
  • ์ด ๊ฒฝ์šฐ docker swarm ๊ฐ™์€ orchestration ํˆด์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ dynamic DNS ๋ฅผ ๊ตฌ์ถ•ํ•ด์•ผํ•œ๋‹ค.

Warning: >The --link flag is a legacy feature of Docker. It may eventually be removed. Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using --link. One feature that user-defined networks do not support that you can do with --link is sharing environmental variables between containers. However, you can use other mechanisms such as volumes to share environment variables between containers in a more controlled way.

** dynamic DNS ๋ž€? - ๊ฐ„๋‹จํ•˜๊ฒŒ ip ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๊ทธ๊ฑธ ์•Œ์•„์ฑ„์„œ ์™ธ๋ถ€ DNS ์„œ๋ฒ„์— ์•Œ๋ ค์ค€๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ip ๊ฐ€ ๋™์ ์œผ๋กœ ๋ฐ”๋€Œ๋Š” ํ™˜๊ฒฝ์—์„œ๋„ ์‚ฌ์šฉ์ž๋Š” ๋ฐ”๋€ ip ๋ฅผ ์•Œ ํ•„์š” ์—†์ด domain name ๋งŒ ์•Œ๋ฉด ๋™์ผํ•˜๊ฒŒ ์ ‘์† ํ•  ์ˆ˜ ์žˆ๋‹ค.

ยฉ 2021 Merlin.ho, Built with Gatsby