Docker Community Forums

Share and learn in the Docker community.

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

docker

(Zbetz) #1

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.


Network_mode host doesn't work
(Octesian) #2

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!


(Zbetz) #3

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

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