Thomas Kremmel
Thomas Kremmel

Reputation: 14783

Docker Change Port Binding

I'm running a container (let's call it old_container) with exposed port 80 and bind the port to the host interface on port 80 using the -p flag.

sudo docker run -p 80:80 -i -t < old-image-id >

In my production environment I want to switch now from the old_container to a new_container. So I want to shut down the old_container and start the new_container.

First I have to do some manual changes in the new_container. So I run it without the -p flag, as I cannot bind it to port 80 before I have done this changes.

sudo docker run -i -t < new-image-id >
#now I m doing my manual changes

When I'm finished with my changes I logout of the new_container. My plan was now to stop the old_container and bind the new_container to port 80. But the [start][1] command does not provide a port binding possibility.

So to come to my question: I'm looking to set the port binding for a stopped container, preferably without the workaround of creating a commit image of the new_container and running this image as another new container.

Upvotes: 13

Views: 23117

Answers (4)

JSON C11
JSON C11

Reputation: 11792

  1. Make the changes to your new container and then stop both the old and new containers.
docker stop old_container new_container
  1. Create a new image from a container’s changes via the commit command like so:
docker commit new_container new_container_01
  1. Run the newly commited image:
docker run -p 80:80 -i -t new_container_01
  1. Cleanup your old, unused containers with the rm (remove) command:
docker rm old_container new_container

Upvotes: 7

sxm1972
sxm1972

Reputation: 752

Once you run your new_container image and make the changes you require, save the new container as a new image using docker commit. then run this new image with -p flag to map the ports.

Upvotes: 0

Widi Harsojo
Widi Harsojo

Reputation: 26

I did create a tools for change PORT of Running Container for myboot2docker.

Ex: two(2) container:

  • docker run -p 11521:1521 --name=xe1 container1
  • docker run -p 21521:1521 --name=xe3 container3

I create another port-mapping for container1 using this command

p-map xe1 15210:1521

and then I connect my Spring Java application to localhost:15210

to switch to container3 just type this command

p-map xe3 15210:1521 

below are details command in action.

~ $ p-map
Change port of running container
Command: p-map <container_name> <host_port:guest_port>

DNAT     tcp  --  0.0.0.0/0       0.0.0.0/0       tcp dpt:21521 to:172.17.0.1:1521
DNAT     tcp  --  0.0.0.0/0       0.0.0.0/0       tcp dpt:11521 to:172.17.0.2:1521

~ $ p-map xe1 15210:1521
Change port of running container

sudo iptables -t nat -A DOCKER -p tcp --dport 15210 -j DNAT --to-destination 172.17.0.1:1521

DNAT     tcp  --  0.0.0.0/0       0.0.0.0/0       tcp dpt:21521 to:172.17.0.1:1521
DNAT     tcp  --  0.0.0.0/0       0.0.0.0/0       tcp dpt:11521 to:172.17.0.2:1521
DNAT     tcp  --  0.0.0.0/0       0.0.0.0/0       tcp dpt:15210 to:172.17.0.1:1521 --

~ $ p-map xe2 15210:1521
Change port of running container
Error: No such image or container: xe2

DNAT     tcp  --  0.0.0.0/0       0.0.0.0/0       tcp dpt:21521 to:172.17.0.1:1521
DNAT     tcp  --  0.0.0.0/0       0.0.0.0/0       tcp dpt:11521 to:172.17.0.2:1521
DNAT     tcp  --  0.0.0.0/0       0.0.0.0/0       tcp dpt:15210 to:172.17.0.1:1521 --

~ $ p-map xe3 15210:1521
Change port of running container

sudo iptables -t nat -D DOCKER -p tcp --dport 15210 -j DNAT --to-destination 172.17.0.1:1521
sudo iptables -t nat -A DOCKER -p tcp --dport 15210 -j DNAT --to-destination 172.17.0.2:1521

DNAT     tcp  --  0.0.0.0/0       0.0.0.0/0       tcp dpt:21521 to:172.17.0.1:1521
DNAT     tcp  --  0.0.0.0/0       0.0.0.0/0       tcp dpt:11521 to:172.17.0.2:1521
DNAT     tcp  --  0.0.0.0/0       0.0.0.0/0       tcp dpt:15210 to:172.17.0.2:1521 --

Upvotes: 0

phoenix
phoenix

Reputation: 617

Your use case sounds good, its quite interesting to see docker on PROD. What is the manual change you are going to do? Although, I assume the solution to your problem might be

1) Attaching & detaching a container (How do I attach to a running Docker container later?)

docker attach container_name ctrl p ctrl q

2) Running docker exec command

docker run -it -p 80:80 --name=old_container <old-image-id>

The above command will create a container named "old_container"

docker exec -d <old_container> mkdir foo

The above command will create foo directory on home folder of running container. I guess you can include your manual changes as a script in place of 'mkdir foo'(I never tried it)

P.S: docker exec is available only from docker 1.3 version

Upvotes: 0

Related Questions