[docker] ๋„์ปค๐Ÿ‹ ์ฐจ๊ทผ์ฐจ๊ทผ ์•Œ์•„๋ณด๊ธฐ

๋ฏธ๋ฃจ๊ณ  ๋ฏธ๋ฃจ๋˜ Docker.. ์ด์   ํ”ผํ•  ์ˆ˜ ์—†์–ด์„œ ๊ณต๋ถ€๋ฅผ ํ•ด๋ณด์•˜๋‹ค. ๋ญ ๋‹ค๋“ค docker ๋งŽ์ด ๋“ค์–ด๋ณด์•˜๊ตฌ, ์—ฌ๋Ÿฌ ๋ธ”๋กœ๊ทธ์—์„œ ์ž์„ธํžˆ ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์—! ์„ค๋ช…์€ PASS~ ๋‚˜๋Š” ๋ฐ”๋กœ ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ์‰ฝ๊ณ  ๋น ๋ฅด๊ณ  ์—๋Ÿฌ ์ตœ๋Œ€ํ•œ ์—†์ด ์„ค์น˜ ํ•  ์ˆ˜ ์žˆ์„์ง€ ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค. ํ›„ํ›„ ์—ฌ๋Ÿฌ ์—๋Ÿฌ์‚ฌํ•ญ์„ ๊ฑฐ์น˜๋ฉด์„œ ์„ฑ๊ณตํ–ˆ์œผ๋‹ˆ, ์ž˜ ๋”ฐ๋ผํ•˜๋ฉด ๋‹น์‹ ๋„ docker ๋งˆ์Šคํ„ฐ~

์ฐจ๊ทผ์ฐจ๊ทผ ํ•ด๋ณด์ž!

0. docker ์„ค์น˜

๋จผ์ € ๊ฐ€์žฅ ์ตœ์‹  ์ƒํƒœ๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ ์œ„ํ•ด apt๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค.

## ์—…๋ฐ์ดํŠธ์™€ ์‚ฌ์ „ ์„ค์น˜
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common

## Docker repository key ์ถ”๊ฐ€ํ•˜๋Š” ๋ช…๋ น์–ด
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

## Docker repository๋ฅผ Add ํ•œ๋‹ค. 
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

## ์‹œ์Šคํ…œ์„ ์ตœ์‹ ์œผ๋กœ ํ•˜๊ธฐ ์œ„ํ•œ ๋‹ค์‹œ ์—…๋ฐ์ดํŠธ์™€ ์—…๊ทธ๋ ˆ์ด๋“œ
sudo apt update
sudo apt upgrade

์ด์ œ ๋„์ปค๋ฅผ ์„ค์น˜ํ•ด ์ค€๋‹ค!

sudo apt install docker-ce

์„ค์น˜๋œ ๋ฒ„์ „ ํ™•์ธ ๋ช…๋ น์–ด

apt-cache policy docker-ce

๋„์ปค๊ฐ€ ์ž˜ ์„ค์น˜ ๋˜์—ˆ๋Š”์ง€ ์•„๋ž˜๋ช…๋ น์–ด๋กœ ํ™•์ธํ•ด๋ณด๋ฉด

sudo systemctl status docker

๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์ด ๋œฌ๋‹ค ๊ทธ๋Ÿผ ์„ฑ๊ณต!

image

Docker๊ฐ€ ์‹คํ–‰ ๋˜๊ณ  ์žˆ์„ ๊ฒฝ์šฐ Docker0๋ผ๋Š” ๋„คํŠธ์›Œํฌ ์ธํ„ฐํŽ˜์ด์Šค๋„ ์ƒ์„ฑ๋˜๋‹ˆ๊นŒ ifconfig ๋กœ ํ™•์ธํ•ด๋ณด์ž docker0 ๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธ!

image

์ด์ œ ๋„์ปค๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค~ ใ…Žใ…Ž


1. docker login

๋„์ปค ์„ค์น˜๋ฅผ ํ–ˆ๋‹ค๋ฉด, ์•„๋ž˜ docker ์‚ฌ์ดํŠธ์—์„œ ํšŒ์›๊ฐ€์ž…์„ ํ•ด์ค€๋‹ค.

https://hub.docker.com/

๋‹ค์Œ์€, ubuntu ์—์„œ ๋กœ๊ทธ์ธ ํ•ด์ฃผ๊ธฐ! ๋ช…๋ น์–ด๋Š” docker login

๋กœ๊ทธ์ธ๋ถ€ํ„ฐ ์—๋Ÿฌ๊ฐ€๋‚ฌ๋‹ค! ์ด๋Ÿด์ค„ ์•Œ์•˜์ง€~!

login_error

