실행 중인 systemd 컨테이너 내에서 명령을 실행하는 방법

실행 중인 systemd 컨테이너 내에서 명령을 실행하는 방법

systemd사용할 수 있는 실행 중인 컨테이너가 있습니다 machinectl login <container>. 먼저 로그인하지 않고 명령을 실행한 다음 로그아웃하지 않고 컨테이너 내에서 직접 명령을 실행하려면 어떻게 해야 합니까?

systemd이것을 말하는 또 다른 방법은 다음과 동등한 것을 찾고 있다는 것입니다 .

$ docker exec <container> <command> 

또는

$ ssh <host> <command>

답변1

노력하다 systemd-run:

# systemd-nspawn -D <machine-root> -b 3 --link-journal host

# systemd-run --machine <machine-name> env
Running as unit run-1356.service.

# journalctl --machine <machine-name> -u run-1356 -b -q
Oct 30 07:45:09 jessie-64 systemd[1]: Started /usr/bin/env.
Oct 30 07:45:09 jessie-64 env[37]: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

에서 발췌맨페이지:

--machine= 스위치와 함께 셸(아래 참조) 또는 systemd-run(1)을 사용하여 대화형으로 또는 백그라운드에서 개별 명령을 직접 호출합니다.

(이 명령은 shell이후부터 사용 가능했습니다.v225)

답변2

시스템이 없거나 machinectl shell(예: CentOS 7 Atomic의 systemd v219) machinectl login어떤 이유로든 실패하는 경우 다음을 사용하여 직접 셸을 가져오거나 명령을 실행할 수 있습니다 nsenter.

예:

$ nsenter \
     --target=$(machinectl show --property Leader ipsec-libreswan | sed "s/^Leader=//") \
     --mount --uts --ipc --net --pid

관련 정보