지식/Kubernetes

#2. Docker 로 ubuntu python 환경 구축하기

ZeroAct 2023. 1. 14.
728x90
반응형

Image

이미지는 개발자가 정의한 환경을 구축하기 위한 명령어 집합이라고 생각할 수 있다.

"운영체제는 뭐를 설치해주시고 python은 몇 버전을 사용해주시고 pytorch 버전은 이거를 사용해주시고 ..." 를 템플릿화 시켜놓은 것이다.

실습

실생활에서 도커 없이 여러사람이 동시에 동일한(진짜로 똑같은) 환경에서 작업하는 것은 매우 힘들다.

아주 간단한 예시로 ubuntu 20.04 환경에 python 3.9 버전을 설치하여 공유해보자.

 

1. Google "How to install python 3.9 on ubuntu 20.04?"

대충 여러 사이트가 나온다.

https://linuxize.com/post/how-to-install-python-3-9-on-ubuntu-20-04/이렇게 하라고 나온다.

 

2. Dockerfile을 만든다.

FROM ubuntu:20.04
RUN apt update -y
RUN apt install software-properties-common -y
RUN add-apt-repository ppa:deadsnakes/ppa -y
RUN apt install python3.9 -y

 

첫 줄은 다른사람이 만들어 놓은 템플릿에 이어서 하겠다는 것이다.
보통 운영체제나 nvidia driver 같이 설치하기 까다로운 작업들은 공식 repository에서 가져와서 시작한다.

사용가능한 ubuntu는 https://hub.docker.com/ 에서 검색하면 된다.
ubuntu repository를 검색하고 원하는 버전을 검색하면 TAG를 얻을 수 있다.
이미지명:버전 으로 가져올 수 있다.

 

나머지 줄은 python 설치하는 법 검색을 통해 얻은 명령어들이다.
앞에 RUN 만 붙여주면 된다.

3. 이미지를 빌드한다.

$ docker build -t ubuntu_python .
[+] Building 162.2s (9/9) FINISHED
 => [internal] load build definition from Dockerfile                                                                               0.0s 
 => => transferring dockerfile: 198B                                                                                               0.0s 
 => [internal] load .dockerignore                                                                                                  0.0s 
 => => transferring context: 2B                                                                                                    0.0s 
 => [internal] load metadata for docker.io/library/ubuntu:20.04                                                                    0.7s 
 => CACHED [1/5] FROM docker.io/library/ubuntu:20.04@sha256:0e0402cd13f68137edb0266e1d2c682f217814420f2d43d300ed8f65479b14fb       0.0s 
 => [2/5] RUN apt update -y                                                                                                        8.6s 
 => [3/5] RUN apt install software-properties-common -y                                                                          129.9s 
 => [4/5] RUN add-apt-repository ppa:deadsnakes/ppa -y                                                                             7.3s 
 => [5/5] RUN apt install python3.9 -y                                                                                            14.6s
 => exporting to image                                                                                                             1.0s
 => => exporting layers                                                                                                            1.0s
 => => writing image sha256:02268cc17bc347a38486c518ce6940d71be9594137b1bef1653cffc0856defae                                       0.0s
 => => naming to docker.io/library/ubuntu_python                                                                                   0.0s

-t 옵션으로 원하는 이미지 이름을 지정하고,

. 은 현재 디렉터리의 Dockerfile 을 찾아서 빌드한다는 의미이다.

 

출력된 결과물을 보면 우리가 작성했던 FROM, RUN 들이 한 줄씩 실행되는 것을 볼 수 있다.

이것이 하나하나도 이미지이다.

이미지 업로드, 다운로드는 저 이미지 단위로 이루어지기 때문에,

최적화를 위해 합칠 수 있다면 합쳐주는 것이 좋다.

(너무 다 합치면 가독성이 떨어지므로 연관된 것만 합치도록 하자.)

FROM ubuntu:20.04
RUN apt update -y && \
    apt install software-properties-common -y && \
    add-apt-repository ppa:deadsnakes/ppa -y && \
    apt install python3.9 -y
$ docker build -t ubuntu_python .
[+] Building 0.9s (6/6) FINISHED
 => [internal] load build definition from Dockerfile                                                                    0.0s 
 => => transferring dockerfile: 32B                                                                                     0.0s
 => [internal] load .dockerignore                                                                                       0.0s 
 => => transferring context: 2B                                                                                         0.0s 
 => [internal] load metadata for docker.io/library/ubuntu:20.04                                                         0.8s 
 => [1/2] FROM docker.io/library/ubuntu:20.04@sha256:0e0402cd13f68137edb0266e1d2c682f217814420f2d43d300ed8f65479b14fb   0.0s
 => CACHED [2/2] RUN apt update -y &&     apt install software-properties-common -y &&     add-apt-repository ppa:dead  0.0s 
 => exporting to image                                                                                                  0.0s 
 => => exporting layers                                                                                                 0.0s 
 => => writing image sha256:b49705fdf2340d7754be85d2de796911d83cfb0b218f930645168505a4d8e581                            0.0s 
 => => naming to docker.io/library/ubuntu_python                                                                        0.0s

빌드 단계가 5에서 2로 줄었다.

 

4. 이미지 실행

이미지를 실행한다는 것은 container를 생성한다는 의미이다.

$ docker run -it ubuntu_python
root@e0605b3950fd:/# python3.9 --version
Python 3.9.16

-it 옵션은 container 를 실행한 후 쉘을 interactive 하게 띄워준다는 의미이다.

python3.9 를 설치했으므로 python3.9를 통해 python을 사용할 수 있다.

 

쉘을 띄우지 않고도 container에게 명령을 내릴 수 있다.

$ docker run ubuntu_python python3.9 --version
Python 3.9.16

 

만약 도커를 사용해 자기가 만든 어떤 서비스를 바로 실행하고 싶다면 다음과 같이 입력하면 된다.

$ docker run [내 이미지 명] [실행 명령]

 

728x90
반응형

'지식 > Kubernetes' 카테고리의 다른 글

#1. Container 와 Container Orchestration  (0) 2023.01.03
Kubernetes 자격증 도전 기록  (0) 2023.01.03

댓글