Docker 플러그인
Docker 플러그인은 Docker-in-Docker를 사용해 이미지를 빌드하고 Docker 레지스트리에 퍼블리시할 때 사용할 수 있어요. 다음 파이프라인 설정은 Docker 플러그인을 사용해 Docker 이미지를 빌드하고 퍼블리시해요.
아래 예제 설정은 git 저장소 루트에 있는 Dockerfile로 이미지를 빌드하고 퍼블리시해요. docker 컨텍스트도 기본적으로 git 저장소의 루트가 돼요.
출처: Drone CI Docs
본문
기본 예제
kind: pipeline
name: default
steps:
- name: docker
image: plugins/docker
settings:
username: kevinbacon
password: pa55word
repo: foo/bar
tags:
- latest
여러 태그를 사용하는 예제 설정이에요:
steps:
- name: docker
image: plugins/docker
settings:
username: kevinbacon
password: pa55word
repo: foo/bar
tags:
- latest
- '1.0.1'
- '1.0'
아래 예제는 태그를 쉼표로 구분한 목록을 담은 .tags 파일을 사용해 태그를 동적으로 구성하는 방법이에요. 이 파일이 존재하면 플러그인이 자동으로 읽어 들여요.
steps:
- name: build
image: golang
commands:
- go build
- go test
- echo -n "5.2.6,5.2.4" > .tags
- name: docker
image: plugins/docker
settings:
username: kevinbacon
password: pa55word
repo: foo/bar
빌드 인자(build arguments)를 사용하는 예제 설정이에요:
steps:
- name: docker
image: plugins/docker
settings:
username: kevinbacon
password: pa55word
repo: foo/bar
build_args:
- HTTP_PROXY=http://yourproxy.com
시크릿을 빌드 인자로 전달하는 예제 설정이에요:
steps:
- name: build-image
image: plugins/docker
environment:
TOKEN:
from_secret: one_of_the_tokens
settings:
username: kevinbacon
password: pa55word
repo: foo/bar
build_args_from_env:
- TOKEN
다른 위치의 Dockerfile을 사용하는 예제 설정이에요:
steps:
- name: docker
image: plugins/docker
settings:
username: kevinbacon
password: pa55word
repo: foo/bar
dockerfile: path/to/Dockerfile
커스텀 레지스트리를 사용하는 예제 설정이에요:
steps:
- name: docker
image: plugins/docker
settings:
username: kevinbacon
password: pa55word
repo: index.company.com/foo/bar
registry: index.company.com
Secrets
민감한 설정 값은 시크릿(secrets)에서 가져오는 것이 모범 사례로 여겨져요. 다음 예제는 사용자 이름과 비밀번호를 시크릿에서 가져오는 방법을 보여줘요:
kind: pipeline
name: default
steps:
- name: docker
image: plugins/docker
settings:
username:
from_secret: docker_username
password:
from_secret: docker_password
repo: foo/bar
tags:
- latest
Autotag
Docker 플러그인은 이미지에 자동으로 태그를 붙이도록 설정할 수 있어요. 이 기능이 활성화되어 있고 이벤트 타입이 tag일 때, 플러그인은 표준 major, minor, release 관례를 따라 이미지에 자동으로 태그를 붙여요. 예를 들어:
v1.0.0은 docker 태그 1, 1.0, 1.0.0을 만들어요
v1.0.0-rc.1은 docker 태그 1.0.0-rc.1을 만들어요
이벤트 타입이 push이고 대상 브랜치가 기본 브랜치(예: master)일 때 플러그인은 이미지에 latest 태그를 자동으로 붙여요. 그 밖의 다른 이벤트 타입과 브랜치는 무시돼요.
예제 설정이에요:
steps:
- name: docker
image: plugins/docker
settings:
repo: foo/bar
auto_tag: true
username: kevinbacon
password: pa55word
태그 접미사(suffix)를 사용하는 예제 설정이에요:
steps:
- name: docker
image: plugins/docker
settings:
repo: foo/bar
auto_tag: true
auto_tag_suffix: linux-amd64
username: kevinbacon
password: pa55word
auto-tagging은 의도적으로 단순하고 독단적인(opinionated) 설계라는 점을 알아두세요. 현재는 이 로직을 더 커스터마이즈하기 위한 풀 리퀘스트를 받고 있지 않아요.
파라미터 (Parameters)
registry
이 레지스트리로 인증해요
username
이 사용자 이름으로 인증해요
password
이 비밀번호로 인증해요
repo
이미지의 저장소 이름이에요
tags
이미지의 저장소 태그예요
dockerfile
사용할 dockerfile이며, 기본값은 Dockerfile이에요
dry_run
빌드 마지막에 docker 이미지를 push할지 여부를 나타내는 불리언 값이에요
purge
마지막에 docker 이미지 정리(cleanup)를 할지 여부를 나타내는 불리언 값이에요
context
사용할 컨텍스트 경로이며, 기본값은 git 저장소의 루트예요
target
사용할 빌드 타깃이며, dockerfile에 정의되어 있어야 해요
force_tag=false
기존에 일치하는 이미지 태그를 교체해요
insecure=false
이 레지스트리와의 비보안 통신을 활성화해요
mirror
중앙 Hub에서 이미지를 직접 pull하지 않고 미러 레지스트리를 사용해요
bip=false
브리지 IP를 전달하는 데 사용해요
custom_dns
컨테이너에 커스텀 DNS 서버를 설정해요
custom_dns_search
docker 데몬의 DNS 검색 도메인이에요
storage_driver
aufs, overlay, vfs 드라이버를 지원해요
storage_path
docker 데몬의 스토리지 경로예요
build_args
docker build에 커스텀 인자를 전달해요
build_args_from_env
환경 변수를 커스텀 인자로 docker build에 전달해요
auto_tag=false
git 브랜치와 git 태그를 기반으로 태그 이름을 자동 생성해요
auto_tag_suffix
이 접미사를 붙여 태그 이름을 생성해요
debug, launch_debug
docker 데몬을 상세(debug) 모드로 실행해요
mtu
docker 데몬의 커스텀 mtu 설정이에요
ipv6
docker 데몬의 IPv6 네트워킹이에요
experimental
docker 데몬의 실험적(Experimental) 모드예요
daemon_off
docker 데몬을 시작하지 않아요
cache_from
캐시 소스로 고려할 이미지들이에요
squash
빌드 시점에 레이어를 스쿼시(squash)해요
pull_image
빌드 시점에 베이스 이미지를 강제로 pull해요
compress
빌드 컨텍스트를 gzip으로 압축해요
custom_labels
추가 k=v 라벨들이에요
label_schema
label-schema 라벨들이에요
email
docker 이메일이에요
no_cache
캐시된 중간 컨테이너를 사용하지 않아요
add_host
추가 host:IP 매핑이에요
자주 발생하는 문제 (Common Problems)
사용자 이름 또는 비밀번호 누락
레지스트리 사용자 이름이나 비밀번호가 누락되면 파이프라인 로그에 아래 항목이 표시돼요. 이 문제의 가장 흔한 원인은 시크릿 설정이 잘못된 경우예요.
Registry credentials or Docker config not provided. Guest mode enabled.
잘못된 사용자 이름 또는 비밀번호
레지스트리 사용자 이름과 비밀번호가 유효하지 않으면 파이프라인 로그에 아래 항목이 표시돼요. 이 문제의 가장 흔한 원인은 시크릿을 만들 때 복사/붙여넣기 문제, 즉 시크릿 문자열에 개행이나 특수 문자가 실수로 함께 복사되는 경우예요.
level=fatal msg="Error authenticating: exit status 1"
비보안(Insecure) 레지스트리
레지스트리가 자체 호스팅되고 있고, 일반 http 또는 자체 서명 인증서를 사용한다면 docker 클라이언트는 레지스트리와 보안 연결을 맺을 수 없어요. 이로 인해 docker login이 다음 오류와 함께 실패해요:
level=fatal msg="Error authenticating: exit status 1"
아래 플러그인 설정으로 해결할 수 있어요:
steps:
- name: docker
image: plugins/docker
settings:
repo: foo/bar
username: kevinbacon
password: pa55word
insecure: true
볼륨 사용
플러그인에 볼륨을 마운트하려고 하면 파이프라인 로그에 아래 항목이 표시돼요. docker 플러그인은 보안상의 이유로 볼륨 마운트를 제한해요.
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
exit status 1
마운트된 볼륨을 제거하거나, 플러그인 단계를 privileged 모드로 실행하도록 설정하면 해결할 수 있어요:
steps:
- name: docker
image: plugins/docker
privileged: true
settings:
repo: foo/bar
username: kevinbacon
password: pa55word
volumes:
- name: temp
path: /tmp
Docker In Docker 문제
플러그인 컨테이너 안에서 docker 데몬을 시작할 수 없으면 파이프라인 로그에 아래 항목이 표시돼요. 이 문제의 가장 흔한 원인은 중첩 컨테이너화(nested containerization)를 막는 보안 소프트웨어(selinux, apparmor 등)예요.
level=fatal msg="Error authenticating: exit status 1"
아래 플러그인 설정으로 추가 분석(triage)을 할 수 있어요:
steps:
- name: docker
image: plugins/docker
settings:
repo: foo/bar
username: kevinbacon
password: pa55word
debug: true
docker가 실패하면 파이프라인 로그에 아래 항목이 표시돼요:
failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to create NAT chain DOCKER: iptables failed: iptables -t nat -N DOCKER: iptables v1.8.3 (legacy): can’t initialize iptables table `nat’: Permission denied (you must be root)
docker가 성공하면 파이프라인 로그에 아래 항목이 표시돼요:
time=“2021-01-21T14:15:59.634657433Z” level=info msg=“Daemon has completed initialization”
time=“2021-01-21T14:15:59.661299094Z” level=info msg=“API listen on /var/run/docker.sock”