View on GitHub

programming-study

허원철의 개발 블로그

docker 명렁어 정리

헷갈리는 명령어 (ENV, ARG, CMD, ENTRYPOINT, ADD, COPY)

ENV vs ARG

ENV

ARG

TEST 1

Scope 확인

FROM debian:jessie-slim

ENV NAME_ENV=wonchul 
ARG NAME_ARG=wonchul

RUN echo "envirment = ${NAME_ENV:-WonChul}" \
    && echo "argument = ${NAME_ARG:-WonChul}"

CMD echo "envirment = ${NAME_ENV:-WonChul}" \
    && echo "argument = ${NAME_ARG:-WonChul}"
  1. override 안했을 때(build, run)
$ docker build -t basic . # build
# 결과
...
envirment = wonchul
argument = wonchul
...
$ docker run basic # run
# 결과
envirment = wonchul
argument = WonChul
  1. override 했을 때(build, run)
$ docker build -t basic --build-arg NAME_ARG=WONCHUL . # build
# 결과
...
envirment = wonchul
argument = WONCHUL
...
$ docker run -e NAME_ENV=WONCHUL basic # run
# 결과
envirment = WONCHUL
argument = WonChul

TEST 2

ENVARG 덮어쓰기

FROM debian:jessie-slim

ARG NAME
ENV NAME=${NAME:-WonChul}

RUN echo "NAME = ${NAME}"

CMD echo "NAME = ${NAME}"
  1. override 안했을 때(build, run)
$ docker build -t basic . # build
# 결과
...
NAME = WonChul
...
$ docker run basic # run
# 결과
NAME = WonChul
  1. override 했을 때(run)
$ docker run -e NAME=WONCHUL basic # run
# 결과
NAME = WONCHUL
  1. override 했을 때(build, run)
$ docker build -t basic --build-arg NAME=WONCHUL . # build
# 결과
...
NAME = WONCHUL
...
$ docker run basic # run
# 결과
NAME = WONCHUL
$ docker run -e NAME=WONCHUL_ENV basic # run
# 결과
NAME = WONCHUL_ENV

CMD

ENTRYPOINT

TEST 1

FROM debian:jessie-slim

CMD ["echo", "hello"]
CMD ["echo", "hello2"]
$ docker build -t basic . # build
$ docker run basic # run
# 결과
hello2
$ docker run basic echo hello3 # run
# 결과
hello3

TEST 2

FROM debian:jessie-slim

ENTRYPOINT ["echo"]
CMD ["hello"]
CMD ["hello2"]
$ docker build -t basic . # build
$ docker run basic # run
# 결과
hello2
$ docker run --entrypoint="cat" basic /etc/hostname # run
# 결과
34n343hbfd

ADD

COPY

공통적으로 .dockerignore에 명시된 영역은 제외