docker 이미지

2020. 1. 31. 16:29devops/docker

docker 이미지

지금까지 우리가 만들어온 컨테이너는 이미지를 기반으로 만들어진다. 컨테이너에서 무슨일을 하던 기반이 되었던 이미지에는 영향을 끼치지 않는다. 우리가 npm, apt-get install은 npm 레퍼지토리 apt 레퍼지토리 등에서 다운 받습니다. docker도 마찬가지로 docker hub라는 곳에서 이미지를 다운받습니다.

docker hub

이 이미지는 공식적으로 인정받은 이미지 부터 개인 개발자가 올리고 다운받을 수 있습니다. 그리고 git과 비슷하게 개인의 이미지를 private로 올릴 수 있습니다. 하지만 하나의 공간만이 무료이고 나머지는 유료라서 주의 하셔야합니다. 

 

직접 https://hub.docker.com/

 

Docker Hub

Docker Certified:Trusted & Supported Products Certified Containers provide ISV apps available as containers. Certified Plugins for networking and volumes in containers. Certified Infrastructure delivers an optimized and validated Docker platform for enterp

hub.docker.com

도커 허브에 방문하여 직접 찾는 방법도 있지만 bash창에서도 docker search를 통해 찾을 수 있습니다. 

docker hub에 검색하여 이미지 찾기

아래와 같이 docker search ubuntu의 결과

docker search ubuntu                                                                                                                                                                                             
NAME                                                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   10436               [OK]
dorowu/ubuntu-desktop-lxde-vnc                            Docker image to provide HTML5 VNC interface …   386                                     [OK]
rastasheep/ubuntu-sshd                                    Dockerized SSH service, built on top of offi…   240                                     [OK]
consol/ubuntu-xfce-vnc                                    Ubuntu container with "headless" VNC session…   208                                     [OK]
ubuntu-upstart                                            Upstart is an event-based replacement for th…   104                 [OK]
ansible/ubuntu14.04-ansible                               Ubuntu 14.04 LTS with ansible                   98                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5      ubuntu-16-nginx-php-phpmyadmin-mysql-5          50                                      [OK]
ubuntu-debootstrap                                        debootstrap --variant=minbase --components=m…   42                  [OK]
nuagebec/ubuntu                                           Simple always updated Ubuntu docker images w…   24                                      [OK]
i386/ubuntu                                               Ubuntu is a Debian-based Linux operating sys…   18
1and1internet/ubuntu-16-apache-php-5.6                    ubuntu-16-apache-php-5.6                        14                                      [OK]
1and1internet/ubuntu-16-apache-php-7.0                    ubuntu-16-apache-php-7.0                        13                                      [OK]
eclipse/ubuntu_jdk8                                       Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, …   12                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10   ubuntu-16-nginx-php-phpmyadmin-mariadb-10       11                                      [OK]
1and1internet/ubuntu-16-nginx-php-5.6                     ubuntu-16-nginx-php-5.6                         8                                       [OK]
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4         ubuntu-16-nginx-php-5.6-wordpress-4             7                                       [OK]
1and1internet/ubuntu-16-apache-php-7.1                    ubuntu-16-apache-php-7.1                        6                                       [OK]
darksheer/ubuntu                                          Base Ubuntu Image -- Updated hourly             5                                       [OK]
1and1internet/ubuntu-16-nginx-php-7.0                     ubuntu-16-nginx-php-7.0                         4                                       [OK]
pivotaldata/ubuntu                                        A quick freshening-up of the base Ubuntu doc…   3
pivotaldata/ubuntu16.04-build                             Ubuntu 16.04 image for GPDB compilation         2
1and1internet/ubuntu-16-php-7.1                           ubuntu-16-php-7.1                               1                                       [OK]
smartentry/ubuntu                                         ubuntu with smartentry                          1                                       [OK]
pivotaldata/ubuntu-gpdb-dev                               Ubuntu images for GPDB development              1
1and1internet/ubuntu-16-sshd                              ubuntu-16-sshd                                  1                                       [OK]

docker image 생성

위와 같이 특별하게 이미지를 찾아서 pull한 다음 사용할 수 있지만 개인이 개발하고 있던 환경을 image화 시킬 필요가 있습니다. 그래서 이번에는 custom하여 이미지를 만들어보도록 하겠습니다. 

 

우선 

docker run -i -t --name commit_test ubuntu:14.04
root@2ef1149bc893:/# vi test.cpp

#include <stdio.h>

저장

후에 ctrl + delete를 사용한 다음 아래의 명령어를 실행하면 아래의 사진과 같이 image가 만들어진 것을 확인할 수 있습니다. -a 작성자를 의미합니다. -m commit 메시지를 의미합니다. 

 

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

위 명령어를 사용하여 이미지를 만들어 줄 수 있다. TAG를 안붙여주면 항상 latest로 설정한다. 

docker commit \                                                                                                                                                                                                    ✔  10143  14:26:17
> -a "hoony" -m "making test.cpp file" \
> commit_test \
> commit_test:first

추가로 commit_test 이미지를 사용하여 commit_test2를 만들어보겠습니다. 

docker run -it --name commit_test2 commit_test:first
vi test.cpp

ls test.cpp명령어를 사용하며 확인하면 이전에 만들었던 파일이 그대로 존재하는 것을 알 수 있습니다. 이번에 새로 만든 commit_test2 comtainer에서는 새로운 cpp파일을 만들어보겠습니다. 

docker commit \                                                                                                                                                                                                    ✔  10147  14:39:39
> -a "hoony" -m "making test2.cpp" \
> commit_test2 \
> commit_test:second

commit 명령어를 실행하여 image를 만들어 보았습니다. 

 

이미지 구조 이해

이번에는 이미지가 어떻게 이루어져있는지 확인해보겠습니다. 

docker inspect ubuntu:14.04
docker inspect commit_test:first
docker inspect commit_test:second

위의 명령어를 입력하면 image에 대한 각종 정보들을 확인할 수 있습니다. 여기서 우리는 Layers에 대한 정보를 확인하겠습니다. 

docker images layers

2번째와 3번째 이미지는 commit_test의 first, second의 정보입니다. 셋을 잘비교해보면 첫 ubuntu에 비해 한줄의 16진수 값이 추가된 것을 확인할 수 있습니다. 아래 이미지와 같이 기존의 layer에 새로 커밋할 때마다 layer를 추가해가는 방식으로 이미지를 만들어갑니다.

우리와 같은 경우에는 새로운 layer가 cpp파일이 될 것입니다.  

 

docker image 삭제

docker stop commit_test2 && docker rm commit_test2
docker rmi commit_test:first

 

 commit_test:first

위 명령어로 삭제하려고 하면 당연히 안된다. 왜냐하면 commit_test:first를 사용하고 있는 container가 아직 실행중이기 때문입니다. 그래서 stop 및 rm을 사용하여 삭제해주었습니다. first를 삭제했다고 해당 이미지의 레이어 파일이 삭제되지 않습니다. 왜냐하면 first를 기반으로 하는 하위 이미지인 commit_test:second가 존재하기 때문입니다. 여기서 나온 Untagged는 이미지에 부여된 이름만 삭제하는 덕을 뜻합니다. 

 

그래도 container와 같이 -f명령어를 사용하여 삭제할 수 있지만 실제로 layer의 내용들이 삭제되는 것이 아닌 이름만이 삭제되는 것입니다. 아래와 같은 이미지를 댕글링(dangling)이미지라고 합니다. 

docker images -f dangling=true

-f 제거 이미지

commit_test:second는 사용하고 있는 container가 없기 때문에 바로삭제 할 수 있습니다.

commit_test_second

아까 commit_test:first와 다르게 Deleted라는 정보가 나옵니다. 이는 레이어가 삭제되었음을 의미합니다. 여기서 주의해야 할 점은 바로 부모 이미지가 존재하지 않아야 깔끔하게 삭제할 수 있다는 점입니다. 그래서 second이미지를 삭제할 때 ubuntu는 삭제되지 않아도 되는 것입니다. 

 

이미지 추출

도커 이미지를 별도로 저장하거나 옮기는 등 필요에 따라 이미지를 단일 바이너리 파일로 저장해야 할 때가 있습니다. 이럴 때 docker save명령어를 사용하여 container의 command, image, name, tag 등의 이미지의 모든 Meta_Data를 포함해 하나의 파일로 추출할 수 있습니다. -o 옵션은 추출될 이름을 결정합니다. 

docker save -o ubuntu_14_04.tar ubuntu:14.04

추출된 이미지는 load 명령어로 도커에 다시 로드할 수 있습니다. save 명령어로 추출된 이미지는 모든 메타데이터 정보를 갖고있기 때문에 load 명령어로 로드하면 바로 사용가능한 이미지로 만들 수 있습니다. 

docker load -i ubuntu_14_04.tar

save오 load 뿐만아니라 비슷한 export import라는 명령어가 있습니다. commit으로 이미지를 만들게 되면 변경사항, 컨테이너의 설정 정보 등이 모드 이미지에 저장됩니다. 그러나 export 명령어는 tar로 추출하면 컨테이너 및 이미지에 대한 설정 정보를 저장하지 않습니다. 

docker export -o rootFS.tar mycontainer 
docker import rootFS.tar myimage:0.0

그러나 이미지를 단일 파일로 저장하는 것은 효율적인 방법이 아닙니다. 추출된 이미지는 레이어 구조의 파일이 아닌 단일 파일이기 때문에 여러 버전의 이미지를 추출 하면 이미지의 용량을 서로 차지하게 됩니다. 

ex) ubuntu:14.04 197mb commit_test:first  197mb 총 394mb가 됩니다. 

'devops > docker' 카테고리의 다른 글

docker file 작성  (0) 2020.01.31
docker image 배포  (0) 2020.01.31
docker 컨테이너 자원 할당 제한  (0) 2020.01.29
docker 컨테이너 log 남기기  (0) 2020.01.26
docker 네트워크  (1) 2020.01.22