์ผ๋‹จ ๋‚˜๋Š” ์•„๋ž˜ ๋ช…๋ น์–ด๋กœ ํ•ด๊ฒฐ์ด ๋˜์—ˆ๋‹ค.

sudo apt install gnupg2 pass

์งœ์ž”~ ๋กœ๊ทธ์ธ ์„ฑ๊ณต!

login_success


2. nvidia-docker ์„ค์น˜

docker ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ•˜๊ธฐ ์ „์—, ๋‚ด ํŒŒ์ผ์ด ์–ด๋–คํ™˜๊ฒฝ์—์„œ ๋Œ์•„๊ฐ€์•ผ ํ•˜๋Š”์ง€ ์•Œ์•„์•ผ ํ•œ๋‹ค.

๋‚˜๋Š” python ํŒŒ์ผ์„ ๋Œ๋ฆฌ๋Š”๋ฐ GPU ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— cuda, cudnn, ubuntu ๋ฒ„์ „์ด ํ•„์š”ํ•˜๋‹ค. ์ฆ‰, GPU ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Docker ์„ ์„ค์น˜ํ•œ ํ›„ nvidia-docker ์ด๋ž€ ๊ฒƒ๋„ ์„ค์น˜ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์„ค์น˜๋Š” ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ•˜๋‚˜์”ฉ ์‹คํ–‰ํ•˜์ž!

์ž ๊น~ ๋‚œ ํ•˜๋‚˜์”ฉ ํ•˜๊ธฐ ๊ท€์ฐฎ์•„์š” ํ•˜๋Š” ์‚ฌ๋žŒ์€ 1) ~3) ํ”„๋กœ์„ธ์Šค๋ฅผ 0) ํ•œ๋ฒˆ๋งŒ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ใ…Žใ…Ž ๋‚˜๋Š” ๋ฌด์„œ์›Œ์„œ ํ•˜๋‚˜์”ฉ ํ•ด๋ด„

0) 1), 2), 3) ๋ช…๋ น์–ด๋ฅผ ํ•œ๋ฒˆ์— ๋Œ๋ฆฌ๋Š” ๋ช…๋ น์–ด! ์ด๊ฑฐ ํ•œ ๋‹ค์Œ์— 4)๋กœ ๊ฐ€์„ธ์š”~

$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
   && curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add - \
   && curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list

์ฐจ๊ทผ์ฐจ๊ทผ ํ•˜๊ณ ์‹ถ์–ด์š”~ ํ•˜๋Š” ์‚ฌ๋žŒ์€ ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ ์ญ‰ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค!

1)

curl -s -L <https://nvidia.github.io/nvidia-docker/gpgkey> | \\
  sudo apt-key add -

2)

distribution=$(. /etc/os-release;echo $ID$VERSION_ID)

3)

curl -s -L <https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list> | \\
  sudo tee /etc/apt/sources.list.d/nvidia-docker.list

4)

sudo apt-get update

5) ์„ค์น˜!

sudo apt-get install -y nvidia-docker2

์„ค์น˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด docker ์„œ๋น„์Šค๋ฅผ ์žฌ์‹œ์ž‘ ํ•ด๋ณด์ž!

sudo systemctl restart docker

docker๊ฐ€ gpu๋ฅผ ์ž˜ ์ฐพ์•„์„œ ์“ฐ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด!

docker run --rm --gpus all ubuntu:18.04 nvidia-smi

gpu๋ฅผ ์ž˜ ์ฐพ์€ ๊ฒƒ์„ ํ™•์ธํ•˜์˜€๋‹ค ใ…Žใ…Ž

image


3. ํ•„์š”ํ•œ ์ฟ ๋‹ค, ์šฐ๋ถ„ํˆฌ ํ™˜๊ฒฝ pull ํ•ด์˜ค๊ธฐ!

