14

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'

0

1 Answer 1

21

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
Sign up to request clarification or add additional context in comments.

3 Comments

@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'
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.
Hello, in my case I have to remove the section networks inside my service, in your example should be testContainer. Apparently the keywords networks and network_mode are mutual exclusive and won't live together on the same service.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.