Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the host network with docker-compose

I'm trying to set up a container with two networks with docker-compose. I would like one of those networks to be the host network (ie: as if I was running docker run --net=host) and the other a user-defined network.

I tried this:

version: '3'

networks:
  test:
    driver: bridge
  host:
    external: true

services:
  helloworld:
    image: python:3.6-alpine
    networks:
      - test
      - host
    cap_add:
      - NET_ADMIN
      - NET_RAW
    ports:
      - 0.0.0.0:8080:8080
    command: sh -c "cd /tmp && python -m http.server 8080"

I'm getting this error:

$ docker-compose up
Removing blah_helloworld_1
Starting 8743618e8af4_blah_helloworld_1 ... error

ERROR: for 8743618e8af4_blah_helloworld_1  network-scoped alias is supported only for containers in user defined networks

ERROR: for helloworld  network-scoped alias is supported only for containers in user defined networks
ERROR: Encountered errors while bringing up the project.
like image 295
Juicy Avatar asked Jul 07 '18 15:07

Juicy


1 Answers

While host is listed in docker network ls it works very differently from other networks.

By default each container is isolated in its own separate network namespace. When a container is put in the "host" network (using network_mode: host with compose), the container simply stays in the host's network namespace.

A container cannot be in a bridge network (like test) and also be in the host network at the same time as that would require this container to simultaneously be both in the host's network namespace and have its own separate network namespace.

like image 69
wadadayum Avatar answered Sep 17 '22 19:09

wadadayum