Docker.github.io: How do you map a port on running container?

Created on 13 Oct 2017  ·  20Comments  ·  Source: docker/docker.github.io

On runnning container, want to export a port outside, Any ways can quick reach it ?
docker commit and new a container is not a good idea in my stuation.
iptables seems can not take effect.

areEngine

Most helpful comment

Tried and tested.

  • Stop your container using docker stop <container-name/container-id>.

  • Edit hostconfig.json and config.v2.json files of the respective container by adding your port to PortBindings key and ExposedPorts key respectively.

  • You'll require sudo access, or as root user. Then run systemctl stop docker and then run systemctl start docker.

  • Finally start your container using docker start <container-name/container-id>.

All 20 comments

thanks, any progress?

You can't change a port mapping on a running container, but you can stop the container and restart using the -p host:container flag or by using the correct directive in the Docker compose file. See https://docs.docker.com/compose/compose-file/#ports.

@mstanleyjones
docker stop container_id
docker start -p host:container container_id ?
It's not a legitimate command...

You're right, you'd run the docker run again:

docker run -d -p 4000:4000 --name mytest mistytest

docker container stop mytest

docker container start mytest # same port

docker container stop mytest

docker container run -p 4001:4000 mytest # Now mapping container port 4000 to host port 4001

@mstanleyjones
It can not run success, see follow
docker container run -p 3306:3306 some-mysql
Unable to find image 'some-mysql:latest' locally
docker: Error response from daemon: Get https://registry-1.docker.io/v2/library/some-mysql/manifests/latest: unauthorized: incorrect username or password.
See 'docker run --help'.

You need to specify image name correctly which is mysql not some-mysql
So the command should be

docker run -p 3306:3306 --name name_for_container mysql

name_of_container is just a name which you can give to the container so that it would be easy to run commands related to container like docker exec

Thanks, but we talk about remaping port for a container, the "some-mysql" is that container which want to remaping port. docker run -p would create a new container not modify the old one.
If can not be modified, all of the containers's relationship will be disabled, and I must to recreate some of them.
@bhavin192 @mstanleyjones

I will try to be more clear.

  1. docker stop <running-container-name>
  2. docker run -p <new-port-mapping> <container-name-from-step-1>

Tried and tested.

  • Stop your container using docker stop <container-name/container-id>.

  • Edit hostconfig.json and config.v2.json files of the respective container by adding your port to PortBindings key and ExposedPorts key respectively.

  • You'll require sudo access, or as root user. Then run systemctl stop docker and then run systemctl start docker.

  • Finally start your container using docker start <container-name/container-id>.

@JonathanAlumbaugh Make a search on your machine for hostconfig.json. You should find a file
in /var/lib/docker/containers/<container-id>.

@sadjunky In my case, systemctl stop docker should be executed before edit hostconfig.json and
config.v2.json; or the config files will be restored.

@sadjunky In my case, systemctl stop docker should be executed before edit hostconfig.json and
config.v2.json; or the config files will be restored.

Haven't checked, will try this out. Thanks for the heads up!

@sadjunky In my case, systemctl stop docker should be executed before edit hostconfig.json and
config.v2.json; or the config files will be restored.

I edit the hostconfig.json before systemctl stop docker and it worked.
It's Ubuntu 18.04 x86_64 with

docker --version
Docker version 18.09.5, build e8ff056

Tried and tested.

  • Stop your container using docker stop <container-name/container-id>.
  • Edit hostconfig.json and config.v2.json files of the respective container by adding your port to PortBindings key and ExposedPorts key respectively.
  • You'll require sudo access, or as root user. Then run systemctl stop docker and then run systemctl start docker.
  • Finally start your container using docker start <container-name/container-id>.

i guess its not ExposedPort inside config.v2.json rather its Ports.

Also when i did sudo service docker stop it removed my docker :/

how can I stop port of a docker, running container.

Use dockerFile can solve this problem. More pls see the link: https://bobcares.com/blog/docker-change-container-configuration/

@Eyshika
Also when i did sudo service docker stop it removed my docker :/

The same happened to me because I made a mistake (typo) in the configuration, and when I fixed it the container reappeared (after restarting docker). It should probably be the same for you.

@JonathanAlumbaugh Make a search on your machine for hostconfig.json. You should find a file
in /var/lib/docker/containers/<container-id>.

Can you give the location for Mac OSX please ? Thanks

@HamaniKhalil
docker run -v /var/lib/docker:/var/lib/docker ubuntu bash
then /var/lib/docker/containers/<container-id> in the bash container that starts.

ref: https://github.com/moby/moby/issues/28283

An alternative to the above option of exposing port on a running container from the host machine is to create an image from the container. Then run the newly created image with desired ports to create a container with the port bindings

docker commit -m "msg" ``docker -l -q`` <your-container:version_number>
The above code would create a new image tagged with the [version_number]
then:
docker run -it --name <container_name> --hostname <container_name> <new_image>
The above command would create a container using the created image.
well the hostname option is not needed if your're running in detatched or daemon mode.

Note: Editing the container config files and stopping the docker for such modification might results into unwanted issues. I prefer this option rather than editing the config files.

Was this page helpful?
0 / 5 - 0 ratings