본문 바로가기
Framework and Tool/Docker

Build Context

by ocwokocw 2023. 8. 12.

- 출처: https://docs.docker.com/build/building/context/

 

Build context

 

docs.docker.com

 

Build context

"docker build"는 Dockerfile과 context로 부터 docker image를 build 하는 명령어다. Build context는 build 명령어에서 제일 마지막에 인자에 해당한다.

docker build [OPTIONS] PATH | URL | -
                       ^^^^^^^^^^^^^^

 

명령어를 보면 짐작할 수 있듯이 build를 위한 context로 아래와 같은 입력들을 전달할 수 있다.

  • local directory의 상대경로나 절대경로
  • 원격 Git repository, tarball, plain-text 파일의 주소
  • 파이프로 연결된 plain-text 파일이나 표준 입력을 사용하는 tarball

Filesystem contexts

build context로 local directory, 원격 Git repository, tar file 을 지정했다면 builder가 build 하는 동안 해당 경로에 접근할 수 있게 된다. build 지시어는 context 내의 모든 파일이나 directory를 참조할 수 있다. 예를 들어 "COPY" 명령어를 사용하면 builder는 build context 에서 파일이나 directory를 build container로 복사한다. Filesystem build context는 재귀적으로 처리된다.

  • local directory나 tarball을 지정하면 모든 하위 directory 들도 포함된다.
  • 원격 Git repository를 지정하면 해당 repository와 모든 하위 module 들도 포함된다.

Text file contexts

build context가 plain-text 파일이면 builder는 해당 파일을 Dockerfile 로 간주한다. 따라서 builder는 filesystem context를 받지 않는다.


Local directories and tarballs

여태까지 build 명령어를 사용하면서 "docker build ." 과 같은 형태를 많이 사용했는데, 이를 build context 관점으로 해석하면 현재 directory(.)를 build context 로 사용한다는 뜻이 된다.

현재 directory를 build context로 사용하면 현재 directory의 파일과 directory들을 builder가 사용할 수 있게 된다. 예를 들어 현재 directory가 아래와 같이 구성되어 있다고 가정해보자.

.
├── index.ts
├── src/
├── Dockerfile
├── package.json
└── package-lock.json

Dockerfile 지시어는 build 시 이를 참조할 수 있으며 directory를 context로 사용할 수도 있다.

# syntax=docker/dockerfile:1
FROM node:latest
WORKDIR /src
COPY package.json package-lock.json .
RUN npm ci
COPY index.ts src .

.dockerignore

.gitignore 처럼 특정 파일이나 directory 들을 무시할수도 있다. 만약 여러 개의 Dockerfile을 사용하면 각 Dockerfile 마다 별도의 ignore 파일을 사용할 수 있다. 이때는 ignore 파일을 Dockerfile과 같은 directory에 두고 Dockerfile의 이름을 ignore 파일의 접두사로 사용하는것과 같은 특별한 naming 규칙을 적용할 수 있다.

.
├── index.ts
├── src/
├── docker
│   ├── build.Dockerfile
│   ├── build.Dockerfile.dockerignore
│   ├── lint.Dockerfile
│   ├── lint.Dockerfile.dockerignore
│   ├── test.Dockerfile
│   └── test.Dockerfile.dockerignore
├── package.json
└── package-lock.json

Git repository

Docker build 명령어의 인자로 Git repository 를 가리키는 URL을 넘기면 builder는 해당 repository를 build context로 간주한다.

Builder는 repository에 대해 얕은 복사(shallow copy)를 수행하며 전체 내역이 아닌 HEAD commit만 다운로드 한다. Builder는 해당 repository에 submodule이 있다면 재귀적으로 clone 한다.

 

Builder가 특정 branch나 repository의 하위 directory를 복사할 수 있도록 Git repository 주소에 URL fragment를 추가할 수 있다. 형식은 "#ref:dir" 이며 각 요소의 의미는 아래와 같다.

  • ref: branch 명, tag, remoter reference
  • dir: repository내의 하위 directory

아래 처럼 명령어를 사용하면 "container" branch의 "docker" 하위 directory를 build context로 간주한다는 의미가 된다.

docker build https://github.com/user/myrepo.git#container:docker

개인적으로 filesystem 외의 build context를 사용해본적이 없다. 나머지 사항은 사용할일이 없을것 같아 자세히 알아보진 않았다. 글에서 기술한것 외의 Tarball, PIPE등에 관심이 있다면 출처에서 확인하도록 하자.

'Framework and Tool > Docker' 카테고리의 다른 글

Swarm mode tutorial  (0) 2023.10.02
Swarm mode overview and concepts  (0) 2023.09.17
Docker - build arguments  (0) 2023.06.28
Docker - Mounts  (0) 2023.06.21
Docker - Multi stage  (0) 2023.06.20

댓글