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?
相关问题
- Docker task in Azure devops won't accept "$(pw
- Unable to run mariadb when mount volume
- Unspecified error (0x80004005) while running a Doc
- What would prevent code running in a Docker contai
- How to reload apache in php-apache docker containe
In Fujimoto Youichi's example
test01
is a container, whereastest02
is an image.Before doing
docker run
you can remove the original container and then assign the container the same name again:(Using
-P
to expose ports to random ports rather than manually assigning).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 e.g. Pipework, if you need to expose a service in a running container without stopping/restarting it.
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.So you don't need to create an image with this approach. You can also change the restart flag here.
P.S. 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.04If you run
docker run <NAME>
it will spawn a new image, which most likely isn't what you want.If you want to change a current image do the following:
docker ps -a
Take the id of your target container and go to:
Stop the container:
Change the files
And change file
Restart your docker and it should work.
As a complement of the response of @Fujimoto-Youichi
You can also use
$ docker run -P CONTAINER
to map ports randomly while creating your container, but be careful with security ascpect !then run
docker inspect CONTAINER
to fin ports mapping as shown in the image below :.
Read more about
docker run -P
in docker runwe an use handy tools like ssh to accomplish this easily.
I was using ubuntu host and ubuntu based docker image.
when a new port is needed to be mapped out,
inside the docker run the following command
172.17.0.1 was the ip of the docker interface (you can get this by running
ifconfig docker0 | grep "inet addr" | cut -f2 -d":" | cut -f1 -d" "
on the host).here I had local 8888 port mapped back to the hosts 8888. you can change the port as needed.
if you need one more port, you can kill the ssh and add one more line of -R to it with the new port.
I have tested this with netcat.