[英]Get exposed port from within docker-compose
I have a docker-compose.yml file as follows:我有一个 docker-compose.yml 文件如下:
version: "3.9"
services:
server:
build: .
ports:
- "8086"
environment:
- PORT=8086
a dockerfile: dockerfile:
FROM python:3.8
RUN python -m pip install --upgrade pip
RUN pip install pipenv
COPY Pipfile* /app/
RUN cd /app && pipenv lock --keep-outdated --requirements > requirements.txt
RUN pip install -r /app/requirements.txt
COPY . /app/src
WORKDIR /app/src
EXPOSE ${PORT}
CMD uwsgi --http :${PORT} --wsgi-file server.py --callable main
So basically, I am running a server on a given port, which I expose in my Dockerfile and docker-compose.所以基本上,我在给定端口上运行服务器,我在 Dockerfile 和 docker-compose 中公开了它。 However, when I run two instances of these containers, I need a way for them to each use their own port.
但是,当我运行这些容器的两个实例时,我需要一种方法让它们各自使用自己的端口。 I also cannot hardcode values as I need to be able to scale to n instances.
我也不能硬编码值,因为我需要能够扩展到 n 个实例。 So rather than using the environment variable
PORT
, I need to somehow get each port from within the running container, expose it, and run the server on that port.因此,我需要以某种方式从正在运行的容器中获取每个端口,将其公开,然后在该端口上运行服务器,而不是使用环境变量
PORT
。
When I call docker-compose up --scale server=2
to run two server containers, I am unable to send HTTP requests because the port mappings do not match up.当我调用
docker-compose up --scale server=2
来运行两个服务器容器时,我无法发送 HTTP 请求,因为端口映射不匹配。
Essentially what I am trying to do is have each container run uwsgi using the port allocated to it by docker-compose.基本上我要做的是让每个容器使用 docker-compose 分配给它的端口运行 uwsgi。 Is there any way to achieve this in a nice manner?
有没有办法以一种很好的方式实现这一目标?
Source: https://github.com/distribworks/dkron来源: https://github.com/distribworks/dkron
You're missing the HOST_PORT
part in your server
service definition.您在
server
服务定义中缺少HOST_PORT
部分。 A service's port definition should be HOST_PORT:CONTAINER_PORT
as per https://docs.docker.com/compose/networking/ .根据https://docs.docker.com/compose/networking/ ,服务的端口定义应该是
HOST_PORT:CONTAINER_PORT
。 Otherwise a random HOST_PORT
will be chosen.否则将选择一个随机的
HOST_PORT
。
You can clone a working sample environment here or see it working here .您可以在此处克隆一个工作示例环境或在此处查看它的工作情况。
You can always see which port Docker Compose is choosing by issuing:您始终可以通过发出以下命令查看 Docker Compose 正在选择哪个端口:
jdsalaro$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
de0120c61c1e 65896179-get-exposed-port-from-within-docker-compose_server "/bin/sh -c 'uwsgi -…" 12 seconds ago Up 11 seconds 0.0.0.0:49157->8086/tcp 65896179-get-exposed-port-from-within-docker-compose_server_1
To fix your problem, just provide a HOST_PORT
as mentioned:要解决您的问题,只需提供提到的
HOST_PORT
:
version: "3.9"
services:
server:
build: .
ports:
- "8086:8086"
environment:
- PORT=8086
When using --scale
, for example --scale server=4
, one can provide a range of ports as HOST_PORT
instead as follows:使用
--scale
时,例如--scale server=4
,可以提供一系列端口作为HOST_PORT
,如下所示:
version: "3.9"
services:
server:
build: .
ports:
- "8000-8004:8086"
environment:
- PORT=8086
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.