๋‹ค์Œ์œผ๋กœ๋Š”, ๋‹ค์Œ ๋ช…๋ น์–ด๋กœ ํ•„์š”ํ•œ ํ™˜๊ฒฝ์„ pull ํ•ด์™”๋‹ค. docker ํ™ˆํŽ˜์ด์ง€์—์„œ ์ด๊ฒƒ์ €๊ฒƒ pull ํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ ==> [https://hub.docker.com/search?q=docker%20pull%20cuda&source=verified](https://hub.docker.com/search?q=docker%20pull%20cuda&source=verified) ์—ฌ๊ธฐ์„œ ๋ณธ์ธ์ด ํ•„์š”ํ•œ๊ฑด pull ํ•ด์˜ค์ž!

docker pull nvidia-cuda-10.0-cudnn7-devel-ubuntu18.04

์งœ์ž”! pull์ด ๋งค์šฐ ์ž˜ ๋˜์—ˆ๋‹ค

pull


4. Dockerfile ๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž

๋‚˜๋Š” python code๋ฅผ ๋Œ๋ฆฌ๋Š” docker์„ ๋งŒ๋“ค์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑ๋˜์—ˆ๋‹ค. ํ˜น์‹œ ๋ณธ์ธ์ด js ๋‚˜ c ์ฝ”๋“œ๋‹ค ํ•˜๋ฉด ์ข€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์œผ๋‹ˆ ์กฐ์‹ฌํ•ฉ์‹œ๋‹ค~

๋จผ์ €, ๋ณธ์ธ์ด ๋Œ๋ฆฌ๊ณ  ์‹ถ์€ ํŒŒ์ผ๊ณผ ๋ชจ๋“  ํด๋”, ๋ฐ์ดํ„ฐ ๋“ฑ๋“ฑ์„ ํ•˜๋‚˜์˜ ํด๋” ์•„๋ž˜์— ๋‹ค ์ •๋ฆฌํ•ด์ค์‹œ๋‹ค.

๋‚˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด GONGIN ์ด๋ผ๋Š” ํด๋” ์•„๋ž˜์— ์‹น! ์ •๋ฆฌ๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค! ์ € ํด๋”์™€ ํŒŒ์ผ๋“ค๋งŒ ์žˆ์œผ๋ฉด ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜์˜€๋‹ค. ๊ทธ๋ฆฌ๊ณ , ์ด ํด๋” ์•ˆ์— Dockerfile์„ ์œ„์น˜์‹œ์ผœ ์ค€๋‹ค.

image

Dockerfile์€ linux ์—์„œ vim ์œผ๋กœ ๋งŒ๋“ค์–ด์คฌ๋‹ค. ๋„์ปคํŒŒ์ผ์€ ์ด ์ด๋ฆ„์ด default ์ธ๊ฑฐ๊ฐ™์œผ๋‹ˆ dockerfile ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฐ”๊พธ์ง€ ๋ง์ž! ๋ฐ”๊ฟ”๋„ ๋˜๋Š”์ง„ ์ž˜ ๋ชจ๋ฅด๊ฒŸ๋Š”๋ฐ Dockerfile ์ด๋ผํ•˜๋‹ˆ Dockerfile ์ด๋ผ๊ณ  ํ•˜์ž..ใ…Žใ…Ž ์—๋Ÿฌ๋Š” ํ”ผํ•˜๋Š”๊ฒŒ..

vim Dockerfile

ํŒŒ์ผ์˜ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•˜์˜€๋‹ค.

FROM nvidia/cuda:10.0-cudnn7-devel-ubuntu18.04

RUN echo 'export PATH=/usr/local/cuda-10.0/bin${PATH:+:${PATH}}' >> ~/.bashrc
RUN echo 'export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}' >> ~/.bashrc
RUN echo 'export PATH=/usr/local/cuda/bin:/$PATH' >> ~/.bashrc
RUN echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}' >> ~/.bashrc

RUN apt-get update -y
RUN apt-get -y install libgl1-mesa-glx
RUN apt-get install -y python3-pip python-dev build-essential
RUN pip3 install --upgrade pip

ADD . /GONGIN
WORKDIR /GONGIN
RUN python3 -m pip install -r requirements.txt

CMD python3 ./toothbrush_head_final.py

5. image build ์‹œํ‚ค๊ธฐ

์ด์ œ ๋งŒ๋“  Dockerfile ์„ build ์‹œํ‚ค์ž! ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ์‹คํ–‰ํŒŒ์ผ๋กœ ๋งŒ๋“ ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

๋ช…๋ น์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด์ฃผ๋ฉด๋œ๋‹ค. -t ๋Š” ํƒœ๊ทธ๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค ๋ญ ์ด๋Ÿฐ๋œป์œผ๋กœ ์•Œ๋ฉด ๋œ๋‹ค.

docker build -t [๋„์ปค์ด๋ฏธ์ง€์ด๋ฆ„]:[ํƒœ๊ทธ] [๋„์ปคํŒŒ์ผ๊ฒฝ๋กœ]

๋‚˜์˜๊ฒฝ์šฐ, ๋„์ปคํŒŒ์ผ์ด ์žˆ๋Š” ์œ„์น˜์—์„œ ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•˜์˜€๊ณ , ๋„์ปค์ด๋ฏธ์ง€์ด๋ฆ„์€ noah, ํƒœ๊ทธ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ v0.1, v0.2 ์ด๋Ÿฐ์‹์œผ๋กœ ํ•ด์ฃผ์—ˆ๋‹ค. ใ…Žใ…Ž

docker build -t noah:v0.1 .

๋„์ปค์ด๋ฏธ์ง€ ์ž˜ ๋นŒ๋“œ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž!

docker images

image

