Docker 组合端口映射 [英] Docker compose port mapping
问题描述
我有一个 docker-compose yml 文件,如下所示
I have a docker-compose yml file as in below
version: '2'
services:
nodejs:
build:
context: .
dockerfile: DockerFile
ports:
- "4000:4000"
links:
- redis
expose:
- "6379"
redis:
build:
context: .
dockerfile: Dockerfile-redis
我的目标是将 nodejs-127.0.0.1 端口 6379 转发到 redis 主机.我已经可以从 nodejs 机器 ping redis,但是端口没有映射.尝试公开选项,但也没有机会.
My goal is to forward nodejs-127.0.0.1 port 6379 to the redis host. I can already ping redis from the nodejs machine, but the ports are not mapped. Tried expose options, but no chance either.
推荐答案
如果您想从 nodejs
容器绑定到 redis 端口,则必须在 redis 中公开该端口
容器:
If you want to bind to the redis port from your nodejs
container you will have to expose that port in the redis
container:
version: '2'
services:
nodejs:
build:
context: .
dockerfile: DockerFile
ports:
- "4000:4000"
links:
- redis
redis:
build:
context: .
dockerfile: Dockerfile-redis
expose:
- "6379"
expose
标签可以让你暴露端口而不将它们发布到主机,但它们会暴露给容器网络.
The expose
tag will let you expose ports without publishing them to the host machine, but they will be exposed to the containers networks.
https://docs.docker.com/compose/compose-file/#暴露
ports
标签将映射主机端口和容器端口 HOST:CONTAINER
The ports
tag will be mapping the host port with the container port HOST:CONTAINER
https://docs.docker.com/compose/compose-file/#端口
这篇关于Docker 组合端口映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!