Microsoft MVP성태의 닷넷 이야기
Linux: 61. docker - 원격 제어를 위한 TCP 바인딩 추가 [링크 복사], [링크+제목 복사]
조회: 3711
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

docker - 원격 제어를 위한 TCP 바인딩 추가

예전에, Portainer 등의 서비스에서 관리를 하기 위해, 혹은 docker 클라이언트 측의 -H 옵션으로 접근하기 위해 daemon.json 파일의 "hosts" 옵션을 추가하는 방법을 알아봤는데요,

Portainer에 윈도우 컨테이너를 등록하는 방법
; https://www.sysnet.pe.kr/2/0/12289

리눅스의 경우에도 daemon.json 파일의 설정을 바꿔야 하는 것은 동일하지만 접근 방법이 다소 다릅니다.

$ cat /etc/docker/daemon.json
{
  "insecure-registries" : ["192.168.100.51:5000"]
}

윈도우와는 달리 기본 IPC 통신 방법이 npipe가 아닌 리눅스 소켓이기 때문에,

$ netstat -ano | grep docker
unix  2      [ ACC ]     STREAM     LISTENING     45095    /var/run/docker.sock
unix  2      [ ACC ]     STREAM     LISTENING     228938836 /var/run/docker/metrics.sock
unix  2      [ ACC ]     STREAM     LISTENING     228931446 /var/run/docker/libnetwork/a3ed856b16e8.sock

daemon.json 파일의 내용을 다음과 같이 변경해야 합니다.

$ cat /etc/docker/daemon.json
{
  "insecure-registries" : ["192.168.100.51:5000"],
  "hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]
}

$ dockerd --validate --config-file=/etc/docker/daemon.json
configuration OK

그런 후, service의 설정 파일에서 ExecStart 실행 옵션을 조정해야 하는데요, 우선 기존 ExecStart의 옵션을 알아내기 위해 docker.service 위치를 알아내고,

$ sudo systemctl status docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2023-06-26 16:31:48 KST; 59s ago
     Docs: https://docs.docker.com
...[생략]...

ExecStart 명령행을 확인합니다.

$ cat /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service time-set.target
Wants=network-online.target containerd.service
Requires=docker.socket

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutStartSec=0

...[생략]...

위의 명령행에서 "fd://"를 제거한 ExecStart를 설정해야 하는데요, 이 값을 직접 "/usr/lib/systemd/system/docker.service" 파일에서 편집하기보다는 다음과 같이 새롭게 override.conf 파일을 생성해 재정의하는 방식으로 진행해 보겠습니다.

$ mkdir -p /etc/systemd/system/docker.service.d

$ cd /etc/systemd/system/docker.service.d

// 이 디렉터리에 override.conf 파일을 생성 후, 다음의 내용을 갖도록 편집

$ cat override.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --containerd=/run/containerd/containerd.sock

이후 서비스를 재시작하면,

$ sudo systemctl daemon-reload

$ sudo systemctl restart docker.service

/etc/systemd/system/docker.service.d/override.conf 파일에 설정한 값이 반영된 것을 확인할 수 있고,

$ sudo systemctl status docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/docker.service.d
           └─override.conf
   Active: active (running) since Mon 2023-06-26 16:40:01 KST; 4s ago
     Docs: https://docs.docker.com
 Main PID: 13173 (dockerd)
    Tasks: 122
   Memory: 57.2M
   CGroup: /system.slice/docker.service
           ├─13173 /usr/bin/dockerd --containerd=/run/containerd/containerd.sock
...[생략]...
...[생략]... level=info msg="[graphdriver] using prior storage driver: overlay2"
...[생략]... level=info msg="Loading containers: start."
...[생략]... level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon optio...d IP address"
...[생략]... level=info msg="Loading containers: done."
...[생략]... level=warning msg="WARNING: API is accessible on http://0.0.0.0:2375 without encryption.\n         Access to the...
...[생략]... level=info msg="Docker daemon" commit=659604f graphdriver=overlay2 version=24.0.2
...[생략]... level=info msg="Daemon has completed initialization"
...[생략]... level=info msg="API listen on /var/run/docker.sock"
...[생략]... systemd[1]: Started Docker Application Container Engine.
...[생략]... dockerd[23914]: time="...[생략]..." level=info msg="API listen on [::]:2375"

명령행에 "-H fd://" 옵션이 삭제되었으므로 해당 옵션을 이제 daemon.json 파일에 있는 것으로 적용하게 돼 위와 같이 메시지에 나오는 것처럼 2375 포트를 대기하는 TCP 소켓을 확인할 수 있습니다.

$ netstat -tano | grep 2375
tcp6       0      0 :::2375                 :::*                    LISTEN      off (0.00/0/0)

"tcp6"이라고 나오기는 하지만 IPv4에 해당하는 "tcp"도 포함하고 있다고 하므로 사용에 문제가 없습니다.




위와 같이 설정했으면 이제 다른 컴퓨터에서 docker -H 명령을 내려 원격 제어가 가능합니다.

// docker host 컴퓨터의 IP가 192.168.100.50이라고 가정하고,
// 다른 컴퓨터에서 IPv4로 접속

$ docker -H 192.168.100.50:2375 info
...[생략]...

