How do I assign a port mapping to an existing Dock

2019-01-07 01:43发布

I'm not sure if I've misunderstood something here, but it seems like it's only possible to set port mappings by creating a new container from an image. Is there a way to assign a port mapping to an existing Docker container?

12条回答
ら.Afraid
2楼-- · 2019-01-07 02:04

For Windows & Mac Users, now there is another pretty easy and friendly way to change the mapping port:

  1. download the kitematic

  2. go the settings page of the container, on ports tab, you can directly modify the published port there.

  3. start the container again

查看更多
虎瘦雄心在
3楼-- · 2019-01-07 02:05

I'm also interested in this problem.

As @Thasmo mentioned, port forwardings can be specified ONLY with docker run command.
Other commands, docker start does not have -p option and docker port only displays current forwardings.

To add port forwardings, I always follow these steps,

  1. stop running container

    docker stop test01
    
  2. commit the container

    docker commit test01 test02
    

    NOTE: The above, test02 is a new image that I'm constructing from the test01 container.

  3. re-run from the commited image

    docker run -p 8080:8080 -td test02
    

Where the first 8080 is the local port and the second 8080 is the container port.

查看更多
走好不送
4楼-- · 2019-01-07 02:06

The other way around you if you are not comfortable with Docker depth configuration IPtables would be your friend.

iptables -t nat -A DOCKER -p tcp --dport ${YOURPORT} -j DNAT --to-destination ${CONTAINERIP}:${YOURPORT}

iptables -t nat -A POSTROUTING -j MASQUERADE -p tcp --source ${CONTAINERIP} --destination ${CONTAINERIP} --dport ${YOURPORT}

iptables -A DOCKER -j ACCEPT -p tcp --destination ${CONTAINERIP} --dport ${YOURPORT}

This is just a trick not a recommended way this works with my scenario because i could not stop container i hope will help you as well.

查看更多
倾城 Initia
5楼-- · 2019-01-07 02:07

If you simply want to change the port of the running container, you do:

  1. stop existing container

    sudo docker stop NAME

  2. now restart with the new port mapping

    sudo docker run -d -p 81:80 NAME

where as:

"-d" to background / deamon the docker

"-p" enable port mapping

"81" external (exposed) port you use to access with your browser

"80" internal docker container listen port

查看更多
Luminary・发光体
6楼-- · 2019-01-07 02:15

Not sure if you can apply port mapping a running container. You can apply port forwarding while running a container which is different than creating a new container.

$ docker run -p <public_port>:<private_port> -d <image>  

will start running container. This tutorial explains port redirection.

查看更多
乱世女痞
7楼-- · 2019-01-07 02:15

Editing hostconfig.json seems to not working now. It only ends with that port being exposed but not published to host. Commiting and recreating containers is not the best approach to me. No one mentioned docker network?

The best solution would be using reversed proxy within the same network

  1. Create a new network if your previous container not in any named ones.

    docker network create my_network

  2. Join your existing container to the created network

    docker network connect my_network my_existing_container

  3. Start a reversed proxy service(e.g. nginx) publishing the ports you need, joining the same network

    docker run -d --name nginx --network my_network -p 9000:9000 nginx

    Optionally remove the default.conf in nginx

    docker exec nginx rm /etc/nginx/conf.d/default.conf

  4. Create a new nginx config

    server
    {
        listen 9000;
    
        location / {
            proxy_pass http://my_existing_container:9000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }
    

    Copy the config to nginx container.

    docker cp ./my_conf.conf nginx:/etc/nginx/conf.d/my_conf.conf

  5. Restart nginx

    docker restart nginx

Advantages: To publish new ports, you can safely stop/update/recreate nginx container as you wish without touching the business container. If you need zero down time for nginx, it is possible to add more reversed proxy services joining the same network. Besides, a container can join more than one network.

Edit:

To reverse proxy non-http services, the config file is a bit different. Here is a simple example:

upstream my_service {
    server my_existing_container:9000;
}

server {
    listen 9000;
    proxy_pass my_service;
}
查看更多
登录 后发表回答