gpt4 book ai didi

docker - 如何使用 docker-compose 在 docker/container 之外公开容器端口?

转载 作者:行者123 更新时间:2023-12-02 03:09:16 25 4
gpt4 key购买 nike

我有一个容器,它有多个端口,我想远程访问此 docker 之外的其中一个端口 (9001)。

  • 我的 Docker IP 是:172.17.0.1
  • 我的容器 IP 是:172.19.0.23
  • 我的服务器IP是:192.168.1.131
<小时/>

我对此进行了搜索,发现了 expose port 关键字,我做到了,但没有成功。

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:9001docker_IP:9001 --> 172.17.0.1:9001 无法访问(在远程或本地模式下)
  • 但是当使用 container_IP:9001 --> 172.19.0.23:9001 时在本地工作。

我应该做什么才能访问 server_IP:9001 --> 192.168.1.131:9001

<小时/>

[注意]:

  • createDB.sh 中运行多个操作,例如在9001端口。

  • 我之前已经设置过允许的端口,使用$ ufwallow 9001

  • 我尝试过 Ubuntu 16.04 和 Ubuntu-Server 16.04
<小时/>

如有任何帮助,我们将不胜感激。

最佳答案

如果您想实际映射您应该使用的端口

ivms:
build: .
container_name: ivms
command: bash bashes/createDB.sh
volumes:
- .:/code
ports:
- "8010:8010"
- "9001:9001" # now you can access them locally

警告您对这两个服务 ivms 和 nginx 使用相同的端口

The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.

The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. -Docker Docs

关于docker - 如何使用 docker-compose 在 docker/container 之外公开容器端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51260058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com