Docker changed published ports on a live container

I would like to change published ports in a real container, for example

docker run -p 80:80 --name nginx_live nginx 

And then later, change this to another port, for example -p 8080:80

+6
source share
2 answers

Docker does not have a mechanism for changing published container ports after it starts. When you publish a port, two things happen:

  • Docker creates iptables rules in the nat table, which redirect traffic to the "public" port in the container.
  • Docker launches a proxy service that listens on this port to process locally generated traffic.

Although you could theoretically manually update the firewall rules to make the service available on the new port, you cannot cancel the Docker proxy and therefore cannot start any new services using this β€œpublic” port.

Your best course of action is to simply remove the container and redistribute it, or rely on some kind of external proxy server to handle the redirection, rather than using the Docker port publishing mechanism.

+5
source

This is not a Docker function.

But it’s easy to add another layer of indirection: expose one container port on your host, and then run an instance of nginx or the FORWARD firewall rule, which maps any local ports that you want to this docker port.

+2
source

Source: https://habr.com/ru/post/983744/


All Articles