๋ณด์ด๋Š”๊ฐ€ ์ € ์ˆ˜๋งŒ์€ version.. ๋‚˜์˜ ์‹œํ–‰์ฐฉ์˜ค๋“ค ใ…Žใ…Ž

6. docker image ์‹คํ–‰์‹œํ‚ค๊ธฐ

์—ฌ๊ธฐ๊นŒ์ง€ ์˜จ ๋‹น์‹  ์นญ์ฐฌํ•ด..! ๊ทธ๊ฑด ๋‚˜์—๊ฒŒ ํ•˜๋Š”๋ง ํžˆํžˆ

์ด์ œ ์‹คํ–‰์„ ์‹œ์ผœ๋ด…์‹œ๋‹ค.

docker run noah:v0.1

์ž˜๋Œ์•„๊ฐ„๋‹ค~ ๋šœ์‰ฌ๋šœ์‰ฌ~

7. TEST

์ด์ œ ๋‹ค๋ฅธ ์„œ๋ฒ„์—์„œ๋„ ๋Œ์•„๊ฐ€๋Š”์ง€(๋ฐฐํฌ๋ฅผ ์œ„ํ•ด ์ฒดํฌ!) ํ™•์ธํ•ด๋ณด์ž, ๋จผ์ € docker image ๋ฅผ tar ํŒŒ์ผ๋กœ save ํ•ด์ค€๋‹ค.

save -o [์ด๋ฆ„์œผ๋กœ ํ•˜๊ณ ์‹ถ์€๊ฒƒ] [์ €์žฅํ•  ์ด๋ฏธ์ง€]
docker save -o noah_docker_v0.10.tar noah:v0.10

ใ…Žใ…Ž ์ด๊ฒƒ์ €๊ฒƒ ์‹œ๋„ํ•˜๋‹ค๋ณด๋‹ˆ 10๋ฒˆ์งธ docker image๋„ ์ƒ๊น€ ใ…‹

์ƒ์„ฑ๋œ tar ํŒŒ์ผ์„ ๋‹ค๋ฅธ ์„œ๋ฒ„๋กœ scp ํ•ด์ค€ ํ›„, ๋กœ๋“œ๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค.

docker load -i noah_docker_v0.10.tar

์•„๋ž˜ ์ด๋ฏธ์ง€์™€ ๊ฐ™์ด ๋กœ๋“œ๋œ๋‹ค.. ๋กœ๋“œ์ค‘~

image

์ด์ œ ์‹คํ–‰์‹œ์ผœ ๋ณธ ํ›„ ์ž˜ ๋Œ์•„๊ฐ€๋ฉด ์„ฑ๊ณต!!

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค~

segmentation fault (core dumped) ์—๋Ÿฌ ํ•ด๊ฒฐ

๋˜ ์—๋Ÿฌ๋‹ค.. ๊ทธ๋ƒฅ ๊ฐ€์ƒํ™˜๊ฒฝ์—์„œ๋Š” ์ž˜ ๋Œ์•„๊ฐ€๋Š” ํŒŒ์ด์ฌ ์ฝ”๋“œ๊ฐ€, ๋„์ปค ๋นŒ๋“œํ•ด์„œ ์‹คํ–‰์‹œํ‚ค๋‹ˆ ์ฝ”์–ด ๋คํ”„ ์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค.. ์ฐพ์•„๋ณด๋‹ˆ

์•„๋ž˜ ๋ช…๋ น์–ด๋กœ ํ•ด๊ฒฐํ–ˆ๋‹ค๋Š” ์‚ฌ๋žŒ์ด ์žˆ๋Š”๋ฐ..

sudo pip install docker-compose

๋‚œ ํ•ด๊ฒฐ์ด ์•ˆ๋œ๋‹ค ์•„์•„์•…!

ignore warning by

os.environ['TF_CPP_MIN_LOG_LEVEL'] = "2"

ํ•„์š”ํ•œ cuda image๋ฅผ pull ํ•ด์˜ค๊ณ , ๊ทธ pull ํ•ด์˜จ image ์— ๋‚ด๊ฐ€ ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ์ถ”๊ฐ€๋กœ install ํ•˜์—ฌ ๋‚˜๋งŒ์˜ custum docker image ๋ฅผ ๋งŒ๋“ค์–ด์„œ, ๋ฐฐํฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ฒŒ docker hub ์— ์˜ฌ๋ฆฌ๋Š” ๊ฒƒ ๊นŒ์ง€๋Š” [docker] ๋„์ปค๐Ÿ‹ ์ฐจ๊ทผ์ฐจ๊ทผ ์•Œ์•„๋ณด๊ธฐ 2 ์—ฌ๊ธฐ ์ฐธ๊ณ !


reference

[1] docker ์„ค์น˜ ์ฐธ๊ณ ์‚ฌ์ดํŠธ

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