Option network_mode: host in docker compose file not working as expected

I have a Windows 7 machine with an Ubuntu VM created through VirtualBox. Within this Ubuntu VM I have docker and docker-compose installed. Version info below:

Host OS: Windows 7 Enterprise
VirtualBox: version 5.2.12
Guest OS: Ubuntu version 18.04
docker: version 18.05.0-ce, build f150324
docker-compose: version 1.21.2, build a133471

My Windows 7 machine has Cisco AnyConnect installed which is used to connect to our VPN, because our app needs resources located within this network. The Ubuntu VM can see the VPN (and resolve URLs in this network) but the docker containers created within the VM cannot. I was able to partially get around this issue by passing --network host to our docker build commands. This binded the containers to the host network (the Ubuntu VM’s network) which allowed the containers to access resources within the VPN.

Now, my issue: We use a docker compose file on our project. The --network host option cannot be passed to our docker-compose build commands, but must instead be specified in the docker compose file with network_mode: host. Unfortunately, this is not working as expected. I have tried both network_mode: host and network_mode: "host", but no luck.

It seems other docker compose users are having the same issue. See here and here.

I have also tried using the host’s networking stack, but no luck. This change likely didn’t work because of following line in the docs:

Only used if you use docker stack commands. If you use the docker-compose command, use network_mode instead.

For reference, here is our docker-compose.yml file:

version: '3.4'
services:
  mssql:
    network_mode: host
    build:
      context: ./compose/mssql
      dockerfile: ./Dockerfile
    environment:
      ACCEPT_EULA: 'Y'
    env_file:
      - .env
    ports:
      - 1433:1433
  selfcheckui:
    network_mode: host
    build:
      context: .
      dockerfile: ./Dockerfile-myeverify-selfcheckui
    ports:
      - 8004:8080
    environment:
      SPRING_PROFILES_ACTIVE: compose
    depends_on:
      - mssql
    env_file:
      - .env
      - proxy.env

If you know of workaround(s) to this issue, any help is appreciated.

3 Likes

I had a similar problem where after connecting to a VPN, my containers couldn’t access the internet. I’ve managed to solve this by adding the network flag to the builds section of my compose files:

version: '3.4'
serivces:
  some_service:
  build:
    network: host
    ...

Hope that helps!

6 Likes

@octesian - Using network: host worked for me, thank you! :slight_smile:

Now I’m wondering why this was not in the compose file documentation.

2 Likes

ERROR: The Compose file ‘./docker-compose.yml’ is invalid because:
Unsupported config option for services.rancher-agent: ‘network’

in 18.09 


You have to use version: ‘3.4’ for this property :wink:

Works perfectly. Thanks

Nope. I’m using 3.4 and it doesn’t recognize “network” as a valid property. I’m using docker for windows if that matters.

I wonder why people who aim their compose.yml for docker-compose are using the v3.x format.
The v2.x format is intended for docker-compose and v3.x is intended for swarm stack deployments


network: host was introduced in version 2.2 of the docker compose specification:

Docker on Windows is a whole different breed. I have no idea what does and does not work for windows. It is just odd to see that a majority of the topics of the last days was Windows related


fantastic! working fine!

version: '3.4'
services:
  database:
    ....
    network_mode: host

I have this same problem and resolution works when I build image locally but when I try download builded image from repo it does not work. I tried with:

  1. network_mode: host" under service name
  2. networks with specified IP address for each container and with main “networks” with configured driver: host
  3. Here is my docker-compose.yml with all changes (some of them are commented out): https://pastebin.com/Eh6usWj1
    Is there some resolution with networking using image downloaded from repo? network: host works only under build: .

perfect, just what I was looking for

In this compose file after using network_mode: host, you are also mapping port to host port, which should not be the case. As per my understanding services with host network should run directly on host machine. May be your services are working because there is a port mapping. Did you try removing the port mapping and test only with network_mode:host?
Let me know i am wrong.
Thank you.

I have no port mapping and network_mode:host with docker-compose version: ‘3.8’ doesn’t work, also doesn’t work with version ‘3.4’ :frowning:

Note everyone having troubles that “network: host” is only supported by Docker on Linux. If you are running Docker for Mac or Docker Desktop in Windows you will never be able to “join” the host network in a native way.

See Networking using the host network | Docker Docs :slight_smile:

  • The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.