如何使用docker-compose在docker/container外部公开容器端口? [英] How to expose a container port outside of docker/container using docker-compose?
问题描述
我有一个具有多个端口的容器,我想以远程方式访问此Docker外部的端口之一(9001
).
I have a container that it has several ports, I want to have access to one of its ports (9001
) outside of this docker as remote.
- 我的docker IP是:
172.17.0.1
- 我的容器IP是:
172.19.0.23
- 我的服务器IP为:
192.168.1.131
- My docker IP is:
172.17.0.1
- My container IP is:
172.19.0.23
- My server IP is:
192.168.1.131
我已经搜索过此内容,却找到了expose port
关键字,但确实做到了,但是没有用.
I have searched about that and I found expose port
keyword, and I did it but not worked.
如何公开docker端口以使您的容器可外部访问
参考
How to expose docker ports to make your containers externally accessible
Reference
这是我的docker-compose文件:
version: '3'
services:
nginx:
image: nginx:latest
container_name: nginx
ports:
- "8010:8010"
volumes:
- .:/code
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
links:
- ivms
restart: unless-stopped
ivms:
build: .
container_name: ivms
command: bash bashes/createDB.sh
volumes:
- .:/code
expose:
- "8010"
- "9001" # exposed disired port
ports:
- "9001:9001"
我在docker-compose文件上方运行:$ docker-compose up -d
- 但是当我使用
server_IP:9001 --> 192.168.1.131:9001
或docker_IP:9001 --> 172.17.0.1:9001
无法访问它(在远程或本地模式下) - 但是使用
container_IP:9001 --> 172.19.0.23:9001
时, 在本地工作.
- But when I using
server_IP:9001 --> 192.168.1.131:9001
ordocker_IP:9001 --> 172.17.0.1:9001
can not access to that (in remote or local mode) - But when using
container_IP:9001 --> 172.19.0.23:9001
this works in local.
我应该怎么做才能访问 server_IP:9001 --> 192.168.1.131:9001
?
What should I do that I can access to server_IP:9001 --> 192.168.1.131:9001
?
[注意]:
-
在
-
运行若干操作,例如在
上创建 ZMQ. 9001
端口.
createDB.sh
中的In
createDB.sh
runs several operations such as creating a ZMQ on9001
port.
我已经使用$ ufw allow 9001
任何帮助将不胜感激.
推荐答案
问题已通过以下说明解决:
Problem resolved with below instruction:
在ZMQ应用程序(在ivms
容器中)中,我曾使用服务器IP来绑定连接,如下所示:
In ZMQ app (in ivms
container) I had used from the server IP to binding a connection as follow:
import zmq
if __name__ == '__main__':
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, "")
socket.bind("tcp://192.168.1.131:9001") # doesn't work with server or docker IP
while True:
data = socket.recv_json()
它只能按以下方式工作:
It was working only as below:
socket.bind("tcp://192.168.1.131:9001") # works, but can't access as remote
现在我按如下方式编辑此行:
socket.bind("tcp://*:9001") # Works both locally and remotely.
这是我的docker-compose.yml配置:
And this is my docker-compose.yml configuration:
version: '3'
services:
nginx:
image: nginx:latest
container_name: nginx
ports:
- "8010:8010"
volumes:
- .:/code
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
links:
- ivms
restart: unless-stopped
ivms:
build: .
container_name: ivms
command: bash bashes/createDB.sh
volumes:
- .:/code
expose:
- "8010"
ports:
- "9001:9001"
这篇关于如何使用docker-compose在docker/container外部公开容器端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!