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
容器中公开该端口:
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/#expose
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/#ports
这篇关于Docker撰写端口映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!