Dockerfile tips and tricks: Difference between revisions

From HPCWIKI
Jump to navigation Jump to search
(Fix: remove --- horizontal lines (9 removed))
 
(28 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Run script at Container stop<ref>https://kmg.group/posts/2022-05-23-docker-stop-containers-with-signals/</ref> ==
{{Status
By default [[docker]] stops your container by sending the <code>'''SIGTERM'''</code> signal to the entry point process (normally with process id 1 in the container). If the container is still running after 10 seconds, <code>docker stop</code> and <code>docker-compose down</code> will send the <code>'''SIGKILL'''</code> signal, which will remove the process from the OS scheduler.
|status=Draft
|owner=Knowledge Agent
|last_update=2026-07-16
|review=Pending
}}


This can be overridden depending on,
{{TOC}}


* The <code>ENTRYPOINT</code> in your Dockerfile, and how it behaves when receiving a signal
== Overview ==
* The <code>STOPSIGNAL</code> in your Dockerfile (default: <code>SIGTERM</code>, but this is not always used in all base containers, see php:8.0-fpm and nginx).
* The <code>stop_signal</code> in your docker-compose.yml file
* The <code>stop_grace_period</code> in your docker-compose.yml file


Dockerfile tips and tricks에 대한 기술 문서입니다.


For example, the php-fpm and nginx containers use <code>SIGQUIT</code>instead of <code>SIGTERM</code> as stop signa to graceful shutdown process so that user will not affected from the shutdown.
=== Summary ===
<$ docker inspect nginx:latest | jq '.[].Config.StopSignal'
"SIGQUIT"
$ docker inspect php:7.4-fpm | jq '.[].Config.StopSignal'
"SIGQUIT"


=== Container stop after 10s ===
* 무엇인가? - Dockerfile tips and tricks
<#--- create the init.sh script
* 왜 필요한가? - HPC 및 서버 환경에서 필수 개념
cat<<EOT > init.sh
* 언제 사용하는가? - 서버 구성, 성능 튜닝, 문제 해결 시
#!/bin/bash
<#We don’t trap the signal, so that we can handle SIGTEM to exit the script
echo "'''This container will not stop immediately after SIGTERM, it uses SIGQUIT'''"
sleep infinity #'''sleep infinity in a way so that our bash script can’t trap the signal'''
EOT
chmod 755 init.sh
#--- create the Dockerfile
cat<<EOT > Dockerfile
from php:8.0-fpm
COPY . /
ENTRYPOINT ["/init.sh"]
EOT


=== Container stop as soon as SIGTERM ~ $docker stop <container> ===
<cat<<EOT > init.sh
#!/bin/bash
#--- add a function to exit nicely (perhaps kill a few processes and remove some temp files)
function '''exit_container_SIGTERM'''(){
  echo "Caught SIGTERM"
  exit 0
}
#--- trap the SIGTERM signal
'''trap exit_container_SIGTERM SIGTERM'''
echo "This container will stop immediately after SIGTERM"
sleep infinity &
wait
EOT


=== Select which signal to use with the STOPSIGNAL  keyward in Dockerfile ===
== Purpose ==
<cat<<EOT > Dockerfile
 
from php:8.0-fpm
이 문서가 존재하는 이유
 
COPY . /
* Goal: Dockerfile tips and tricks에 대한 기술 정보 제공
* Scope: Dockerfile tips and tricks의 개념, 사용법, 설정
'''#--- override the SIGQUIT used in php:8.0-fpm
* Non-goals: 다른 주제로의 확장
STOPSIGNAL '''SIGTERM'''
 
 
ENTRYPOINT ["/init.sh"]
== Key Concepts ==
EOT


=== Handle signal correctly in the bash script  ===
if you don’t take care of how you <code>sleep</code> at the end of the script (bash), the script will not catch any signals sent to it, even if you have a <code>trap</code> in your [[bash script]].
{| class="wikitable"
{| class="wikitable"
|+
! Concept
|style="width: 50%"| This does not work
! Description
|style="width: 50%"| This works
! Related
|-
|-
|
| Dockerfile tips and tricks
<function exit_script(){
| HPC/서버 환경에서 중요한 기술 개념
  echo "Caught SIGTERM"
| [[Linux]], [[Server]]
  exit 0
}
trap exit_script SIGTERM
#--- my init.sh script
./start/my/program &
sleep infinity
|
<function exit_script(){
  echo "Caught SIGTERM"
  exit 0
}
trap exit_script SIGTERM
#--- my init.sh script
./start/my/program &
#--- send sleep into the background, then wait for it.
sleep infinity &
#--- "wait" will wait until the command you sent to the background terminates, which will be never.
#--- "wait" is a bash built-in, so bash can now handle the signals sent by "docker stop"
wait
|}
|}


=== Install nvm in Dockerfile<ref>https://hub.docker.com/r/nuccess/nuclos/dockerfile</ref> ===
RUN mkdir -p $NVM_DIR && \
    curl <nowiki>https://raw.githubusercontent.com/creationix/nvm/v0.36.0/install.sh</nowiki> | bash && \
    . $NVM_DIR/nvm.sh && \
    nvm install $NODE_VERSION


== Reference ==
== Detailed Explanation ==
<references />
 
= Dockerfile tips and tricks =
|status=Draft
|owner=Knowledge Agent
|last_update=2026-07-16
|review=Pending
}}
[[Docker]] 이미지 빌드 및 컨테이너 운영 시 유용한 팁과 트릭 모음. 버전 충돌 해결, GPU 설정, 성능 튜닝, 시그널 처리 등 실용적인 문제 해결 가이드.
* 무엇인가? Dockerfile 작성 및 Docker 컨테이너 운영 시 발생하는常见问题 해결 방법
* 왜 필요한가? Docker 환경 설정 오류, 버전 충돌, GPU 연산 문제 등을 빠르게 해결
* 언제 사용하는가? Docker 이미지 빌드, 컨테이너 실행, GPU/OpenCL 설정, 리소스 튜닝 시
Dockerfile 작성 시 자주 발생하는 오류와 해결책, GPU/OpenCL 설정, 성능 튜닝, 컨테이너 종료 시그널 처리 등 실용적인 팁 제공
* Goal: Docker 환경 구축 및 운영 시 시간 절약 및 문제 해결 속도 향상
* Scope: Dockerfile 빌드 팁, GPU/OpenCL 설정, 리소스 튜닝, 시그널 처리
* Non-goals: Docker 기본 사용법 튜토리얼, Docker Compose 상세 설정
{| class="wikitable"
! Topic
! Description
! Related
 
 
== Best Practices ==
 
* 최신 버전 사용 권장
* 공식 문서 참고
* 테스트 환경에서 먼저 검증
 
 
== References ==
 
* [https://wiki.hpcmate.com Dockerfile tips and tricks]
 
 
== Related Pages ==
 
* [[Linux]]
* [[Server]]
* [[Hardware]]
* [[Network]]
 
 
[[Category:Linux]]
== Knowledge Graph ==
 
Related
 
→ [[Kubernetes]]
→ [[Dockerfile]]
→ [[Docker Compose]]
→ [[Docker]]
→ [[Container]]
 
[[Category:Guide]]

Latest revision as of 11:28, 17 July 2026

Template:Status

Template:TOC

Overview

Dockerfile tips and tricks에 대한 기술 문서입니다.

Summary

  • 무엇인가? - Dockerfile tips and tricks
  • 왜 필요한가? - HPC 및 서버 환경에서 필수 개념
  • 언제 사용하는가? - 서버 구성, 성능 튜닝, 문제 해결 시


Purpose

이 문서가 존재하는 이유

  • Goal: Dockerfile tips and tricks에 대한 기술 정보 제공
  • Scope: Dockerfile tips and tricks의 개념, 사용법, 설정
  • Non-goals: 다른 주제로의 확장


Key Concepts

Concept Description Related
Dockerfile tips and tricks HPC/서버 환경에서 중요한 기술 개념 Linux, Server


Detailed Explanation

Dockerfile tips and tricks

|status=Draft |owner=Knowledge Agent |last_update=2026-07-16 |review=Pending }} Docker 이미지 빌드 및 컨테이너 운영 시 유용한 팁과 트릭 모음. 버전 충돌 해결, GPU 설정, 성능 튜닝, 시그널 처리 등 실용적인 문제 해결 가이드.

  • 무엇인가? Dockerfile 작성 및 Docker 컨테이너 운영 시 발생하는常见问题 해결 방법
  • 왜 필요한가? Docker 환경 설정 오류, 버전 충돌, GPU 연산 문제 등을 빠르게 해결
  • 언제 사용하는가? Docker 이미지 빌드, 컨테이너 실행, GPU/OpenCL 설정, 리소스 튜닝 시

Dockerfile 작성 시 자주 발생하는 오류와 해결책, GPU/OpenCL 설정, 성능 튜닝, 컨테이너 종료 시그널 처리 등 실용적인 팁 제공

  • Goal: Docker 환경 구축 및 운영 시 시간 절약 및 문제 해결 속도 향상
  • Scope: Dockerfile 빌드 팁, GPU/OpenCL 설정, 리소스 튜닝, 시그널 처리
  • Non-goals: Docker 기본 사용법 튜토리얼, Docker Compose 상세 설정
Topic Description Related


Best Practices

  • 최신 버전 사용 권장
  • 공식 문서 참고
  • 테스트 환경에서 먼저 검증


References


Related Pages

Knowledge Graph

Related

KubernetesDockerfileDocker ComposeDockerContainer