Change docker container mapping port
To change the running container mapping port with or without recreating container.
By recreating container
Stop and commit running container, then run new container using new image.
This requires changing image name and knowing the docker run command parameters.
docker stop test01
docker commit test01 test02
docker run -p 8080:8080 -td test02
Modify configuration file
Stop the container and docker service, then change the docker container configuration file hostconfig.json
. After that, start docker service and container.
This requires updating docker run command document.
- Stop docker.
docker stop test01
systemctl stop docker
- Edit
hostconfig.json
file
vi /var/lib/docker/containers/[hash_of_the_container]/hostconfig.json
or following file when using snap
/var/snap/docker/common/var-lib-docker/containers/[hash_of_the_container]/hostconfig.json
- Start docker
systemctl start docker
docker start test01