Dockerfile tips and tricks: Difference between revisions

From HPCWIKI
Jump to navigation Jump to search
(Fix: remove --- horizontal lines (9 removed))
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Root password inside a Docker container ==
{{Status
Set container root password, when docker image is built by yourself  <syntaxhighlight lang="bash">
|status=Draft
RUN echo 'root:Docker!' | chpasswd
|owner=Knowledge Agent
or
|last_update=2026-07-16
RUN echo 'Docker!' | passwd --stdin root
|review=Pending
</syntaxhighlight>
}}
To create/change a root password in a running container,


<code>docker exec -itu 0 {container} passwd</code> 
{{TOC}}


use -u 0 to login to overriding the USER setting as root, when docker image from 3rd party,
== Overview ==


<code>docker container exec -u 0 -it mycontainer bash</code>
Dockerfile tips and tricks에 대한 기술 문서입니다.


== Failed to create NAT chain DOCKER as ... ==
=== Summary ===
Reason - Docker package looks broken


Solution - $sudo apt update && sudo apt upgrade && sudo systemctl restart docker.service<ref>https://stackoverflow.com/questions/75713844/how-to-resolve-failed-to-create-nat-chain-docker-as-reboot-not-working</ref> 
* 무엇인가? - Dockerfile tips and tricks
* 왜 필요한가? - HPC 및 서버 환경에서 필수 개념
* 언제 사용하는가? - 서버 구성, 성능 튜닝, 문제 해결 시


== Run script at Container stop<ref>https://kmg.group/posts/2022-05-23-docker-stop-containers-with-signals/</ref> ==
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.


This can be overridden depending on,
== Purpose ==


* The <code>ENTRYPOINT</code> in your Dockerfile, and how it behaves when receiving a signal
이 문서가 존재하는 이유
* 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


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


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.
$ docker inspect nginx:latest | jq '.[].Config.StopSignal'
"SIGQUIT"
$ docker inspect php:7.4-fpm | jq '.[].Config.StopSignal'
"SIGQUIT"


=== Container stop after 10s ===
== Key Concepts ==
#--- create the init.sh script
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 ===
cat<<EOT > Dockerfile
from php:8.0-fpm
COPY . /
'''#--- override the SIGQUIT used in php:8.0-fpm
STOPSIGNAL '''SIGTERM'''
ENTRYPOINT ["/init.sh"]
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|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


== Docker cleanup ==
== Detailed Explanation ==
When working with Docker, you can end up piling up unused images, containers, and datasets that clutter the output and take up disk space.  beyond docker images, disk space cab be took up with unused containers, volumes, networks. these objects are generally not removed unless you explicitly ask Docker to do so. otherwise unused objects can cause Docker to use extra disk space.


= 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


Although each type of object, Docker provides a <code>prune</code> command, Docker has a single command that cleans up all '''<u>dangling resources</u>''', such as images, containers, volumes, and networks, not tagged or connected to a container.
Following commands prunes images, containers and networks only. Volumes are not pruned by default until you speficy the --volume flags in this
<code>#docker  system prune</code>
There are also may possible filter<ref>https://docs.docker.com/engine/reference/commandline/system_prune/#filter</ref> options that you can check on Docker site<ref>https://docs.docker.com/config/pruning/</ref><ref>https://docs.docker.com/engine/reference/commandline/system_prune/</ref>
== Dangling image vs unused image ==
'''Dangling images''' are images which do not have a tag, and do not have a '''child image which displays "<code><none></code>" on its name when you run <code>docker images</code>comand.  The main reason to keep them around is for <u>build caching purposes</u> in case you need to build multiple different top images from some common Docker image layers. because they can be used a independent layers that have no relationship to any tagged images.'''
However, An '''unused image''' is an image that has tags but currently not being used as a container.
Ofcourse it is safes to delete them when you build final Docker image and ready to use.
* List Dangling images
<code>docker images -f dangling=true</code>


* Remove Dangling Images
== Best Practices ==


<code>docker rmi $(docker images -f dangling=true -q)</code>
* 최신 버전 사용 권장
OR
* 공식 문서 참고
<code>docker images --quiet --filter=dangling=true | xargs --no-run-if-empty docker rmi</code>
* 테스트 환경에서 먼저 검증




== References ==


For easy life, we can add alias command in .bashrc file like
* [https://wiki.hpcmate.com Dockerfile tips and tricks]


<code>alias docker_clean_images='docker rmi $(docker images -a --filter=dangling=true -q)'</code>


<code>alias docker_clean_ps='docker rm $(docker ps --filter=status=exited --filter=status=created -q)'</code>
== Related Pages ==


== Adding a path to container ==
* [[Linux]]
The docker client does not know about environment variables that are present inside the container, so prepending this at the command-line won't work indeed.
* [[Server]]
* [[Hardware]]
* [[Network]]


If this is just for an interactive session,  this should probably work<ref>https://github.com/moby/moby/issues/35435</ref>
docker run -it <container> bash -c 'exec env PATH=/home/app:$PATH bash'
or
docker run -it <container> /path-to-script/entrypoint.sh bash


[[Category:Linux]]
== Knowledge Graph ==


Related


→ [[Kubernetes]]
→ [[Dockerfile]]
→ [[Docker Compose]]
→ [[Docker]]
→ [[Container]]


== Reference ==
[[Category:Guide]]
<references />

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