Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker change published ports on live container

Tags:

docker

I'd like to change the published ports on a live container for example

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

And then later on, change that to another port, example -p 8080:80

like image 563
joedborg Avatar asked Mar 13 '15 12:03

joedborg


People also ask

How do you expose a new port on a running container?

You can do this in the following ways: Add an EXPOSE instruction in the Dockerfile. Use the –expose flag at runtime to expose a port. Use the -p flag or -P flag in the Docker run string to publish a port.

Can we expose a port on running container?

You cannot do this via Docker, but you can access the container's un-exposed port from the host machine.


1 Answers

Docker does not have a mechanism for changing the published ports of a container once it has started. When you publish a port, two things happen:

  • Docker creates iptables rules in the nat table that redirect traffic to the "public" port to the container.
  • Docker starts a proxy service listening on that port to handle locally generated traffic.

While you could in theory manually update the firewall rules to make the service available at a new port, you would not be able to unbind the Docker proxy and would thus be unable to start any new services using that "public" port.

Your best course of action is simply to delete the container and redeploy it, or rely on some sort of front-end proxy to handle the redirect rather than using Docker's port publishing mechanism.

like image 114
larsks Avatar answered Sep 21 '22 18:09

larsks