Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you map a port on running container? #4942

Closed
Mararesliu opened this issue Oct 13, 2017 · 21 comments
Closed

How do you map a port on running container? #4942

Mararesliu opened this issue Oct 13, 2017 · 21 comments
Labels
area/Engine

Comments

@Mararesliu
Copy link

@Mararesliu Mararesliu commented Oct 13, 2017

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.

@londoncalling londoncalling changed the title How to mapping port on running container? How do you map a port on running container? Oct 13, 2017
@londoncalling londoncalling added issue/support-request area/Engine labels Oct 13, 2017
@Mararesliu
Copy link
Author

@Mararesliu Mararesliu commented Oct 16, 2017

thanks, any progress?

@mdlinville
Copy link
Contributor

@mdlinville mdlinville commented Oct 16, 2017

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.

@Mararesliu
Copy link
Author

@Mararesliu Mararesliu commented Oct 17, 2017

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

@mdlinville
Copy link
Contributor

@mdlinville mdlinville commented Oct 17, 2017

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

@Mararesliu
Copy link
Author

@Mararesliu Mararesliu commented Oct 19, 2017

@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'.

@bhavin192
Copy link
Contributor

@bhavin192 bhavin192 commented Oct 20, 2017

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

@Mararesliu
Copy link
Author

@Mararesliu Mararesliu commented Oct 23, 2017

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

@mdlinville
Copy link
Contributor

@mdlinville mdlinville commented Oct 24, 2017

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>

@sadjunky
Copy link

@sadjunky sadjunky commented Nov 5, 2018

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>.

@sadjunky
Copy link

@sadjunky sadjunky commented Nov 8, 2018

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

@wanglailai
Copy link

@wanglailai wanglailai commented Nov 28, 2018

@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
Copy link

@sadjunky sadjunky commented Nov 28, 2018

@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!

@browncrane
Copy link

@browncrane browncrane commented Apr 26, 2019

@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

@Eyshika
Copy link

@Eyshika Eyshika commented Jul 10, 2019

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 :/

@bharatrbk
Copy link

@bharatrbk bharatrbk commented Jul 19, 2019

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

@geeksun
Copy link

@geeksun geeksun commented Aug 17, 2019

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

@hjonin
Copy link

@hjonin hjonin commented Sep 5, 2019

@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.

@HamaniKhalil
Copy link

@HamaniKhalil HamaniKhalil commented Apr 13, 2020

@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

@alexleekt
Copy link

@alexleekt alexleekt commented Apr 29, 2020

@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: moby/moby#28283

@matfolio
Copy link

@matfolio matfolio commented Dec 28, 2020

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.

@lemonaidrmeyers
Copy link

@lemonaidrmeyers lemonaidrmeyers commented Oct 5, 2021

Is there a way to change ports without recreating the containers on a mac with docker-compose and a yml? It's very very time consuming to recreate all the networked dockers when you want to test port changes.

This did not work for me @alexleekt

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/Engine
Projects
None yet
Development

No branches or pull requests