Docker 복습

별빛사막·2025년 2월 25일
0

docker

목록 보기
1/7
post-thumbnail

주 프로세스: Entrypiont + Cmd

  • docker run hello-world
    : null + "/hello"
  • docker run nginx
    : /docker-entrypoint.sh + nginx g daemon off;

/docker-entrypoint.sh 설정파일

대부분의 Docker 이미지에서는 /docker-entrypoint.sh 스크립트를 ENTRYPOINT로 사용하기 때문에, 해당 파일의 내용을 살펴볼 필요가 있다.

✅ /docker-entrypoint.sh 설정파일

#!/bin/sh
# vim:sw=4:ts=4:et

set -e

entrypoint_log() {
    if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then
        echo "$@"
    fi
}

if [ "$1" = "nginx" ] || [ "$1" = "nginx-debug" ]; then
    if /usr/bin/find "/docker-entrypoint.d/" -mindepth 1 -maxdepth 1 -type f -print -quit 2>/dev/null | read v; then
        entrypoint_log "$0: /docker-entrypoint.d/ is not empty, will attempt to perform configuration"

        entrypoint_log "$0: Looking for shell scripts in /docker-entrypoint.d/"
        find "/docker-entrypoint.d/" -follow -type f -print | sort -V | while read -r f; do
            case "$f" in
                *.envsh)
                    if [ -x "$f" ]; then
                        entrypoint_log "$0: Sourcing $f";
                        . "$f"
                    else
                        # warn on shell scripts without exec bit
                        entrypoint_log "$0: Ignoring $f, not executable";
                    fi
                    ;;
                *.sh)
                    if [ -x "$f" ]; then
                        entrypoint_log "$0: Launching $f";
                        "$f"
                    else
                        # warn on shell scripts without exec bit
                        entrypoint_log "$0: Ignoring $f, not executable";
                    fi
                    ;;
                *) entrypoint_log "$0: Ignoring $f";;
            esac
        done

        entrypoint_log "$0: Configuration complete; ready for start up"
    else
        entrypoint_log "$0: No files found in /docker-entrypoint.d/, skipping configuration"
    fi
fi

exec "$@"

✅ 중요한 코드만 살펴보자!

  1. #!/bin/sh → bash, sh, zsh 어떤 쉘을 사용할지 명시한다.
  2. if [ "$1" = "nginx" ] || [ "$1" = "nginx-debug" ]; then
    → 첫번째 인자($1)가 nginx이면, 전처리해준다
    → 여기서 전처리는 /docker-entrypoint.d 에 있는 파일 순서대로 실행하는 것!
  3. exec "$@" → 입력받은 명령어를 그대로 실행

역할: 입력받은 명령어를 실행해준다. 다만, 첫번째 인자가 nginx이면 전처리를 해준다.


명령어 1 : docker run nginx

docker run nginx
=docker run nginx "nginx" "-g" "daemon off;"
=docker run --entrypoint ./docker-entrypoint.sh nginx "nginx" "-g" "daemon off;"

명령어 2 : docker run nginx ls -al

docker run nginx ls -al -- ①
≒ docker run --entrypoint ls nginx -al -- ②
= docker run --entrypoint ./docker-entrypoint.sh nginx ls -al -- ③

둘다 ls -al이 실행되지만, 내부적으로 동작하는 방식에는 명확한 차이가 있다.

① /docker-entrypoint.sh 스크립트가 실행 → CMD ls -al로 덮어쓰기
→ exec "$@"에 의해 최종 명령어 exec ls -al 실행

② 기본적으로 설정된 /docker-entrypoint.sh가 무시되고 ls로 덮어쓰기
→ al은 CMD에 전달된 인자 → ls -al 실행

③ 1번과 동일하다

실행 명령어ENTRYPOINT 동작CMD 동작최종 실행 명령어
docker run nginx ls -al기본 /docker-entrypoint.sh 실행덮어씌워짐 (ls -al)/docker-entrypoint.shexec ls -al
docker run --entrypoint ls nginx -al기본 ENTRYPOINT 무시, ls로 덮어쓰기인자로 전달됨 (-al)ls -al
docker run --entrypoint ./docker-entrypoint.sh nginx ls -al명시적으로 동일한 ENTRYPOINT 실행인자로 전달됨 (ls -al)/docker-entrypoint.shexec ls -al
profile
조금씩 매일 성장하자

0개의 댓글