简体   繁体   中英

How do I expose port to host's ephemeral port and bind it to localhost

Docker compose quesion (any version is fine)

I need to expose port 60000 from a container to host's ephemeral (random) port and bind it to localhost only.

Syntax

ports: 
   - 60000

results in 0.0.0.0:32XXX port exposure.

Syntax - "127.0.0.1:YYYY:60000" requires me to set static host port (YYYY)

I know I can bind docker daemon to listen only on localhost, but that doesnt really work for me, as I need another containers to listen on 0.0.0.0

If the syntax you show doesn't work, then Docker Compose 3.7 can't do this (but it is probably a fairly straightforward GitHub pull request to add it). (Also note that this syntax is documented in the docker run documentation; it's just a parsing issue in Compose.)

The best workaround is probably going to be to pick a "public" port yourself. If you're trying to adapt a workflow built around docker run -p 127.0.0.1::60000 or even docker run -P that will be trickier.

The actual source code for Docker Compose is in the GitHub docker/compose repository. That includes a configuration schema for the docker-compose.yml file, and a ServicePort.parse method to handle the contents of ports: . It seems like the parse method for a string doesn't support a missing published port (I'm not sure why) and the schema doesn't allow an external_ip: option for the long syntax .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM