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?
相关问题
- Docker task in Azure devops won't accept "$(pw
- Unable to run mariadb when mount volume
- Unspecified error (0x80004005) while running a Doc
- What would prevent code running in a Docker contai
- How to reload apache in php-apache docker containe
For Windows & Mac Users, now there is another pretty easy and friendly way to change the mapping port:
I'm also interested in this problem.
As @Thasmo mentioned, port forwardings can be specified ONLY with
docker run
command.Other commands,
docker start
does not have-p
option anddocker port
only displays current forwardings.To add port forwardings, I always follow these steps,
stop running container
commit the container
NOTE: The above,
test02
is a new image that I'm constructing from thetest01
container.re-run from the commited image
Where the first 8080 is the local port and the second 8080 is the container port.
The other way around you if you are not comfortable with Docker depth configuration IPtables would be your friend.
This is just a trick not a recommended way this works with my scenario because i could not stop container i hope will help you as well.
If you simply want to change the port of the running container, you do:
where as:
Not sure if you can apply port mapping a running container. You can apply port forwarding while running a container which is different than creating a new container.
will start running container. This tutorial explains port redirection.
Editing hostconfig.json seems to not working now. It only ends with that port being exposed but not published to host. Commiting and recreating containers is not the best approach to me. No one mentioned
docker network
?The best solution would be using reversed proxy within the same network
Create a new network if your previous container not in any named ones.
docker network create my_network
Join your existing container to the created network
docker network connect my_network my_existing_container
Start a reversed proxy service(e.g. nginx) publishing the ports you need, joining the same network
docker run -d --name nginx --network my_network -p 9000:9000 nginx
Optionally remove the default.conf in nginx
docker exec nginx rm /etc/nginx/conf.d/default.conf
Create a new nginx config
Copy the config to nginx container.
docker cp ./my_conf.conf nginx:/etc/nginx/conf.d/my_conf.conf
Restart nginx
docker restart nginx
Advantages: To publish new ports, you can safely stop/update/recreate nginx container as you wish without touching the business container. If you need zero down time for nginx, it is possible to add more reversed proxy services joining the same network. Besides, a container can join more than one network.
Edit:
To reverse proxy non-http services, the config file is a bit different. Here is a simple example: