大网数据广告活动
服务器运维问答

How to manage a Docker container with port mapping?

· 52CE 我爱测

I installed the ELK Docker container . I run it with these parameters: sudo docker run -p 5601:5601 -p 9200:9200 -p 5044:5044 -p 5000:5000 -it --name elk sebp/elk Check check: sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STA…

问题原文

原帖链接 · wishi · CC BY-SA 3.0

I installed the ELK Docker container.

I run it with these parameters:

sudo docker run -p 5601:5601 -p 9200:9200 -p 5044:5044 -p 5000:5000 -it --name elk sebp/elk

Check check:

sudo docker ps

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                                                                                      NAMES
4f42137c954d        sebp/elk:latest     "/usr/local/bin/star   22 hours ago        Up 22 hours         0.0.0.0:5000->5000/tcp, 0.0.0.0:5044->5044/tcp, 0.0.0.0:5601->5601/tcp, 0.0.0.0:9200->9200/tcp, 9300/tcp   elk            

Jo, looks good.

Here is the external interface of the KVM guest, in which the Docker containers reside:

marius@elk:~$ ifconfig eth0 eth0 Link encap:Ethernet HWaddr 52:54:00:15:cf:f2
inet addr:192.168.100.134 Bcast:192.168.100.255 Mask:255.255.255.0

But port 5044 (for Elastic Beats e.g.) is closed in the KVM network interface:

marius@elk:~$ nc -v 192.168.100.134 5044
nc: connect to 192.168.100.134 port 5044 (tcp) failed: Connection refused

The reason for this appears to be, that I created chaos:

marius@elk:~$ sudo iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
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         
ACCEPT     tcp  --  anywhere             172.17.0.4           tcp dpt:5000
ACCEPT     tcp  --  anywhere             172.17.0.4           tcp dpt:5044
ACCEPT     tcp  --  anywhere             172.17.0.4           tcp dpt:5601
ACCEPT     tcp  --  anywhere             172.17.0.4           tcp dpt:9200

And the new IP of the Docker container after a restart is 172.17.42.1 and 0.4

marius@elk:~$ /sbin/ifconfig docker0 Link encap:Ethernet HWaddr a6:3d:01:38:7a:6a
inet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0

So, how do I restart / manage a Docker container in a way, so that I don't run into IPtables conflicts

And how do I fix this without creating a Docker / IPtables chaos each time? I don't want to manually flush away Docker's iptables.

已采纳解答

原帖链接 · Sobrique · CC BY-SA 3.0

First and foremost - this is because you're operating under a bit of a misconception. Docker containers have dynamic IP addressing within the docker network. You are asking for pain if you try and rely on that being static.

So don't - just get used to the idea that whilst containers have to have IP addresses to work, you shouldn't ever refer to them directly. You have a number of ways of avoiding this:

  • docker inspect + pass environment variables to containers.
  • With newer docker (1.10+) you have a name service
  • haproxy can dynamically reroute traffic to a range of IP addresses, so you can use something like haproxy + confd + etcd to 'detect' container locations, and dynamically add new entries to the haproxy configuration.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-03-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。