-
Stop the Running Container:
docker stop <container_name_or_id>
-
Remove the Container:
docker rm <container_name_or_id>
-
Run the Container with New Port Mappings:
docker run -d -p <new_host_port>:<container_port> --name <container_name> <image_name>
If you are using Docker Compose, you can modify the docker-compose.yml
file to change the ports and then apply the changes.
-
Edit
docker-compose.yml
:Change the port mapping under the service definition.
services: your_service_name: ports: - "<new_host_port>:<container_port>"
-
Apply the Changes:
docker-compose down docker-compose up -d
-
Stop the Running Container:
docker stop <container_name_or_id>
-
Remove the Container:
docker rm <container_name_or_id>
-
Run the Container with Network Mode Host:
docker run -d --network host --name <container_name> <image_name>
This way, the container will use the host's network stack, and you can change the exposed ports without needing to update the container.
- Choose the method that best suits your needs and workflow.