[Docker] 도커 컨테이너 자동 재시작

18 Oct 2023 - juno

#cloud  #docker 


2023-10-18 클라우드 프로그래밍 7주차 강의


  1. 도커 컨테이너 옵션을 활용한 재시작 방법
  2. 우분투 systemd 활용한 재시작 방법

1. 도커 컨테이너 옵션을 활용한 재시작 방법

docker run --restart=always -d your-image

도커 컨테이너 재시작 정책

이미 실행 중인 컨테이너의 재시작 정책을 변경하려면 docker update 명령어를 사용할 수 있음

docker update --restart=always your-container-id-or-name

2. 우분투 systemd 활용한 재시작 방법

sudo nano /etc/systemd/system/your-container.service

your-container.service 파일 내용

[Unit]
Description=Your Container Description
After=docker.service
Requires=docker.service
[Service]
ExecStart=/usr/bin/docker start -a your-container-name-or-id
ExecStop=/usr/bin/docker stop -t 2 your-container-name-or-id
Restart=always
RestartSec=5s
[Install]
WantedBy=multi-user.target

systemd service 옵션 내용

Systemd 데몬 리로드

sudo systemctl daemon-reload

서비스 활성화

sudo systemctl enable your-container.service

서비스 시작 및 상태 확인

sudo systemctl start your-container.service
sudo systemctl status your-container.service

#끝


reference

교수님 블로그, hull.kr