Docker 错误:仅允许一个“主机”网络实例

san*_*mar 7 docker centos7 docker-compose docker-swarm

我正在尝试运行一个容器,其中我需要将网络驱动程序用作“主机”而不是“桥接器”。我在 Centos 机器上运行它,我的 docker-compose.yml 是

version: '3.4'

services:
  testContainer:
    build:
      context: .
      args: 
        HADOOP_VERSION: 2.6.0
        HIVE_VERSION: 1.1.0
    image: testcontainer
    container_name: testcontainer
    hostname: testcontainer
    ports:
      - 9200:9200
      - 9300:9300
      - 5601:5601
      - 9001:9001
    ulimits:
      memlock:
        soft: -1
        hard: -1  
    networks:
      - elknet  

networks:
  elknet:
    driver: host      
Run Code Online (Sandbox Code Playgroud)

但是当我触发“ docker-compose up ”时出现以下错误:

错误:仅允许一个“主机”网络实例

任何人都可以建议我如何使用 docker-compose.yml 使用主机网络。

另请注意,如果我按照@larsks的建议使用network_host,我仍然收到错误

version: '3.4'

services:
  testContainer:
    build:
      context: .
      args: 
        HADOOP_VERSION: 2.6.0
        HIVE_VERSION: 1.1.0
    image: testcontainer
    container_name: testcontainer
    hostname: testcontainer
    ports:
      - 9200:9200
      - 9300:9300
      - 5601:5601
      - 9001:9001
    ulimits:
      memlock:
        soft: -1
        hard: -1  
    network_mode: host
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

错误:撰写文件“./docker-compose.yml”无效,因为:服务的配置选项不受支持:“testContainer”

lar*_*sks 15

删除 中networks的部分docker-compose.yml,并将network_mode指令添加到您的服务定义中:

services:
  testContainer:
    build:
      context: .
      args: 
        HADOOP_VERSION: 2.6.0
        HIVE_VERSION: 1.1.0
    image: testcontainer
    container_name: testcontainer
    hostname: testcontainer
    ports:
      - 9200:9200
      - 9300:9300
      - 5601:5601
      - 9001:9001
    ulimits:
      memlock:
        soft: -1
        hard: -1  
    network_mode: host
Run Code Online (Sandbox Code Playgroud)