Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
 
(연관된 글이 2개 있습니다.)

docker - RULE_APPEND failed (No such file or directory): rule in chain DOCKER

docker run 시에 포트를 지정했더니 오류가 발생합니다.

$ docker run -p 15000:15000 hello-world
docker: Error response from daemon: driver failed programming external connectivity on endpoint eloquent_goldwasser (132d751ee683967072afa5ea77a3cb217b817fcc5f1fb2aa45306316cfd749a7): Unable to enable DNAT rule:  (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 15000 -j DNAT --to-destination 172.17.0.2:15000 ! -i docker0: Warning: Extension tcp revision 0 not supported, missing kernel module?
Warning: Extension DNAT revision 0 not supported, missing kernel module?
iptables v1.8.10 (nf_tables):  RULE_APPEND failed (No such file or directory): rule in chain DOCKER
 (exit status 4)).

메시지에 나온 명령어를 그대로 실행해도 오류가 재현되는데요,

$ iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 15000 -j DNAT --to-destination 172.17.0.2:15000 ! -i docker0
iptables v1.8.10 (nf_tables): Could not fetch rule set generation id: Permission denied (you must be root)

$ sudo iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 15000 -j DNAT --to-destination 172.17.0.2:15000 ! -i docker0
Warning: Extension tcp revision 0 not supported, missing kernel module?
Warning: Extension DNAT revision 0 not supported, missing kernel module?
iptables v1.8.10 (nf_tables):  RULE_APPEND failed (No such file or directory): rule in chain DOCKER

아래의 글을 읽어보면,

리눅스서버 보안 iptables 완벽사용법
; https://www.linux.co.kr/bbs/board.php?bo_table=lecture&wr_id=3746

docker가 하려고 했던 것이 대충 해석이 됩니다.

-t nat: nat 테이블을 대상으로,
-A DOCKER: "Append" 모드로 DOCKER 체인에,
-p tcp: TCP 프로토콜 중,
-d 0/0 --dport 15000: destination으로 "0.0.0.0:15000"으로 지정된 패킷을,
-j DNAT: "Destination NAT"으로 대상을 172.17.0.2:15000으로 전달

현재 설정에 보면 "-A DOCKER"로 지정된 "DOCKER" chain을 확인할 수 있고 기본적으로는 "DROP" 정책이 적용돼 있습니다.

$ sudo iptables --list
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy DROP)
target     prot opt source               destination
DOCKER-USER  all  --  anywhere             anywhere
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain DOCKER (1 references)
target     prot opt source               destination

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-ISOLATION-STAGE-2 (1 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-USER (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere

그렇다면 혹시 FORWARD 체인 전체를 ACCEPT로 바꾸면 되지 않을까요? ^^ 이거저거 건드리기 전에 백업을 좀 해두고,

[백업]
$ sudo iptables-save > /some/file

[복원]
$ sudo iptables-restore > /some/file

다음과 같이 처리했는데,

$ sudo iptables -P FORWARD ACCEPT
$ sudo iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
...[생략]...

아쉽게도 docker run 시 iptables 명령 자체의 실행은 계속하는 바람에 오류는 여전히 발생했습니다.




결국 문제는, 적어도 iptables를 직접 실행했을 때의 오류는 발생해서는 안 되는 것입니다.

자, 그럼 천천히 뜯어볼까요? ^^ 일단 위의 상황을 보면 딱히 iptables 명령어의 옵션이 잘못된 것도 없습니다. 그렇다면, 이제 경고가 눈에 들어오는데요,

Warning: Extension tcp revision 0 not supported, missing kernel module?
Warning: Extension DNAT revision 0 not supported, missing kernel module?

이에 대해 검색해 보면,

iptables comment extension missing
; https://github.com/NixOS/nixpkgs/issues/242853

위의 글은 "comment" extension이 없어 발생한 상황이지만, 답글에 "xt_mark", "xt_comment", "xt_multiport" 3개의 모듈을 올리고 정상적으로 구동됐다는 내용이 있는 것으로 봐서 저 역시 관련 커널 모듈이 없어서 발생한 문제로 보입니다.

그래서, docker가 정상 동작하는 다른 머신에서 커널 모듈을 확인하고,

$ lsmod | grep tables
nf_tables             372736  1891 nft_compat,nft_chain_nat,nft_limit
libcrc32c              12288  3 nf_conntrack,nf_nat,nf_tables
nfnetlink              20480  4 nft_compat,nf_conntrack_netlink,nf_tables
ip6_tables             36864  1 ip6table_filter
arp_tables             28672  0
ip_tables              32768  1 iptable_filter
x_tables               65536  21 ip6table_filter,xt_conntrack,xt_statistic,iptable_filter,nft_compat,xt_LOG,xt_tcpudp,xt_addrtype,xt_CHECKSUM,xt_nat,ip6t_rt,xt_comment,ip6_tables,ipt_REJECT,ip_tables,xt_limit,xt_hl,xt_MASQUERADE,ip6t_REJECT,xt_mark,arp_tables

제 것을 비교해 보면,

$ lsmod | grep tables
nf_tables             221184  55 nft_compat,nft_counter,nft_chain_nat
nfnetlink              20480  4 nft_compat,nf_conntrack_netlink,nf_tables
ip_tables              32768  0
x_tables               49152  5 xt_conntrack,nft_compat,xt_addrtype,ip_tables,xt_MASQUERADE

대충 감으로, "xt_tcpudp", "xt_nat" 모듈이 없어서 발생한 것 같습니다. 테스트를 위해 모듈을 로드하고,

// How to Add, Remove, and Manage Linux Kernel Modules (Drivers)
// ; https://www.cyberciti.biz/faq/add-remove-list-linux-kernel-modules/

$ sudo insmod /usr/lib/modules/5.15.119/kernel/net/netfilter/xt_tcpudp.ko
$ sudo insmod /usr/lib/modules/5.15.119/kernel/net/netfilter/xt_nat.ko

명령어를 실행했더니 잘됩니다. ^^

$ sudo iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 15000 -j DNAT --to-destination 172.17.0.2:15000 ! -i docker0

물론, docker도 잘됩니다.

$ docker run -p 15000:15000 hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.
...[생략]...




insmod로 로드한 커널 모듈은 시스템을 재부팅하면 사라지므로, 영구적으로 로드하기 위해 아래의 글을 따라,

2.10. 시스템 부팅 시 커널 모듈 자동 로드
; https://docs.redhat.com/ko/documentation/red_hat_enterprise_linux/8/html/managing_monitoring_and_updating_the_kernel/loading-kernel-modules-automatically-at-system-boot-time_managing-kernel-modules

modules.conf 파일을 이런 식으로 수정했습니다.

$ ll /etc/modules-load.d/modules.conf
lrwxrwxrwx 1 root root 10 Aug  8 23:51 /etc/modules-load.d/modules.conf -> ../modules

$ cat /etc/modules-load.d/modules.conf
...[생략]...
nft_chain_nat
nft_counter
nft_compat
nf_tables
xt_conntrack
xt_MASQUERADE
xt_addrtype
xt_tcpudp
xt_nat
veth

dhd

snd-seq

재부팅하고 다시 살펴보면 끝!

$ sudo reboot now

$ lsmod | grep tables
nf_tables             221184  55 nft_compat,nft_counter,nft_chain_nat
nfnetlink              20480  4 nft_compat,nf_conntrack_netlink,nf_tables
ip_tables              32768  0
x_tables               49152  7 xt_conntrack,nft_compat,xt_tcpudp,xt_addrtype,xt_nat,ip_tables,xt_MASQUERADE

참고로, 저렇게 docker 또는 iptables가 실행된 이후 설정 상태를 보면,

$ sudo iptables --list
...[생략]...

Chain DOCKER (1 references)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             172.17.0.2           tcp dpt:ssh

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

...[생략]...

ACCEPT 설정이 DOCKER 체인에 추가된 것을 확인할 수 있습니다.




기타 시행착오로, 아래의 글에서,

Failed to setup IP tables: Unable to enable NAT rule
; https://stackoverflow.com/questions/54380847/failed-to-setup-ip-tables-unable-to-enable-nat-rule

나온 설명대로 따라 해봤으나,

$ sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
update-alternatives: using /usr/sbin/iptables-legacy to provide /usr/sbin/iptables (iptables) in manual mode
$ sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
update-alternatives: using /usr/sbin/ip6tables-legacy to provide /usr/sbin/ip6tables (ip6tables) in manual mode

이후 docker 서비스가 실행되지 않습니다.

$ sudo journalctl -u docker.service
Sep 09 16:28:48 Khadas systemd[1]: Starting docker.service - Docker Application Container Engine...
Sep 09 16:28:49 Khadas dockerd[1210]: time="2024-09-09T16:28:49.309579811+09:00" level=info msg="Starting up"
Sep 09 16:28:49 Khadas dockerd[1210]: time="2024-09-09T16:28:49.320183919+09:00" level=info msg="detected 127.0.0.53 nameserver, assuming systemd-resolved, so using re>
Sep 09 16:28:49 Khadas dockerd[1210]: time="2024-09-09T16:28:49.626823266+09:00" level=info msg="[graphdriver] using prior storage driver: overlay2"
Sep 09 16:28:49 Khadas dockerd[1210]: time="2024-09-09T16:28:49.742570336+09:00" level=info msg="Loading containers: start."
Sep 09 16:28:49 Khadas dockerd[1210]: time="2024-09-09T16:28:49.839517543+09:00" level=info msg="unable to detect if iptables supports xlock: 'iptables --wait -L -n': >
Sep 09 16:28:49 Khadas dockerd[1210]: failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to register "bridge" d>
Sep 09 16:28:49 Khadas dockerd[1210]: Perhaps iptables or your kernel needs to be upgraded.
Sep 09 16:28:49 Khadas dockerd[1210]:  (exit status 3)
...[생략]...

다시 삭제한 후에야 정상적으로 서비스가 돌아왔습니다.

$ sudo update-alternatives --remove iptables /usr/sbin/iptables-legacy
$ sudo update-alternatives --remove ip6tables /usr/sbin/ip6tables-legacy

$ sudo systemctl start docker




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

[연관 글]






[최초 등록일: ]
[최종 수정일: 9/10/2024]

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

비밀번호

댓글 작성자
 




... 16  17  18  19  [20]  21  22  23  24  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
13439정성태11/10/202311515닷넷: 2158. C# - 소켓 포트를 미리 시스템에 등록/예약해 사용하는 방법(Port Exclusion Ranges)파일 다운로드1
13438정성태11/9/202311014닷넷: 2157. C# - WinRT 기능을 이용해 윈도우에서 실행 중인 Media App 제어
13437정성태11/8/202311232닷넷: 2156. .NET 7 이상의 콘솔 프로그램을 (dockerfile 없이) 로컬 docker에 배포하는 방법
13436정성태11/7/202311311닷넷: 2155. C# - .NET 8 런타임부터 (Reflection 없이) 특성을 이용해 public이 아닌 멤버 호출 가능
13435정성태11/6/202310588닷넷: 2154. C# - 네이티브 자원을 포함한 관리 개체(예: 스레드)의 GC 정리
13434정성태11/1/202310538스크립트: 62. 파이썬 - class의 정적 함수를 동적으로 교체
13433정성태11/1/20239389스크립트: 61. 파이썬 - 함수 오버로딩 미지원
13432정성태10/31/202310196오류 유형: 878. 탐색기의 WSL 디렉터리 접근 시 "Attempt to access invalid address." 오류 발생
13431정성태10/31/202310739스크립트: 60. 파이썬 - 비동기 FastAPI 앱을 gunicorn으로 호스팅
13430정성태10/30/202310938닷넷: 2153. C# - 사용자가 빌드한 ICU dll 파일을 사용하는 방법
13429정성태10/27/202311067닷넷: 2152. Win32 Interop - C/C++ DLL로부터 이중 포인터 버퍼를 C#으로 받는 예제파일 다운로드1
13428정성태10/25/202311218닷넷: 2151. C# 12 - ref readonly 매개변수
13427정성태10/18/202310724닷넷: 2150. C# 12 - 정적 문맥에서 인스턴스 멤버에 대한 nameof 접근 허용(Allow nameof to always access instance members from static context)
13426정성태10/13/202311232스크립트: 59. 파이썬 - 비동기 호출 함수(run_until_complete, run_in_executor, create_task, run_in_threadpool)
13425정성태10/11/202311102닷넷: 2149. C# - PLinq의 Partitioner<T>를 이용한 사용자 정의 분할파일 다운로드1
13423정성태10/6/202311001스크립트: 58. 파이썬 - async/await 기본 사용법
13422정성태10/5/202310751닷넷: 2148. C# - async 유무에 따른 awaitable 메서드의 병렬 및 예외 처리 [1]
13421정성태10/4/202311022닷넷: 2147. C# - 비동기 메서드의 async 예약어 유무에 따른 차이
13420정성태9/26/202319252스크립트: 57. 파이썬 - UnboundLocalError: cannot access local variable '...' where it is not associated with a value
13419정성태9/25/202310846스크립트: 56. 파이썬 - RuntimeError: dictionary changed size during iteration
13418정성태9/25/202312479닷넷: 2146. C# - ConcurrentDictionary 자료 구조의 동기화 방식
13417정성태9/19/202311776닷넷: 2145. C# - 제네릭의 형식 매개변수에 속한 (매개변수를 가진) 생성자를 호출하는 방법
13416정성태9/19/202310371오류 유형: 877. redis-py - MISCONF Redis is configured to save RDB snapshots, ...
13415정성태9/18/202311815닷넷: 2144. C# 12 - 컬렉션 식(Collection Expressions) [2]
13414정성태9/16/202311144디버깅 기술: 193. Windbg - ThreadStatic 필드 값을 조사하는 방법
13413정성태9/14/202311961닷넷: 2143. C# - 시스템 Time Zone 변경 시 이벤트 알림을 받는 방법
... 16  17  18  19  [20]  21  22  23  24  25  26  27  28  29  30  ...