I'm am deploying an app inside a docker container that randomly assigns ports when it starts.
The problem is I want to use docker-compose
but is there a way to expose all ports for the service using docker-compose? Without docker-compose, I would use docker run ... -P
Thanks
Docker Compose exposes all specified container ports, making them reachable internally and externally from the local machine. Once again, in the PORTS column, we can find all the exposed ports. The value to the left of the arrow shows the host address where we can reach the container externally.
Short answer: EXPOSE is a way of documenting. --publish (or -p ) is a way of mapping a host port to a running container port.
Just publish the ssh service on different host ports. E.g., start one container with -p 2221:22 and another with -p 2222:22 . Now you have two ssh containers running, one reachable on host port 2221 and another on host port 2222 . Save this answer.
I'd recommend using port ranges i.e
ports:
- "1-65535:1-65535"
You will probably need to tinker with this range according to your app specifications so that you won't accidentally expose something that's already in use by the host (such as SSH).
docker-compose version 1.25.0
Use it like following
ports: [22, 23]
It needs to be array of port numbers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With