10

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.
2
  • 2
    Well you could use network_mode property in the service. This is mutually exclusive with the networks property though. If you use the network_mode: "host" your service has access to the test network, without explicitly joining. What exactly do you want to accomplish? Commented Jul 7, 2018 at 17:39
  • 1
    @vstm Can you pls describe how to access test network if network_mode: "host" is enabled? Commented Apr 15, 2019 at 19:41

2 Answers 2

1

As per the docs:

  • target: the port inside the container
  • published: the publicly exposed port
  • protocol: the port protocol (tcp or udp)
  • mode: host for publishing a host port on each node, or ingress for a swarm mode port to be load balanced.

Example:

 ports:
   - target: 8080
     published: 8080
     protocol: tcp
     mode: host
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.