使用主机网络部署 Docker 堆栈仍然无法正常工作

标签 docker docker-compose docker-swarm

我知道 docker issue #25873 中的一些讨论说 17.06 ce 支持此功能。我在 Linux 中安装了 17.09,在 Windows 中安装了 18.03。两者都有相同的问题提示
network "host" is declared as external, but it is not in the right scope: "local" instead of "swarm"
想知道为什么我不能使用本地范围的主机网络?有没有办法在运行 docker stack deploy 时使用主机网络?

Docker 版本

Client:
 Version:       18.03.0-ce
 API version:   1.37
 Go version:    go1.9.4
 Git commit:    0520e24
 Built: Wed Mar 21 23:06:28 2018
 OS/Arch:       windows/amd64
 Experimental:  false
 Orchestrator:  swarm

Server:
 Engine:
  Version:      18.03.0-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.4
  Git commit:   0520e24
  Built:        Wed Mar 21 23:14:32 2018
  OS/Arch:      linux/amd64
  Experimental: false

docker 信息
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 1
Server Version: 18.03.0-ce
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: active
 NodeID: cqa33vk220d3zpp9g2gqar9hd
 Is Manager: true
 ClusterID: cphphnyrr379dbm5044snxrjy
 Managers: 1
 Nodes: 1
 Orchestration:
  Task History Retention Limit: 5
 Raft:
  Snapshot Interval: 10000
  Number of Old Snapshots to Retain: 0
  Heartbeat Tick: 1
  Election Tick: 3
 Dispatcher:
  Heartbeat Period: 5 seconds
 CA Configuration:
  Expiry Duration: 3 months
  Force Rotate: 0
 Autolock Managers: false
 Root Rotation In Progress: false
 Node Address: 192.168.65.3
 Manager Addresses:
  192.168.65.3:2377
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: cfd04396dc68220d1cecbe686a6cc3aa5ce3667c
runc version: 4fc53a81fb7c994640722ac585fa9ca548971871
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 4.9.87-linuxkit-aufs
Operating System: Docker for Windows
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.934GiB
Name: linuxkit-00155d006407
ID: YRXM:MKGD:7VQM:ZNK2:XKCI:DYTL:VVQH:XP5X:QRCY:CKP2:5EVJ:3QE4
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
 File Descriptors: 33
 Goroutines: 145
 System Time: 2018-04-08T07:24:34.2713256Z
 EventsListeners: 1
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

Docker 撰写文件
version: '3.4'

services:
  test:
    image: alpine
    command: top
    networks:
      - host

networks:
  host:
    external:
      name: host

执行
C:\>docker stack deploy -c docker-compose.yml test

network "host" is declared as external, but it is not in the right scope: "local" instead of "swarm"

最佳答案

截至 11 月 Docker 18.09 版本,这应该在现代 Linux 上与 Windows Server 2019 相同。您可以将一项服务直接连接到主机端口以及覆盖网络。

要将主机 NIC 直接用于已发布的端口,请不要使用网络,只需像这样更改已发布的端口模式:

services:
  traefik:
    image: traefik:alpine
    ports:
      - target: 80
        published: 80
        protocol: tcp
        mode: host

对于正常发布的端口,它将是 mode: ingress
如果您还希望该服务与覆盖网络通信,您可以按照通常的方式添加网络。我有一个 full example of this for Traefik here .

关于使用主机网络部署 Docker 堆栈仍然无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49715508/

相关文章:

docker - 有没有办法创建 Docker 卷并用数据预填充它?

networking - 使用 Docker Swarm 和覆盖网络进行组播

maven - 在 pom.xml 中包含 vaadin-cdi 依赖项足以使 WAR 无法部署。为什么?

linux - 以非 root 用户身份启动容器与以 root 用户身份启动然后降级为非 root 用户

Docker-compose 无法运行从文件读取配置的 jar

postgresql - 在 docker-compose up 后加载 Postgres 转储

docker - 如何从 Docker compose 安装软件包?

docker - 来自守护进程 : rpc error: code = 2 desc = name conflicts with an existing object 的错误响应

Docker 群模式路由网格无法按预期工作

docker - Docker Windows容器CMD-命令未运行