///
Search

07.Docker Image를 만들기

지금까지는 누가 만든 이미지를 가져다가 썼는데 이번 시간에는 직접 이미지를 만드는 걸 실습해본다.

이미지란?

도커는 레이어드 파일 시스템 기반이다.
이미지를 구성하는 방식은? → AUFS, BTRFS, Overlayfs, ...
이미지는 프로세스가 실행되는 파일들의 집합(환경)
프로세스는 환경(파일)을 변경할 수 있음
이 환경을 저장해서 새로운 이미지를 만든다

이미지의 종류

읽기 전용
쓰기 가능

읽기 전용

Base Image는 변경이 안 된다. (수정)X
예시를 들자면 우분투를 베이스 이미지로 하면 우분투 이미지를 수정하거나 바꿀수는 없다.

상태 변화

Base Image : 우분투라고 가정해보자
초기 우분투 상태에서는 github에서 배포하는 git이 안 깔려있다.
그래서 git을 설치를 하고 난 그 컨테이너 자체를 commit 명령어를 통하여 image를 만들수 있는 것이다.
A 이미지가 있다고 가정하면 A+B 해서 한개의 이미지로 만들수 있으나, A이미지 자체를 수정하거나 변경을 할수가 없다는 의미이다.

실습

맨 처음 도커를 실행 시켜야지 도커 명령어들이 작동을 한다.
본 필자는 맥북에서 진행하고 있으며, 맥북에서는 도커 어플리케이션을 실행시키면 바로 도커가 준비가 된다.
도커가 실행중인지 확인 할려면 아래의 명령어로 테스트 해볼수 있다.
docker ps
Go
복사
맨 처음 Git을 설치하기 위함으로 본 명령어를 입력한다.
명령어
docker run -it --name git ubuntu:latest bash
Go
복사
실행화면
(base) glory@Gloryui-MacBookPro (GITHUB) % docker run -it --name git ubuntu:latest bash Unable to find image 'ubuntu:latest' locally latest: Pulling from library/ubuntu Digest: sha256:7123123123123123123123131231231232131232131321312128ad26615c3715 Status: Downloaded newer image for ubuntu:latest root@321232323b4142e:/#
Go
복사
이렇게 하면 우분투 latest를 기반으로한 이름이 git이라는 컨테이너가 생성된다. 그리고 bash를 통해 접속을 한 상태이다.
그러면 여기에 git이 있는지 확인해보자
명령어
git
Go
복사
실행화면
root@31231231242e:/# git bash: git: command not found root@341231231232e:/#
Go
복사
이렇게 git이 없는 상태로 나오게 된다.
여기서 git을 설치하기위해서 아래의 명령어를 순차적으로 입력한다.
apt-get update apt-get install -y git git --version
Go
복사
위의 명령어를 순차적으로 3개를 입력하면 최종적으로 설치된 git version의 버전이 나오게 된다.
자 그러면 여기 컨테이너에서 나오도록 한다.
명령어
exit
Go
복사
실행화면
root@3223232322e:/# exit exit (base) glory@Gloryui-MacBookPro (GITHUB) %
Go
복사
아래의 명령어는 ubuntu 라는 이름의 컨테이너를 조회하는 명령어이다.
명령어
docker images | grep ubuntu
Go
복사
실행화면
$ docker images | grep ubuntu ubuntu latest cf0f3ca922e0 5 days ago 64.2MB $
Go
복사
그리고 ubuntu에 docker에 commit 명령어를 입력해서 이미지를 압축하는 작업을 수행한다.
명령어
docker commit git ubuntu:git
Go
복사
실행화면
(base) glory@Gloryui-MacBookPro (GITHUB) % docker commit git ubuntu:git sha256:e2903a5c1123232321...12312fafd0b5ae55808619cbee2fc8 (base) glory@Gloryui-MacBookPro (GITHUB) %
Go
복사
그리고 이미지화가 잘 되었는지 확인해보기 위해 다음과 같은 명령어를 입력한다.
명령어
docker images
Go
복사
실행화면
(base) glory@Gloryui-MacBookPro (GITHUB) % docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu git e2903a5c180c About a minute ago 202MB wordpress latest d94129b6e1f4 2 weeks ago 550MB ubuntu 20.04 f63181f19b2f 2 weeks ago 72.9MB
Go
복사
위에 보면 도커 이미지에서 git이라고 TAG가 적힌 ubuntu 레포지토리를 확인할 수 있다. 저게 바로 우리가 방금 만든 이미지이다.
그러면 이제 git이미지의 레포지토리를 가지고 다시 설치를 해본다.(새로운 이미지의 이름은 git2이고 우리가 만든 git을 활용하는 작업)
명령어
docker run -it --name git2 ubuntu:git bash
Go
복사
실행화면
(base) glory@Gloryui-MacBookPro (GITHUB) % docker run -it --name git2 ubuntu:git bash root@2b023232381:/#
Go
복사
그리고 확인차 git 명령어를 입력하도록 한다.
실행화면
root@2b02323232381:/# git usage: git [--version] [--help] [-C <path>] [-c <name>=<value>] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path] [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare] [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] <command> [<args>] These are common Git commands used in various situations: start a working area (see also: git help tutorial) clone Clone a repository into a new directory init Create an empty Git repository or reinitialize an existing one work on the current change (see also: git help everyday) add Add file contents to the index mv Move or rename a file, a directory, or a symlink restore Restore working tree files rm Remove files from the working tree and from the index sparse-checkout Initialize and modify the sparse-checkout examine the history and state (see also: git help revisions) bisect Use binary search to find the commit that introduced a bug diff Show changes between commits, commit and working tree, etc grep Print lines matching a pattern log Show commit logs show Show various types of objects status Show the working tree status grow, mark and tweak your common history branch List, create, or delete branches commit Record changes to the repository merge Join two or more development histories together rebase Reapply commits on top of another base tip reset Reset current HEAD to the specified state switch Switch branches tag Create, list, delete or verify a tag object signed with GPG collaborate (see also: git help workflows) fetch Download objects and refs from another repository pull Fetch from and integrate with another repository or a local branch push Update remote refs along with associated objects 'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept. See 'git help git' for an overview of the system. root@2323232321:/#
Go
복사
이렇게 새로 만들었던 git이미지는 그안에 git이 들어가있는 상태이니 저렇게 바로 git관련 명령어 안내문이 나오는 것을 확인할 수 있다.

도커 빌드 명령어 규칙

텍스트
docker build -t subicura/ubuntu:git01 .
Go
복사