Docker Compose 中的主机本地网络
Host-local networks in Docker Compose
我有以下 docker-compose.yml,但需要建模 public/private 网络拆分,其中 Redis 实例必须只能由本地主机访问。
version: "2.2" # for compatibility, I can upgrade if needed
services:
nginx:
image: nginx
ports:
- 8080:80
redis:
image: redis
ports:
- 6379:6379
如果我需要将其限制为只能在 docker 网络内访问,这似乎很简单。考虑:
version: "2.2"
services:
nginx:
image: nginx
ports:
- 8080:80
networks:
- frontend
- backend
redis:
image: redis
ports:
- 6379:6379
networks:
- backend
networks:
frontend: {}
backend:
internal: true
然而,我们的本地 Web 开发人员需要能够在构建、运行 和本地调试时从他们的主机(docker 网络之外)访问该 Redis 实例。
- 运行 Redis Web UI 调用了 redis-commander。
- 使用环境变量指向 运行 redis。
- 公开这个新容器并访问它而不是公开 Redis 容器。
services:
redis:
# Do comment ports! no need to expose it
# ports:
# - 6379:6379
// ....
redis-commander:
image: rediscommander/redis-commander:latest
restart: unless-stopped
environment:
REDIS_HOST: redis:6379 # <-- Here you point to your redis
# REDIS_PASSWORD # <- in case redis is protected with password
ports:
- "8081:8081"
让您的开发人员访问 http://localhost:8081 并尽情享受吧。
只需要将redis的服务端口绑定到localhost(127.0.0.1)即可。
尝试以下...
...
redis:
image: redis
ports:
- 127.0.0.1:6379:6379
...
我有以下 docker-compose.yml,但需要建模 public/private 网络拆分,其中 Redis 实例必须只能由本地主机访问。
version: "2.2" # for compatibility, I can upgrade if needed
services:
nginx:
image: nginx
ports:
- 8080:80
redis:
image: redis
ports:
- 6379:6379
如果我需要将其限制为只能在 docker 网络内访问,这似乎很简单。考虑:
version: "2.2"
services:
nginx:
image: nginx
ports:
- 8080:80
networks:
- frontend
- backend
redis:
image: redis
ports:
- 6379:6379
networks:
- backend
networks:
frontend: {}
backend:
internal: true
然而,我们的本地 Web 开发人员需要能够在构建、运行 和本地调试时从他们的主机(docker 网络之外)访问该 Redis 实例。
- 运行 Redis Web UI 调用了 redis-commander。
- 使用环境变量指向 运行 redis。
- 公开这个新容器并访问它而不是公开 Redis 容器。
services:
redis:
# Do comment ports! no need to expose it
# ports:
# - 6379:6379
// ....
redis-commander:
image: rediscommander/redis-commander:latest
restart: unless-stopped
environment:
REDIS_HOST: redis:6379 # <-- Here you point to your redis
# REDIS_PASSWORD # <- in case redis is protected with password
ports:
- "8081:8081"
让您的开发人员访问 http://localhost:8081 并尽情享受吧。
只需要将redis的服务端口绑定到localhost(127.0.0.1)即可。 尝试以下...
...
redis:
image: redis
ports:
- 127.0.0.1:6379:6379
...