본문 바로가기
  • 경제적 자유를 얻는 그날까지
엔지니어링/인공지능

[ 머신러닝 ] 모두를 위한 머신러닝 도커환경설정

by 베니스상인 2021. 12. 28.

 

개발환경

  • OS: Mac
  • Docker

 

모두를 위한 딥러닝 tensorflow 과정을 학습하기 위한 개발환경 구성

 

1. MacOS용 도커 다운로드 및 설치

  • 다운받아서 설치하면 된다.
  • docker  계정을 등록해야 한다.

 

https://www.docker.com/get-started

 

Get Started with Docker | Docker

Learn about the complete container solution provided by Docker. Find information for developers, IT operations, and business executives.

www.docker.com

 

설치가 완료되면 터미널에서 hello-world 이미지가 정상적으로 실행되는지 확인한다.

(base) swiftui-MacBook-Pro:~ swift$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

 

2. Tensorflow용 docker를 다운받아서 설치한다.

(base) swiftui-MacBook-Pro:~ swift$ docker pull deeplearningzerotoall/tensorflow
Using default tag: latest
latest: Pulling from deeplearningzerotoall/tensorflow
32802c0cfa4d: Pull complete 
da1315cffa03: Pull complete 
fa83472a3562: Pull complete 
f85999a86bef: Pull complete 
2e42b3b21fc4: Pull complete 
3afb25e85c85: Pull complete 
9151c4e76014: Pull complete 
353f8b2e8611: Pull complete 
224ded802557: Pull complete 
7d7dc91d19d6: Pull complete 
8f3562f80ef0: Pull complete 
415734973548: Pull complete 
1d050ddfab69: Pull complete 
6839be11255c: Pull complete 
ef6495e8e5b2: Pull complete 
31562b302457: Pull complete 
738ac0410428: Pull complete 
Digest: sha256:9fc44968ef92269677b32f9e0ef5154e1dd4f975909d412ca43a59dc41773c27
Status: Downloaded newer image for deeplearningzerotoall/tensorflow:latest
docker.io/deeplearningzerotoall/tensorflow:latest

 

이미지가 정상적으로 설치되었는지 확인한다.

(base) swiftui-MacBook-Pro:~ swift$ docker images
REPOSITORY                         TAG       IMAGE ID       CREATED        SIZE
hello-world                        latest    feb5d9fea6a5   3 months ago   13.3kB
deeplearningzerotoall/tensorflow   latest    4d3d9d563502   2 years ago    3.32GB

 

3. docker 실행한다.

(base) swiftui-MacBook-Pro:~ swift$ docker run -i -t --name tf -p 8888:8888 -p 6006:6006 deeplearningzerotoall/tensorflow /bin/bash
root@71b2b45d517c:/workspace#

 

4. github에 있는 소스코드를 다운로드한다.

root@71b2b45d517c:/workspace# git clone https://github.com/shlee853/TensorFlow.git
Cloning into 'TensorFlow'...
remote: Enumerating objects: 1223, done.
remote: Total 1223 (delta 0), reused 0 (delta 0), pack-reused 1223
Receiving objects: 100% (1223/1223), 1.85 MiB | 5.43 MiB/s, done.
Resolving deltas: 100% (767/767), done.

root@71b2b45d517c:/workspace# cd TensorFlow/

 

5. 주피터 노트북 사용을 위해 필요구성요소들을 설치하고 실행한다.

root@71b2b45d517c:/workspace/TensorFlow# pip install --upgrade pip
root@71b2b45d517c:/workspace/TensorFlow# pip install -r requirements.txt

root@71b2b45d517c:/workspace/TensorFlow# jupyter notebook --ip 0.0.0.0 --allow-root
[I 23:49:03.775 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
[I 23:49:04.169 NotebookApp] Serving notebooks from local directory: /workspace/TensorFlow
[I 23:49:04.169 NotebookApp] The Jupyter Notebook is running at:
[I 23:49:04.169 NotebookApp] http://(71b2b45d517c or 127.0.0.1):8888/?token=9540f0167d896062f68e85f413c43ea09e19a533c56739b4
[I 23:49:04.169 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 23:49:04.175 NotebookApp] No web browser found: could not locate runnable browser.
[C 23:49:04.175 NotebookApp] 
    
    To access the notebook, open this file in a browser:
        file:///root/.local/share/jupyter/runtime/nbserver-56-open.html
    Or copy and paste one of these URLs:
        http://(71b2b45d517c or 127.0.0.1):8888/?token=9540f0167d896062f68e85f413c43ea09e19a533c56739b4

 

위 로그에서 맨 아래쪽 http 주소는 주피터 노트북이 설치된 주소이므로 브라우저에 아래와 같이 붙혀넣는다.

토큰 뒤에 있는 코드는 실행마다 변경될 수 있으므로 그에 맞게 넣어줘야 한다.

http://127.0.0.1:8888/?token=9540f0167d896062f68e85f413c43ea09e19a533c56739b4

 

 

이상 설정이 끝났으므로 주피터 노트북에서 스텝별로 실행하면서 공부하면 된다.

 

텐서보드를 사용하는 방법

 

도커에서 실행되는 주피터 노트북이 터미널을 하나 점유하고 있으므로 새로운 터미널을 도커 실행환경으로 열어야 한다.

  • docker ps로 컨테이너 번호를 확인한다.
  • 새로운 터미널에서 기존 컨테이너의 도커를 실행한다.
(base) swiftui-MacBook-Pro:~ swift$ docker ps
CONTAINER ID   IMAGE                              COMMAND       CREATED        STATUS        PORTS                                            NAMES
71b2b45d517c   deeplearningzerotoall/tensorflow   "/bin/bash"   35 hours ago   Up 35 hours   0.0.0.0:6006->6006/tcp, 0.0.0.0:8888->8888/tcp   tf

(base) swiftui-MacBook-Pro:~ swift$ docker exec -it 71b2b45d517c bash
root@71b2b45d517c:/workspace#

 

텐서보드를 열어줄 때 로그창이 기록되는 폴더를 확인하고 열어준다.

root@71b2b45d517c:/workspace# tensorboard --logdir=./logs/
TensorBoard 1.12.2 at http://71b2b45d517c:6006 (Press CTRL+C to quit)

 

위 페이지 링크는 주피터 노트북이 사용하는 아이피와 동일하고 포트번호만 6006으로 변경하여 실행하면 텐서보드를 확인할 수 있다.

 

 

 

728x90

댓글