142

I want to use docker compose with the host network.

I have a docker container that access a local REST api. Usually I run

docker run --net=host -p 18080:8080 -t -i containera

which can access the host REST api which runs at http://127.0.0.1:8080. Since I want to scale the container containera I found docker compose to scale the container. But the docker compose file from the documentation does not work. The docker container does not query the REST API.

I tried the following compose file but the property

version: "3"
services:
  web:
    image: conatinera:latest
    network_mode: "host"
    deploy:
      replicas: 1
      resources:
        limits:
          cpus: "0.5"
          memory: 4G 
      restart_policy:
        condition: on-failure
    ports:
      - "18080:8080"

but the property network_mode is ignored/not allowed. with the message

Ignoring unsupported options: network_mode
A.Dumas
  • 2,619
  • 3
  • 28
  • 51
  • 1
    Are you using docker swarm? How are you deploying? `docker-compose up` or `docker stack deploy`? Depending on this your current compose file can have several mistakes on it. – codestation Jun 13 '19 at 17:57
  • Currently I use the swarm. but when I use docker-compose up also returns `services.web.build contains unsupported option: 'network'` – A.Dumas Jun 13 '19 at 18:10
  • 1
    Swarm and compose deployments are very different and your yml file is mostly invalid in both modes because you are mixing options from both modes. Please indicate what deployment method are you intending to use so you can get a proper answer (and add the docker-swarm tag and remove the docker-compose one depending what answer do you want) – codestation Jun 13 '19 at 18:17
  • Thank you for the clarification. I want to use compose deployment and will add it to the question – A.Dumas Jun 13 '19 at 18:21

8 Answers8

129

The equivalent configuration for docker-compose v3 is using the network_mode key: https://docs.docker.com/compose/compose-file/compose-file-v3/#network_mode

You should set network_mode to "host" in your docker-compose.yml.

If using docker swarm, see codestation's answer.

ford
  • 10,687
  • 3
  • 47
  • 54
  • How can I use this network mode for version 3 in a swarm? or how can I add the network mode since I get a unsupported compose file version different from 3 – A.Dumas Jun 13 '19 at 14:33
  • 2
    `network_mode` is used in v3 and v2. v1 called it `net`: https://docs.docker.com/compose/compose-file/compose-file-v1/#net – ford Jun 13 '19 at 14:34
  • I added `net` to my compose file but the net property is ignored – A.Dumas Jun 13 '19 at 14:45
  • 1
    With the compose file in your updated question, you have `version: "3"`, so you should be using `network_mode`. – ford Jun 13 '19 at 15:20
  • 1
    tried both but the `network_mode` still get the ignore – A.Dumas Jun 13 '19 at 15:26
  • When you set `network_mode` `host` you should get rid of the `networks`: https://stackoverflow.com/questions/63777655/docker-error-only-one-instance-of-host-network-is-allowed – user3379167 Sep 23 '22 at 10:31
57

You are mixing options that are invalid on either compose and swarm deployments.

If you are deploying with docker-compose up then your compose file should be like this:

version: "3"
services:
  web:
    image: conatinera:latest
    network_mode: "host"        
    restart: on-failure

Te options deploy is ignored on compose mode and the ports option is ignored when using host mode networking. I recommend to don't use host mode networking and use a reverse proxy in another container to balance your scaled containers.


(Feel free to ignore this part of the answer as you clarified that you aren't using swarm deployments).

If you are using swarm deployment then your compose file should be like this:

version: "3.4"
services:
  web:
    image: conatinera:latest
    deploy:
      replicas: 1
      resources:
        limits:
          cpus: "0.5"
          memory: 4G 
      restart_policy:
        condition: on-failure
    networks:
      - host

networks:
  host:
    name: host
    external: true

Again, published ports and host mode networking do not mix. Also is probably that your scaling will fail because all the containers will try to bind to the same port. I recommend to don't use host mode networking and let docker load balance your replicas.

codestation
  • 2,938
  • 1
  • 22
  • 22
19

I was facing the same problem. I found that when network_mode is set to host, port mapping doesn't work as the container will look for the port of the host. So, removing the port mapping worked for me like the following.

services:
  web-abc:
    build: ./abc
    # ports:
    #   - "7000:7000"
    volumes:
      - .:/code
    network_mode: host
Sadman Amin
  • 429
  • 4
  • 7
11

In docker compose this worked perfectly

extra_hosts:
      - "host.docker.internal:host-gateway"

here is an explanation What is the equivalent of --add-host=host.docker.internal:host-gateway in a Compose file

Victor Oduor
  • 111
  • 1
  • 3
  • 1
    If host REST api considers a client's request URL it will be like **http(s)://host.docker.internal/restservice** in your case, so if it is needed to get some specific URL feel free to use any alias instead of **host.docker.internal**, for example: `specific-url.local:host-gateway` – Maksym Kosenko Nov 16 '22 at 11:23
  • Inside the container on a MacOS you may need to use docker.for.mac.host.internal instead of host.docker.internal – Eugene Jul 07 '23 at 22:05
7

i think you should define the docker-compose file like this: This is just an example, please read the docuementation: https://docs.docker.com/compose/compose-file/#network-configuration-reference

version: "3"
services:
  web:
    image: conatinera:latest
    networks:
      mynetwork: {}
    deploy:
      replicas: 1
      resources:
        limits:
          cpus: "0.5"
          memory: 4G 
      restart_policy:
        condition: on-failure
    ports:
      - "18080:8080"
networks:
  mynetwork:
    external: true
    name: host
Gianmarco Carrieri
  • 771
  • 2
  • 9
  • 20
6

Which platform you are on? host mode is working in Linux only AFAIK. If network_mode is not working try network: host?

version: '3.4'
serivces:
  some_service:
  build:
    network: host
Ntwobike
  • 2,406
  • 1
  • 21
  • 27
2

network_mode: host is not allowed in swarm mode.

isaadabbasi
  • 101
  • 5
2

I myself did not have success with networks or network_mode, but if you want to access a network service on the host, you can simply have the host service listen on the docker0 network interface, which is accessible from the container (depending on your network mode) at the same ip address. Proof of concept below.

On the host:

$ ip -4 a | grep docker0
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0

$ echo hello world | nc -l -p 8888 -s 172.17.0.1

On the container:

$ docker run --rm -it alpine nc -w1 172.17.0.1 8888
hello world

Another way to make a host service accessible to a docker container is to listen on a unix socket, which can be mounted in the container.

Aryeh Leib Taurog
  • 5,370
  • 1
  • 42
  • 49