만약 로컬에서는 되는데,

$ docker -H 127.0.0.1:2375 info

원격에서만 안 된다면 방화벽을 조사해야 합니다.

Linux - 외부에서의 접속을 허용하기 위한 TCP 포트 여는 방법
; https://www.sysnet.pe.kr/2/0/13385




마지막으로, ExecStart의 값에서 "-H fd://"를 제거하지 않고 daemon.json에 "hosts"를 포함하게 되면, docker.service 시작에 실패하게 됩니다.

...[생략]... dockerd[29113]: unable to configure the Docker daemon with file /etc/docker/daemon.json: the following directives are specified both as a flag and in the configuration file: hosts: (from flag: [fd://], from file: [tcp://0.0.0.0:2375 unix:///var/run/docker.sock])
...[생략]...  systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
...[생략]...  systemd[1]: Failed to start Docker Application Container Engine.
-- Subject: Unit docker.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit docker.service has failed.
-- 
-- The result is failed.

즉, "fd://" 옵션의 사용은 배타적으로 설정해야 합니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 6/28/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  4  5  6  [7]  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13458정성태11/26/20232352닷넷: 2170. .NET Core/5+ 기반의 COM Server를 tlb 파일을 생성하는 방법(tlbexp)
13457정성태11/25/20232293VS.NET IDE: 187. Visual Studio - 16.9 버전부터 추가된 "Display inline type hints" 옵션
13456정성태11/25/20232596닷넷: 2169. C# - OpenAI를 사용해 PDF 데이터를 대상으로 OpenAI 챗봇 작성 [1]파일 다운로드1
13455정성태11/25/20232488닷넷: 2168. C# - Azure.AI.OpenAI 패키지로 OpenAI 사용파일 다운로드1
13454정성태11/23/20232838닷넷: 2167. C# - Qdrant Vector DB를 이용한 Embedding 벡터 값 보관/조회 (Azure OpenAI) [1]파일 다운로드1
13453정성태11/23/20232338오류 유형: 879. docker desktop 설치 시 "Invalid JSON string. (Exception from HRESULT: 0x83750007)"
13452정성태11/22/20232440닷넷: 2166. C# - Azure OpenAI API를 이용해 사용자가 제공하는 정보를 대상으로 검색하는 방법파일 다운로드1
13451정성태11/21/20232570닷넷: 2165. C# - Azure OpenAI API를 이용해 ChatGPT처럼 동작하는 콘솔 응용 프로그램 제작파일 다운로드1
13450정성태11/21/20232379닷넷: 2164. C# - Octokit을 이용한 GitHub Issue 검색파일 다운로드1
13449정성태11/21/20232458개발 환경 구성: 688. Azure OpenAI 서비스 신청 방법
13448정성태11/20/20232695닷넷: 2163. .NET 8 - Dynamic PGO를 결합한 성능 향상파일 다운로드1
13447정성태11/16/20232578닷넷: 2162. ASP.NET Core 웹 사이트의 SSL 설정을 코드로 하는 방법
13446정성태11/16/20232530닷넷: 2161. .NET Conf 2023 - Day 1 Blazor 개요 정리
13445정성태11/15/20232848Linux: 62. 리눅스/WSL에서 CA 인증서를 저장하는 방법
13444정성태11/15/20232605닷넷: 2160. C# 12 - Experimental 특성 지원
13443정성태11/14/20232629개발 환경 구성: 687. OpenSSL로 생성한 사용자 인증서를 ASP.NET Core 웹 사이트에 적용하는 방법
13442정성태11/13/20232450개발 환경 구성: 686. 비주얼 스튜디오로 실행한 ASP.NET Core 사이트를 WSL 2 인스턴스에서 https로 접속하는 방법
13441정성태11/12/20232764닷넷: 2159. C# - ASP.NET Core 프로젝트에서 서버 Socket을 직접 생성하는 방법파일 다운로드1
13440정성태11/11/20232437Windows: 253. 소켓 Listen 시 방화벽의 Public/Private 제어 기능이 비활성화된 경우
13439정성태11/10/20232964닷넷: 2158. C# - 소켓 포트를 미리 시스템에 등록/예약해 사용하는 방법(Port Exclusion Ranges)파일 다운로드1
13438정성태11/9/20232543닷넷: 2157. C# - WinRT 기능을 이용해 윈도우에서 실행 중인 Media App 제어
13437정성태11/8/20232752닷넷: 2156. .NET 7 이상의 콘솔 프로그램을 (dockerfile 없이) 로컬 docker에 배포하는 방법
13436정성태11/7/20232978닷넷: 2155. C# - .NET 8 런타임부터 (Reflection 없이) 특성을 이용해 public이 아닌 멤버 호출 가능
13435정성태11/6/20232905닷넷: 2154. C# - 네이티브 자원을 포함한 관리 개체(예: 스레드)의 GC 정리
13434정성태11/1/20232684스크립트: 62. 파이썬 - class의 정적 함수를 동적으로 교체
13433정성태11/1/20232397스크립트: 61. 파이썬 - 함수 오버로딩 미지원
1  2  3  4  5  6  [7]  8  9  10  11  12  13  14  15  ...