Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose open all ports

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

like image 735
Adetiloye Philip Kehinde Avatar asked Nov 12 '16 16:11

Adetiloye Philip Kehinde


People also ask

Does Docker compose expose ports?

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.

What is difference between expose and publishing?

Short answer: EXPOSE is a way of documenting. --publish (or -p ) is a way of mapping a host port to a running container port.

How do you run multiple containers of the same image and expose different ports?

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.


2 Answers

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).

like image 144
amirb Avatar answered Oct 06 '22 10:10

amirb


docker-compose version 1.25.0


Use it like following


ports: [22, 23]

It needs to be array of port numbers.

like image 45
Kumar Avatar answered Oct 06 '22 10:10

Kumar