0

When using docker and docker-compose, ports are binded and reserved at the beginning of the execution process. Since some applications outside the container can compete (and potentially crash) for the same ports, it would be interesting a solution to dynamically bind / unbind port for running docker containers.

One existing solution is is using variables to define the binding ports in docker-compose, but once started the container can not have any port binded or unbinded.

ports:
  # Service1 
  - 14443:${PORTSERVICE1}
  # Service2
  - 13306:{PORTSERVICE2}

Is there any way to change ports binding dynamically after container initialization?

Rafael Borja
  • 3,458
  • 5
  • 20
  • 30
  • Not without editing Docker internals. If you're just worried about port conflicts, specify only a single port number in `ports:` and Docker will pick a non-conflicting host port; [`docker-compose port`](https://docs.docker.com/compose/reference/port/) can tell you what it is. – David Maze Feb 13 '21 at 00:39
  • I need fixed ports to expose services to the host. But sometimes I need to disable those services and remove the port binding. – Rafael Borja Feb 16 '21 at 23:23

0 Answers0