Skip to content

Latest commit

 

History

History
65 lines (43 loc) · 1.41 KB

reallocation-of-port.md

File metadata and controls

65 lines (43 loc) · 1.41 KB

Method 1: Stop and Re-run the Container with New Ports

  1. Stop the Running Container:

    docker stop <container_name_or_id>
  2. Remove the Container:

    docker rm <container_name_or_id>
  3. Run the Container with New Port Mappings:

    docker run -d -p <new_host_port>:<container_port> --name <container_name> <image_name>

Method 2: Use Docker Compose

If you are using Docker Compose, you can modify the docker-compose.yml file to change the ports and then apply the changes.

  1. Edit docker-compose.yml:

    Change the port mapping under the service definition.

    services:
      your_service_name:
        ports:
          - "<new_host_port>:<container_port>"
  2. Apply the Changes:

    docker-compose down
    docker-compose up -d

Method 3: Using docker network and docker run --network host

  1. Stop the Running Container:

    docker stop <container_name_or_id>
  2. Remove the Container:

    docker rm <container_name_or_id>
  3. 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.