※ 당연히 컨테이너는 리눅스 VM의 브릿지 IP를 사용하므로, 호스트에선 해당 IP로 접근이 불가.
devops@localhost /]$ netstat -ntp
No info could be read for "-p": geteuid()=1001 but you should be root.)
ctive Internet connections (w/o servers)
roto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
cp 0 0 192.168.56.101:22 192.168.56.1:7645 ESTABLISHED -
cp 0 36 192.168.56.101:22 192.168.56.1:8220 ESTABLISHED -
호스트 -> 192.168.56.101:x --(포트포워딩)--> 172.17.0.x:80
-p 옵션으로 포트 포워딩
docker run -d --name myapache2 -p 9000:8080 httpd:2.4
docker run -d --name myapache3 -p 9001:80 httpd:2.4
docker ps
로 포트포워딩 정보가 보임.
[root@localhost devops]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
345a5337d740 httpd:2.4 "httpd-foreground" About a minute ago Up About a minute 0.0.0.0:9001->80/tcp myapache3
14d8e80807d1 httpd:2.4 "httpd-foreground" 5 minutes ago Up About a minute 80/tcp, 0.0.0.0:9000->8080/tcp myapache2
[root@localhost devops]# iptables -L -t nat -n | grep 900
DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:9000 to:172.17.0.2:8080
DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:9001 to:172.17.0.3:80
docker run -d --name mynginx -p 192.168.56.101:9002:80 nginx
nginx도 80번 포트 쓰니까 80번으로 할당
[root@localhost devops]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4507c7cfd026 nginx "/docker-entrypoint.…" 22 seconds ago Up 19 seconds 192.168.56.101:9002->80/tcp mynginx
345a5337d740 httpd:2.4 "httpd-foreground" 12 minutes ago Up 12 minutes 0.0.0.0:9001->80/tcp myapache3
14d8e80807d1 httpd:2.4 "httpd-foreground" 17 minutes ago Up 13 minutes 80/tcp, 0.0.0.0:9000->8080/tcp myapache2
eac85254712c mariadb "docker-entrypoint.s…" About an hour ago Exited (0) 48 minutes ago mydb
[root@localhost devops]#
curl http://127.0.0.1:9002 -> curl: (7) Failed connect to 127.0.0.1:9002; Connection refused
curl http://192.168.56.101:9002
-> 성공 (길어서 생략)docker run -d --name mynginx2 -p 192.168.56.101::80 nginx
::80