为了学习目的,我正在制作一个ETL应用程序示例。我希望有两个容器:一个MySQL容器和一个与Redis一起运行的Python容器来提供数据。
我想打开这些集装箱上的港口,但出于明显的安全原因,我不想向互联网开放它们。
如果我在这些容器上打开端口,它们是只对主机开放,还是主机也必须打开这些端口?
发布于 2022-01-11 14:52:03
如果您没有任何Compose ports:或docker run -p选项,容器将能够彼此通信,但无法从主机或离开主机到达。示例组合设置:
version: '3.8'
services:
app:
build: .
ports: ['8080:8080'] # the application itself is visible
environment:
PGHOST: postgres # using normal Docker inter-container communication
REDIS_HOST: redis
postgres:
image: postgres:13
# no ports:
redis:
image: redis:6
# no ports:还可以使用可选绑定地址设置ports:。如果该地址为127.0.0.1,则可以从主机系统上的非容器进程访问已发布的端口。其他主机将无法连接到它,容器也无法使用主机网关地址或host.docker.internal连接到它。
services:
postgres:
# from non-container processes on the host,
# PGHOST=localhost PGPORT=54321 reaches this container
#
# from other containers in this Compose file,
# PGHOST=postgres PGPORT=5432 still works
ports: ['127.0.0.1:54321:5432']https://stackoverflow.com/questions/70668677
复制相似问题