docker swarm 网络内部容器访问外部物理机端口
时间: 2023-09-08 13:11:23 浏览: 57
在 Docker Swarm 中,你可以使用 `--publish` 或 `-p` 选项将一个容器的端口映射到主机上的端口。但是,如果你想从网络内部的容器访问主机上的端口,你需要使用主机的 IP 地址和端口号。
假设你有一个主机上运行着一个 web 服务器,并且它监听在 8080 端口上,你可以使用以下命令启动一个容器,并访问主机上的 web 服务器:
```
docker run --rm -it --network my-overlay-network busybox
```
上述命令中,我们启动了一个基于 busybox 镜像的容器,并将其加入到名为 my-overlay-network 的 overlay 网络中。
在容器内部,你可以使用以下命令测试是否可以访问主机上的 web 服务器:
```
wget http://<主机IP地址>:8080
```
注意将 `<主机IP地址>` 替换为你主机的 IP 地址。如果一切正常,该命令将下载主机上的 web 服务器的首页。
希望这能回答你的问题!
相关问题
docker swarm驱逐所有容器
您可以使用以下命令来驱逐所有Docker Swarm中的容器:
```
docker service rm $(docker service ls -q)
```
此命令将删除所有服务,其中包含所有容器。 如果您只想删除容器而不是服务,请使用以下命令:
```
docker container rm $(docker container ls -aq)
```
请注意,这将删除所有容器,无论它们是否在Docker Swarm中运行。
docker swarm集群的访问地址端口和api版本怎么查看
你可以通过以下命令查看Docker Swarm集群的访问地址和端口:
```
docker swarm init
```
该命令将输出一个类似于以下内容的信息:
```
Swarm initialized: current node (abc123) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-xxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 192.168.0.1:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
```
其中,`192.168.0.1:2377` 就是 Swarm 集群的访问地址和端口。
另外,你可以通过以下命令查看 Docker API 的版本:
```
docker version
```
在命令输出的信息中,你可以找到 `API version` 字段,该字段的值就是当前 Docker API 的版本号。