[英]docker-compose expose same port as the service
I have a docker-compose.yml:我有一个 docker-compose.yml:
services:
backend:
build:
./backend
ports:
- 8100:8100
container_name: "backend"
frontend:
build:
./frontend
ports:
- 4200:4200
container_name: "frontend"
depends_on:
- backend
And i want to get rid of the ports part.我想摆脱端口部分。 I have.env files in the folders /backend and /frontend with the portnumber set in there (eg PORT=8100).
我在文件夹/backend 和/frontend 中有.env 文件,其中设置了端口号(例如PORT=8100)。 In the dockerfile i can just do Export ${PORT}.
在 dockerfile 中,我可以执行导出 ${PORT}。 But since i cant read the.env from the docker-compose location i am not able to expose the port in the same way.
但由于我无法从 docker-compose 位置读取.env,我无法以相同的方式公开端口。 Is it possible to just have a wildcard to expose the port of the containers to the same port on my host like:
是否可以只使用通配符将容器的端口公开到我主机上的同一端口,例如:
ports:
- *:*
No, there's no syntax like this.不,没有这样的语法。 The
ports:
syntax always requires you to specify the container-side port, and you must have a ports:
block if you want the container to be accessible from outside Docker. ports:
语法总是要求你指定容器端的端口,如果你希望容器可以从 Docker 外部访问,你必须有一个ports:
块。
If you weren't using Compose there is a docker run -P
(capital P
) option, but there the container ports are published on randomly-selected host ports.如果您没有使用 Compose,则有一个
docker run -P
(大写P
)选项,但容器端口在随机选择的主机端口上发布。 (This is one of the few contexts where "expose" as a Docker verb does anything useful: docker run -P
publishes all exposed ports.) (这是“暴露”为 Docker 动词可以做任何有用的少数情况之一:
docker run -P
发布所有暴露的端口。)
However: there is no rule that the two port numbers must match.但是:没有规定两个端口号必须匹配。 Rather than having the port number configurable in the Dockerfile (requiring a rebuild on any change) it's more common to use a fixed port number in the image and allow the host port number to be configured at deploy time.
与其在 Dockerfile 中配置端口号(需要对任何更改进行重建),更常见的是在映像中使用固定端口号并允许在部署时配置主机端口号。
For example, assume both images are configured to use the default Express port 3000. You can remap these when you run the containers:例如,假设两个镜像都配置为使用默认的 Express 端口 3000。您可以在运行容器时重新映射它们:
version: '3.8'
services:
backend:
build: ./backend
ports: ['8100:3000']
frontend:
build: ./frontend
depends_on: [backend]
ports: ['4200:3000']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.