Docker网络详解之Host

Docker中的host模式指定是容器与主机享受相同的network namespace,在这种情况下,我们访问主机端口就能访问我们的容器。比如说我们运行tomcat容器并且用
-- network=host 来指定我们的网络模式为host,这样我们访问本机的8080端口就能访问到我们的tomcat容器。下面这段是官网对于host模式的定义:

If you use the host network driver for a container, that container’s network stack is not isolated 
from the Docker host. For instance, if you run a container which binds to port 80 and you use 
host networking, the container’s application will be available on port 80 on the host’s IP address.

The host networking driver only works on Linux hosts, and is not supported on Docker for Mac, 
Docker for Windows, or Docker EE for Windows Server.

In Docker 17.06 and higher, you can also use a host network for a swarm service, by passing --
network host to the docker container create command. In this case, control traffic (traffic related 
to managing the swarm and the service) is still sent across an overlay network, but the individual 
swarm service containers send data using the Docker daemon’s host network and ports. This 
creates some extra limitations. For instance, if a service container binds to port 80, only one 
service container can run on a given swarm node.

If your container or service publishes no ports, host networking has no effect.

这里面说明了host模式只能在linux下运行,其实我们想想也就知道,docker的网络模式通过linux下的network namespace来实现的。我们也能够在Docker Swarm中使用host模式来创建我们的container,但是这种情况下我们运行在一个端口的容器只能使用一个。如果我们的容器或者服务没有监听任何端口,那么我们的host 模式将不会生效。

对于host模式的思考:
1.host模式下是怎么占领端口的?
host模式端口占用模式是你的容器占用你主机上当前所监听的端口(官网描述为publish),比如我们都知道tomcat占用8080端口,那么当我们用host模式启动的时候,主机上的8080端口会被tomcat占用,这个时候其他的容器就不能指定我们的8080端口了,但是可以指定其他端口,所以说一台主机上可以运行多个host模式的容器,只要彼此监听的端口不一样就行。

2.host模式下使用-p或者-P会出现WARNING: Published ports are discarded when using host network mode
当你是host模式的时候,主机会自动把他上面的端口分配给容器,这个时候使用-p或者-P是无用的。但是还是可以在Dockerfile中声明EXPOSE端口,关于EXPOSE命令的详细解释接下来我会继续写篇文章。

3.host模式设计的原因
host模式设计出来就是为了性能,访问主机的端口就能访问到我们的容器,使容器直接暴露在公网下,但是这却对docker的隔离性造成了破坏,使得安全性大大降低。这种模式有利有弊,对于每个人来说看法都不一样,具体取舍看个人。

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Jellyfin Docker是一种在Docker容器中运行的媒体服务器应用程序。要设置Jellyfin Docker网络设置为host模式,首先需要确保已正确安装和配置Dockerhost模式是Docker的一种网络设置,它允许容器与主机共享网络栈和IP地址。这意味着容器使用主机网络接口,并且可以直接访问主机上的网络资源。 要将Jellyfin Docker设置为host模式,需要创建一个Docker Compose文件或使用docker命令行工具。以下是一个Docker Compose示例文件: ```yaml version: '3' services: jellyfin: image: jellyfin/jellyfin network_mode: host ports: - 8096:8096 volumes: - /path/to/media:/media ``` 在上面的示例中,`network_mode: host`指定了使用host模式。`ports`部分将容器的8096端口映射到主机的8096端口,以便可以通过主机访问Jellyfin。`volumes`将主机上的媒体文件夹挂载到容器中。 保存上述内容为`docker-compose.yml`文件,然后使用以下命令启动Jellyfin容器: ``` docker-compose up -d ``` 这将使用host模式运行Jellyfin Docker,并通过8096端口提供媒体服务器。您可以通过访问`http://localhost:8096`或服务器的IP地址访问Jellyfin Web界面。 请注意,host模式会直接访问主机网络,因此需要确保主机上没有其他服务使用相同的端口。此外,host模式容器将无法与其他容器直接通信,因为它们不在同一网络中。 希望以上回答能够帮助您设置Jellyfin Docker网络设置为host模式。如有更多疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值