๐Ÿณ Docker & Kubernetes

[Docker] Anaconda/Jupyter notebook์„ dockerfile๋กœ ์„ค์น˜ + ์ปค์Šคํ…€ ์ด๋ฏธ์ง€ ๋งŒ๋“ค๊ธฐ

darly213 2022. 8. 19. 16:48
728x90

1. jupyter/datascience-notebook image ๊ธฐ๋ฐ˜ CMD๋กœ ์‹คํ–‰

CMD๋กœ ์ƒ์„ฑํ•˜๋Š” ๊ณผ์ •

  1. ๋„์ปค ์ด๋ฏธ์ง€ pull
  2. $ docker pull jupyter/datascience-notebook image
  3. ์ปจํ…Œ์ด๋„ˆ run
  4. $ docker run \ -d \ -it \ -p 8000:8888 \ -e GRANT_SUDO=yes --name jupyter
  5. ์ปจํ…Œ์ด๋„ˆ ์ ‘์†
  6. $ docker exec -it jupyter bash
  7. ๋น„๋ฐ€๋ฒˆํ˜ธ ์ƒ์„ฑ
  8. / ipython > from notebook.auth import passwd > passwd() # ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ # ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ # ์•”ํ˜ธํ™” ๋น„๋ฐ€๋ฒˆํ˜ธ ์ถœ๋ ฅ (๋ณต์‚ฌํ•ด๋‘˜ ๊ฒƒ)
  9. jupyter notebook ๋น„๋ฐ€๋ฒˆํ˜ธ ์„ค์ •
  10. / sudo vim ~/.jupyter/jupyter_notebook_config.py # in vim -> insert mode(i) c.NotebookApp.password_required=True **c.NotebookApp.password=**'{passwd๋กœ ์ƒ์„ฑํ•œ ์•”ํ˜ธํ™” ๋น„๋ฐ€๋ฒˆํ˜ธ}' # esc -> :wq / exit
  11. / sudo apt-get update / sudo apt-get install vim -y
  12. ์ปจํ…Œ์ด๋„ˆ ์žฌ์‹คํ–‰
  13. $ docker restart jupyter
  14. hosting ํ•œ ์„œ๋ฒ„ ํ™•์ธ
  15. [localhost:8800](http://localhost:8800) ์œผ๋กœ ์ ‘์† → jupyter server on

2. ubuntu:18.04 image ๊ธฐ๋ฐ˜ dockerfile๋กœ ์ด๋ฏธ์ง€ ์ƒ์„ฑ

  • ์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ

Dockerfile๋กœ ์ฃผํ”ผํ„ฐ ์„œ๋ฒ„ ๊ตฌ์ถ•ํ•˜๊ธฐ

์ฒ˜์Œ์—๋Š” ๋˜‘๊ฐ™์ด dockerfile์„ ๊ตฌ์„ฑํ•ด์„œ ์‹คํ–‰ํ•ด๋ดค์—ˆ๋Š”๋ฐ, ๋‚ด ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰๋˜์ง€ ์•Š๋Š” ๋‚ด์šฉ๋“ค์ด ์žˆ์–ด์„œ ํ‹ˆํ‹ˆํžˆ ์ˆ˜์ •ํ•˜๊ณ  ๋ถ„์„ํ•˜๋ฉด์„œ ๋ณ€๊ฒฝํ•ด๋ณด์•˜๋‹ค. ํ•˜๋‚˜์”ฉ ๋œฏ์–ด๋ณด์ž.

FROM ubuntu:18.04

# setting basic directory
WORKDIR /home
RUN mkdir /soft
RUN mkdir /workspace
RUN apt-get update
RUN apt-get install -y net-tools wget nano lsof

# Anaconda installing
WORKDIR /home/soft
RUN wget https://repo.anaconda.com/archive/Anaconda3-2020.07-Linux-x86_64.sh
RUN bash Anaconda3-2020.07-Linux-x86_64.sh -b -p /home/soft/anaconda
RUN rm Anaconda3-2020.07-Linux-x86_64.sh
ENV PATH /home/soft/anaconda/bin:$PATH

WORKDIR /root/.jupyter
RUN jupyter notebook --generate-config

# jupyter server setting
RUN echo "c.NotebookApp.password = 'sha1:b00ee0c0e13c:9b3f0c11356d567b4b9c516af4aeae5df5a2f1e4'" >> /root/.jupyter/jupyter_notebook_config.py
RUN echo "c.NotebookApp.ip = '0.0.0.0'" >> /root/.jupyter/jupyter_notebook_config.py
RUN echo "c.NotebookApp.password_require=True" >> /root/.jupyter/jupyter_notebook_config.py110a0e6ab9fe
RUN echo "c.NotebookApp.allow_root = True" >> /root/.jupyter/jupyter_notebook_config.py
RUN echo "c.NotebookApp.open_browser = False" >> /root/.jupyter/jupyter_notebook_config.py

# set jupyter server excute location
WORKDIR '/home/soft'

๊ธฐ๋ณธ ์ด๋ฏธ์ง€

  • ubuntu:18.04 image๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒ์„ฑํ–ˆ๋‹ค.

๊ธฐ๋ณธ ์ž‘์—…ํด๋” ์„ค์ •

# setting basic directory
WORKDIR /home
RUN mkdir /soft
RUN mkdir /workspace
RUN apt-get update
RUN apt-get install -y net-tools wget nano lsof
  • ubuntu root ๋ฐ”๋กœ ์•„๋ž˜์— home์ด๋ผ๋Š” ํด๋”๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ทธ ์•ˆ์— ์ž‘์—… ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•˜๋„๋ก ํ–ˆ๋‹ค.
  • ~/home/soft/workspace
  • package ์„ค์น˜

anaconda ์„ค์น˜

# Anaconda installing
WORKDIR /home/soft
RUN wget https://repo.anaconda.com/archive/Anaconda3-2020.07-Linux-x86_64.sh
RUN bash Anaconda3-2020.07-Linux-x86_64.sh -b -p /home/soft/anaconda
RUN rm Anaconda3-2020.07-Linux-x86_64.sh
ENV PATH /home/soft/anaconda/bin:$PATH
  • anaconda๋ฅผ ์„ค์น˜ํ•  work directory๋ฅผ ์ƒˆ๋กœ ์„ค์ •ํ•ด์คฌ๋‹ค.
    • ์ด์ œ home/soft ์•„๋ž˜์—์„œ anaconda ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • anaconda image ์„ค์น˜๋Š” ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค.

dockerfile์„ ์ด์šฉํ•œ anaconda ์„ค์น˜

[๋„์ปค] Dockerfile๋กœ conda ํ™˜๊ฒฝ ๋ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์„ค์น˜๋œ ๋„์ปค ์ƒ์„ฑํ•˜๊ธฐ

Jupyter config ์„ค์ •

WORKDIR /root/.jupyter
RUN jupyter notebook --generate-config
  • config ํŒŒ์ผ ์ƒ์„ฑ์„ ์œ„ํ•ด work directory ๋ฅผ .jupyter ํด๋” ์•„๋ž˜๋กœ ์„ค์ •ํ•˜๊ณ  ์ƒ์„ฑํ–ˆ๋‹ค.
# jupyter server setting
RUN echo "c.NotebookApp.password = 'sha1:b00ee0c0e13c:9b3f0c11356d567b4b9c516af4aeae5df5a2f1e4'" >> /root/.jupyter/jupyter_notebook_config.py
RUN echo "c.NotebookApp.ip = '0.0.0.0'" >> /root/.jupyter/jupyter_notebook_config.py
RUN echo "c.NotebookApp.password_require=True" >> /root/.jupyter/jupyter_notebook_config.py110a0e6ab9fe
RUN echo "c.NotebookApp.allow_root = True" >> /root/.jupyter/jupyter_notebook_config.py
RUN echo "c.NotebookApp.open_browser = False" >> /root/.jupyter/jupyter_notebook_config.py
  • ํ„ฐ๋ฏธ๋„์„ ์‚ฌ์šฉํ•ด์„œ ์„ธํŒ…ํ•  ๋•Œ๋Š” vim์„ ์ผ๋Š”๋ฐ, dockerfile์—์„œ vim์„ ์“ธ ์ˆ˜๋Š” ์—†์œผ๋‹ˆ๊นŒ echo + >>๋กœ ์‚ฝ์ž…ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์„ ์ทจํ–ˆ๋‹ค.
    • password ์„ค์ •
      • password๋Š” sha1๋กœ ์•”ํ˜ธํ™”ํ•ด์„œ ๋งŒ๋“ค์—ˆ๋‹ค. ์ง€๊ธˆ์€ 1234๋‹ค.์ปค์Šคํ…€ํ•ด์„œ ๋‚˜์˜จ ๊ฑธ๋กœ ๋Œ€์ฒดํ•ด๋„ ๋œ๋‹ค. ๋นŒ๋“œ๋Š” ๋ฌผ๋ก  ๋‹ค์‹œ ํ•ด์•ผํ•จ.
      • # generate security password from notebook.auth import passwd passwd(algorithm="sha1"
    • ip๋ฅผ localhost๋กœ ์„ค์ •
    • password require ์„ค์ •
    • root ์ ‘๊ทผ ์„ค์ •

bash ์‹คํ–‰ ํด๋” ์„ค์ •

  • /root/.jupyter์—์„œ๋Š” ๋‹น์—ฐํžˆ anaconda ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ด์ „์— ๋งŒ๋“ค์–ด๋‘” /home/soft๋กœ ์„ค์ •ํ•œ๋‹ค.
  • # set jupyter server excute location WORKDIR '/home/soft'

Image build

$ docker buildx build --platform=linux/amd64 -t notebook:anaconda-jupyter .
  • M1 Mac OS ์—์„œ ๋นŒ๋“œํ•œ ์ด๋ฏธ์ง€๋ผ buildx์™€ ์˜ต์…˜์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค.
  • cache ์—†์œผ๋ฉด ์ง„์งœ ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค(10๋ถ„ ๊ฑธ๋ฆผ)
    • ์ •์ƒ์ด๋‹ˆ๊นŒ ์ซ„์ง€ ๋ง์ž.

Run Container

$ docker run -d -it -p {host-port}:8888 --name jupyter-server mysterias/notebook:anaconda/jupyter
  • ํฌํŠธ ๋ฒˆํ˜ธ๋Š” jupyter notebook ๋‚ด๋ถ€์—์„œ ํฌํŠธ๋ฒˆํ˜ธ๋ฅผ 8888 ์“ฐ๊ธฐ ๋•Œ๋ฌธ์— host๋งŒ ์ปค์Šคํ…€ํ•˜๊ณ  ๋„์ปค ์ปจํ…Œ์ด๋„ˆ ์ชฝ์€ 8888๋กœ ๊ณ ์ •ํ•œ๋‹ค.

Access into container

$ docker exec -it jupyter-server bash
  • ์ด์ œ bash๋กœ ์ ‘์†ํ•ด์„œ jupyter notebook ์ž…๋ ฅํ•˜๋ฉด ์„œ๋ฒ„๊ฐ€ ์ผœ์ง„๋‹ค. localhost:{own-port} ๋กœ ์ ‘๊ทผํ•˜์ž.
  • default password๋Š” 1234์ด๋‹ค.

Result

Docker hub์— ์ „์ฒด ๋„์ปคํŒŒ์ผ๊ณผ ํ•จ๊ป˜ ์—…๋กœ๋“œํ•ด๋‘์—ˆ๋‹ค. pullํ•ด์„œ ์‹คํ–‰ํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค :D

https://hub.docker.com/r/mysterias/notebook

728x90