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

"ip route" Commands in Dockerfile Generate Error

· 52CE 我爱测

I create a Docker container like this: $sudo docker build -t "logstash-collector" ~/logging/logstash/ $sudo docker run -d \ --user=root \ --name="logstash-collector-01" \ --net=logging \ --cap-add=NET_ADMIN \ logstash-collector A…

问题原文

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

I create a Docker container like this:

$sudo docker build -t "logstash-collector" ~/logging/logstash/

$sudo docker run -d \
--user=root \
--name="logstash-collector-01" \
--net=logging \
--cap-add=NET_ADMIN \
logstash-collector

And from there I can change its default gateway to an nginx container (for transparent load balancing):

$sudo docker exec -it logstash-collector-01 ip route delete default
$sudo docker exec -it logstash-collector-01 ip route add default via 172.18.0.10

And it works, and everything's fine:

$sudo docker exec -it logstash-collector-01 ip route 
default via 172.18.0.10 dev eth0 
172.18.0.0/16 dev eth0  proto kernel  scope link  src 172.18.0.3 

But when I try to automate this by putting those commands in my ~/logging/logstash/Dockerfile like:

FROM logstash
COPY logstash.conf /usr/local/share
CMD ["-f", "/usr/local/share/logstash.conf", "--config.reload.automatic"]
CMD ["ip", "route", "delete", "default"]
CMD ["ip", "route", "add", "default", "via", "172.18.0.10"]

I see them getting executed during build:

But the container fails to launch. sudo docker logs logstash-collector-01 tells me: RTNETLINK answers: File exists

That error happens if I issue the second ip route command without issuing the first one (Linux won't add a route if one is already in its active routing table). But how can this be? Are my Dockerfile's CMD commands processing out of order?

已采纳解答

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

This is because you can only specify one CMD per dockerfile.

CMD is what the container will be running (which would normally be logstash, a long-running, daemon-like process), so what are you doing is only running the last CMD.

See https://stackoverflow.com/questions/23692470/why-cant-i-use-docker-cmd-multiple-times-to-run-multiple-services#23693804

You could consider a lo-fi solution like using a single container's network to share between nginx and the logstash agents, but you then need to keep track of ports.

Higher up the chain are solutions like kubernetes that make this kind of thing easier.

In between, you might look into consul(/SkyDNS) to help with DNS discovery.

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

资料来源与适用范围

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

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

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