Docker compose, running containers in net:host
Asked Answered
M

4

140

I want to spawn 3 services in the "host" net using docker-compose. Here is my docker-compose.yml file:

version: '2'
services:
  mysql:
    image: mysql
    net: "host"
  nginx:
    image: nginx
    net: "host"
  app:
    image: tomcat
    net: "host"

I got the following error:

$ docker-compose up
[31mERROR[0m: Validation failed in file '.\docker-compose.yml', reason(s):
Unsupported config option for services.app: 'net'
Unsupported config option for services.mysql: 'net'
Unsupported config option for services.nginx: 'net'

I'm using boot2docker on windows.

Docker, and Docker-compose version:

$ docker -v
Docker version 1.10.2, build c3959b1
$ docker-compose -version
docker-compose version 1.6.0, build cdb920a

If I run all services manually by using docker run --net = "host" everything is working fine.

In the documentation I read that net command is supported in docker-compose:

net

Networking mode. Use the same values as the docker client --net parameter.

net: "bridge"

net: "none"

net: "container:[name or id]"

net: "host"

https://docs.docker.com/v1.6/compose/yml/#net

What am I doing wrong?

Mathes answered 12/3, 2016 at 16:45 Comment(1)
Update based on @andy-shinn's answer, the parameter is called network_mode: "host". As documented in docker compose version 3 #network_mode.Gentlemanatarms
A
137

Those documents are outdated. I'm guessing the 1.6 in the URL is for Docker 1.6, not Compose 1.6. Check out the correct syntax here: https://docs.docker.com/compose/compose-file/compose-file-v3/#network_mode. You are looking for network_mode when using the v2/v3 YAML format.

Atheistic answered 12/3, 2016 at 16:51 Comment(4)
That's really confusing... I'll bookmark the documentation link right away. network_mode is working fine thanks!Mathes
As I understood, I shoud use network_mode: "host" under service?Eyeopener
note that network_mode: host won't work on docker for MacIngeingeberg
@FlavienVolken on mac, one solution is to use the host IP explicitly. There is some special hostname you can use which points to this (docker_host_domain or something like that, i forget).Ticking
T
95

Just print

network_mode: "host"

Terpene answered 31/5, 2017 at 9:27 Comment(1)
'network_mode' and 'networks' cannot be combined. In case you already have networks - it won't work.Felixfeliza
F
68

you can try just add

network_mode: "host"

example :

version: '2'
services:
  feedx:
    build: web
    ports:
    - "127.0.0.1:8000:8000"
    network_mode: "host"

list option available

network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"

https://docs.docker.com/compose/compose-file/#network_mode

Foolish answered 27/5, 2019 at 7:5 Comment(2)
is the port forwarding ignored in this case?Underarm
docker-compose up will fail with a message: docker.errors.InvalidArgument: "host" network_mode is incompatible with port_bindings. It is notable that running docker-compose config does not catch this as of version 3.8.Taggart
P
6

Maybe I am answering very late. But I was also having a problem configuring host network in docker compose. Then I read the documentation thoroughly and made the changes and it worked. Please note this configuration is for docker-compose version "3.7". Here einwohner_net and elk_net_net are my user-defined networks required for my application. I am using host net to get some system metrics.

Link To Documentation https://docs.docker.com/compose/compose-file/#host-or-none

version: '3.7'
services:
  app:
    image: ramansharma/einwohnertomcat:v0.0.1
    deploy:
      replicas: 1
      ports:
       - '8080:8080'
    volumes:
     - type: bind
       source: /proc
       target: /hostfs/proc
       read_only: true
     - type: bind
       source: /sys/fs/cgroup
       target: /hostfs/sys/fs/cgroup
       read_only: true
     - type: bind
       source: /
       target: /hostfs
       read_only: true
    networks:
     hostnet: {}
    networks:
     - einwohner_net
     - elk_elk_net
networks:
 einwohner_net:
 elk_elk_net:
   external: true
 hostnet:
   external: true
   name: host
Prosecute answered 9/4, 2019 at 20:49 Comment(1)
> Only used if you use docker stack commands. If you use the docker-compose command, use network_mode instead.Democratic

© 2022 - 2024 — McMap. All rights reserved.