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 >

  • How to backup the cassandra running in docker container
  • Mapping a Volume in a docker
  • Execute command in linked docker container
  • Shipyard with existing Docker-Swarm
  • proxypass to phpmyadmin container but got error 502 and Connection refused
  • ERROR: Device or resource busy, when running dockerfile or creating container on RedHat 6.8
  • 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.

  • Restart docker when it consumes certain amount of memory
  • update running docker container
  • Docker daemon running under SSL contacted using cURL
  • docker --add-hosts flag equivalent in remote API?
  • Mac Docker Machine ZSH shell set env variable not applying
  • Delete a recreate a docker container using the same volume
  • 2 Solutions collect form web for “Docker Change Port Binding”

    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

    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 --
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.