Docker systemctl 사용하는 방법

epiphany·2025년 3월 21일

Docker Container에서 systemctl 사용이 안되는 이유

systemd daemon이 포함은 되어있지만 기본적으로 inactive로 설정되어짐

이유는 daemon을 실행할 때, cgroup을 못찾거나 권한 문제로 이용할 수 없기 때문

  • cgroup은 docker host의 linux kernel를 빌려쓰기 때문에 linux의 자원을 group 단위로 할당하는 역할을 한다.

각 OS Container에서 systemd를 통해 실행하는 daemon이 해당 자원을 할당받지 못하게 해놨고, 이것 때문에 이용할 수 없음

→ 보안에 위협이 되기 때문에 권한을 제한해둠

** systemctl이란 systemd를 관리하는 명령어

systemctl 사용을 위한 설정 방법

Dockerfile을 아래와 같이 작성

FROM ubuntu
 
ARG DEBIAN_FRONTEND=noninteractive
 
RUN sed -i 's/kr.archive.ubuntu.com/mirror.kakao.com/g' /etc/apt/sources.list
 
RUN apt-get update && apt-get upgrade -y \
        && apt-get install -y init systemd \
        && apt-get install -y build-essential \
        && apt-get install -y vim curl \
        && apt-get install -y net-tools iputils-ping \
        && apt-get clean autoclean \
        && apt-get autoremove -y \
        && rm -rf /var/lib/{apt,dpkg,cache,log}
 
CMD ["/sbin/init"]

Docker 빌드 후 실행

docker build --rm -t ubuntu-systemd4 .
 
docker run -d --privileged=true ubuntu-systemd4 /sbin/init
docker ps
docker exec -it 0e31fee5e47d bash
profile
iamda.tistory.com 이사 중

0개의 댓글