How do I assign a port mapping to an existing Docker container?
docker, linux-containers, lxc, port
Solution
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.
- Stop the container (`docker stop <container_name>`).
- Stop docker service (per Tacsiazuma's comment)
- Change the file.
- Restart your docker engine (to flush/clear config caches).
- Start the container (`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.
Problem
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?