How do I assign a port mapping to an existing Docker container?
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?
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,
stop running container
docker stop test01
commit the container
docker commit test01 test02
NOTE: The above, test02
is a new image that I'm constructing from the test01
container.
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.
You can change the port mapping by directly editing the hostconfig.json
file at /var/lib/docker/containers/[hash_of_the_container]/hostconfig.json
You can determine the [hash_of_the_container] via the docker inspect <container_name>
command and the value of the "Id" field is the hash.
1) stop the container
2) change the file
3) restart your docker engine (to flush/clear config caches)
4) start the container
So you don't need to create an image with this approach. You can also change the restart flag here.
PS You may visit https://docs.docker.com/engine/admin/ to learn how to correctly restart your docker engine as per your host machine. I used sudo systemctl restart docker
to restart my docker engine that is running on Ubuntu 16.04
If by "existing" you mean "running", then it's not (currently) possible to add a port mapping.
You can, however, dynamically add a new network interface with eg Pipework, if you need to expose a service in a running container without stopping/restarting it.
链接地址: http://www.djcxy.com/p/18230.html上一篇: 如何在码头集装箱内使用sudo?