6

I am trying to run a container where I need to user network driver as "host" instead of "bridge". I am running it on Centos machine and my docker-compose.yml is

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      

But i am getting the following error when I fire "docker-compose up" :

ERROR: only one instance of "host" network is allowed

Can anyone please suggest how can I use host network using docker-compose.yml.

Also note that if I use network_host as suggested by @larsks, I am still getting error

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

I am getting following error

ERROR: The Compose file './docker-compose.yml' is invalid because: Unsupported config option for services: 'testContainer'

sanjeev kumar
  • 373
  • 1
  • 6
  • 21

1 Answers1

12

Get rid of the networks section in your docker-compose.yml, and add a network_mode directive to your service definition:

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
larsks
  • 277,717
  • 41
  • 399
  • 399
  • @Iarsks It throws this error when I use "network_mode: host" ---> ERROR: The Compose file './docker-compose.yml' is invalid because: Unsupported config option for services: 'testContainer' – sanjeev kumar Sep 08 '20 at 05:24
  • It looks like you're working with Docker Swarm, which has different behavior. Your question has been linked to a duplicate that has the answer you need. – larsks Sep 08 '20 at 11:02