13

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.

Thomas Kremmel
  • 14,575
  • 26
  • 108
  • 177
  • You might want to take a look at [this question](http://stackoverflow.com/questions/19897743/exposing-a-port-on-a-live-docker-container?rq=1) – icecrime Jul 11 '14 at 08:32
  • Do not like the answer :) Leave the question open. Probably there will be a way in the future to do this – Thomas Kremmel Jul 11 '14 at 09:39

4 Answers4

7
  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
JSON C11
  • 11,272
  • 7
  • 78
  • 65
0

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

Community
  • 1
  • 1
phoenix
  • 607
  • 6
  • 13
0

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 --
0

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.

sxm1972
  • 722
  • 8
  • 15