简体   繁体   English

将随机端口暴露给 docker-compose.yml

[英]Expose random port to docker-compose.yml

I need multiple instance of same application, for that I am using server.port=0 to run application in random port.我需要同一个应用程序的多个实例,为此我使用server.port=0在随机端口中运行应用程序。 my question is how can I map randomly generated port to docker-compose.yml to create multiple instances.我的问题是如何将随机生成的端口映射到 docker-compose.yml 以创建多个实例。 I am using spring boot at the back-end.我在后端使用弹簧靴。 I am unable to find any solution.我找不到任何解决方案。 Any help much appreciated.非常感谢任何帮助。

Each Docker container runs a single process in an isolated network namespace, so this isn't necessary.每个 Docker 容器在隔离的网络命名空间中运行单个进程,因此这不是必需的。 Pick a fixed port.选择一个固定端口。 For HTTP services, common port numbers include 80, 3000, 8000, and 8080, depending on permissions and the language runtime (80 requires elevated privileges, 3000 is Node's default, and so on).对于 HTTP 服务,常见的端口号包括 80、3000、8000 和 8080,具体取决于权限和语言运行时(80 需要提升权限,3000 是 Node 的默认值,依此类推)。 The exact port number doesn't matter.确切的端口号无关紧要。

You access the port from outside Docker space using a published port.您可以使用已发布的端口从 Docker 空间外部访问该端口。 If you're running multiple containers, there is the potential for conflict if multiple services use the same host port, which is probably what you're trying to avoid.如果您正在运行多个容器,并且多个服务使用相同的主机端口,则可能会发生冲突,这可能是您试图避免的。 In the docker run -p option or the Docker Compose ports: setting, it's possible to list only the port running inside the container, and Docker will choose a host port for you.docker run -p选项或 Docker Compose ports:设置中,可以只列出容器内运行的端口,Docker 会为你选择一个主机端口。

version: "3"
services:
  web:
    image: ...
    ports:
      - "8000"                       # no explicit host port
    command: ... -Dserver.port=8000  # fixed container port

docker-compose port web 8000 will tell you what the host (public) port number is. docker-compose port web 8000会告诉你主机(公共)端口号是什么。 For communication between containers in the same docker-compose.yml file, you can use the service name and the (fixed, known) internal port, http://web:8000 .对于同一个docker-compose.yml文件中的容器之间的通信,您可以使用服务名称和(固定的、已知的)内部端口http://web:8000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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