English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to Get Docker Dynamically Allocated Port

I. Reason

When we need to use docker to start a service in a cluster environment, if we always specify a fixed public port for the docker service, it will greatly limit the flexibility and maintainability of service deployment. So, is there a way to easily obtain the dynamically allocated port of docker after the service starts?

II. Solution

Docker provides the port subcommand, the specific usage is as follows:

docker port <container-name> |cut -d:' -f2

III. Subsequent Issues

Currently, this method is only suitable for the scenario of service registration between external services.

If the service registration is completed in the service program (such as reporting to zookeeper, etc.), then there is currently no way. There is an issue3778In the follow-up, I don't know when docker will provide

Summary

That's all for this article. I hope the content of this article can be helpful to everyone's learning or work. If you have any questions, you can leave a message for communication.

You May Also Like