Image Layers
- image: layer들의 구성
- layer: filesystem의 변화(additions, deletions, modifications)의 집합
- 특징: atomic 한 단위, 각 layer들은 생성되면 불변의 상태가 됨
layering 예시
- basic commands와 package manager 추가
- python runtime과 pip 설치
- requirements.txt에를 읽고 application에 필요한 의존성 확인
- application에 필요한 python package들 설치
- 실제 source code 추가
- 위와 같이 layering을 했을 때, 다른 python application의 image를 정의하기 위해서, 1, 2 layer는 재사용할 수 있어진다.
layer 작동 방식
-
content-addressable storage와union filesystems을 사용해서 layering을 할 수 있다. -
작동원리
- 각 레이어를 다운로드 -> host filesystem의 각각의 directory에 압축을 푼다.
- image에서 container를 실행 시킬 때,
union filesystems이 생성된다. layer들을 쌓아서 하나의 새로운 통합된 View를 만든다. - container를 실행 하면,
chroot를 사용해서 루트 디렉토리를 이 통합된 디렉토리 위치도 설정한다.
-
union filesystem이 생성되고, container가 실행될 때 Copy-on-Write 방식으로 복제 하여 image를 건들이지 않고 container를 실행시킨다.
Example
docker container commit을 사용해서 layer를 생성할 수 있다. (이 방식 보다는 Dockerfile으로 하는 것이 권장된다.)
# ubuntu image를 pulling 해서, 실행한다.
# 실행 결과로 ubuntu shell prompt가 뜬다.
docker run --name=base-container -ti ubuntu
#============ container 안의 shell
# package manager를 사용해서 nodejs를 설치한다.
apt update && apt install -y nodejs
# 이러한 변화는 Copy-on-Write 방식으로 container의 고유 directory에서 일어난다.
#============ host의 shell
# 아래 명령어를 통해서 base-container의 변화를 반영한 node-base라는 image를 생성한다.
docker container commit -m "Add node" base-container node-base
# 아래 명령어를 통해서 특정 image의 변경 사항을 볼 수 있다.
docker image history node-base