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?
You can change the port mapping by directly editing the hostconfig.json
file at
/var/lib/docker/containers/[hash_of_the_container]/hostconfig.json
or /var/snap/docker/common/var-lib-docker/containers/[hash_of_the_container]/hostconfig.json
if you installed Docker as a snap.
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.
docker stop <container_name>
).docker start <container_name>
).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.04.
Adding ports: As mentioned in the comments you might also want to export the port from the (containered) machine. (For example when a new port appears in docker inspect
but not in docker container ls
)
In this case you also have to edit the config.v2.json
file (next to hostconfig-file).
The syntax is like "ExposedPorts":{"8080/tcp":{}}
(if you wish to expose port 8080 from the container)