27

I am trying to execute a "docker-compose up" command. Please find below my docker-compose file. I tried with network_mode: "host" but it doesn't work. I am on Linux OS. request you to let me know if I am making any blunder.

version: '3.6'
services:
   mongo:
      image: "mongo:latest"
      container_name: ohif-mongo
       ports:
         - "27017:27017"

   viewer:
      image: ohif/viewer:latest
      container_name: ohif-viewer
      ports:
        - "3030:80"
      network_mode: "host"   # please make note of the alignment
      links:
         - mongo
      environment:
         - MONGO_URL=mongodb://mongo:27017/ohif
      extra_hosts:
         - "pacsIP:172.xx.xxx.xxx"
      volumes:
         - ./dockersupport-app.json:/app/app.json

after execution, I get the below error

ERROR: for 8f4c3de7e3a3_ohif-viewer  Cannot create container for service viewer: conflicting options: host type networking can't be used with links. This would result in undefined behavior
 ERROR: for viewer  Cannot create container for service viewer: conflicting options: host type networking can't be used with links. This would result in undefined behavior

I don't know why the error message is displayed twice. Not sure whether this is expected

Second, when I change the alignment of network_mode: "host" (by 1/2 spaces)

  ports:
        - "3030:80"
        network_mode: "host"   # please see the change in alignment now
      links:
         - mongo

I get the below error message

ERROR: yaml.parser.ParserError: while parsing a block mapping
 in "./docker-compose.yml", line 10, column 5
expected <block end>, but found '<block mapping start>'
 in "./docker-compose.yml", line 14, column 6

How can I start the container with network=host mode?

The Great
  • 7,215
  • 7
  • 40
  • 128

3 Answers3

23

network_mode: host is used for sharing the same networking space with the Host. For example you can want to access an application that is running on your Linux PC from the container. If you want to link services together, you can use links, or depends_on, and if the services are on different hosts just create an overlay network

Martlark
  • 14,208
  • 13
  • 83
  • 99
joekrom
  • 280
  • 2
  • 7
  • 7
    Mind that you cannot combine `network_mode: host` and `networks:` in one docker-compose file, it will cause "ERROR: 'network_mode' and 'networks' cannot be combined". – questionto42 Jan 28 '21 at 23:02
  • 5
    Strange this is an accepted answer. It just says what network_mode is, not 'How to make Network_Mode : "host" work in docker-compose.yml file' which was the actual question. – Midiman Mar 15 '22 at 21:53
6

From docs:

network_mode: "host" cannot be mixed with links.

And about links

Warning: The --link flag is a legacy feature of Docker. It may eventually be removed. Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using --link. One feature that user-defined networks do not support that you can do with --link is sharing environmental variables between containers. However, you can use other mechanisms such as volumes to share environment variables between containers in a more controlled way.

Just remove the links. They are no longer required.

k-lusine
  • 397
  • 4
  • 11
  • Even after removing that, I am not able to access the OHIF - viewer locally. How can I know whether my docker-compose up execution is successful and all the services are triggered successfully? – The Great Jun 10 '19 at 06:51
  • docker-compose up does execute successfully but am not able to view the web viewer? Is there anyway I can check the logs or verify the execution is successful? – The Great Jun 10 '19 at 06:55
  • When I try lynx command, it throws an error message that "unable to connect to remote host" but when I execute the docker run commands indvidually with network=host, it all works fine – The Great Jun 10 '19 at 06:57
  • In the [dockerfile](https://github.com/OHIF/Viewers/blob/master/dockerfile) of ohif/viewer we can see that it exposes the port 80, not 3000. I also removed network host (not sure why you need it here, if there's any specific reason, let's discuss to find another solution). Such a minimalistic docker-compose file worked for me. – k-lusine Jun 10 '19 at 07:33
  • version: '3.6' services: mongo: image: "mongo:latest" container_name: ohif-mongo ports: - "27017:27017" viewer: image: ohif/viewer:latest container_name: ohif-viewer ports: - "3030:80" volumes: - ./dockersupport-app.json:/app/app.json – k-lusine Jun 10 '19 at 07:34
  • @ Lusine Karapetian - I tried that as well. Can you please help me with this? It is related to the same issue https://stackoverflow.com/questions/56522164/how-to-get-the-port-numbers-for-a-service-docker-compose-file – The Great Jun 10 '19 at 07:39
  • I don't see any ports to access them locally – The Great Jun 10 '19 at 07:39
  • @AVLES why do you want to use the network mode **host** – k-lusine Jun 10 '19 at 07:41
  • Else we aren't able to access them locally in our desktop. Our docker is running in a remote server (linux) for which we don;'t have any UI. If I don't use them, I am not able to port forward them to local desktop. Am not really sure, do people usually do that when there is no internet inside the conatiner – The Great Jun 10 '19 at 07:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194681/discussion-between-avles-and-lusine-karapetian). – The Great Jun 10 '19 at 07:44
0

A container can't be on both the host network and the bridge network, not by using links, nor by any other combination of properties. You have to explicitly pass traffic between the host and the internal bridge network.

To get from the host to the internal network, the host network can access ports exposed by internal containers. So, for instance, note the use of 'localhost' here:

services:
  mongo:
    image: "mongo:latest"
    ports:
      - "27017:27017"
  viewer:
    image: ohif/viewer:latest
    network_mode: host
    environment:
        MONGO_URL: mongodb://localhost:27017/ohif
    # no links required

To go the other direction, from internal to host, you have to make use of the 'host-gateway' special property.

services:
  proxy-app:
    image: proxy-app
    network_mode: host
  service-app:
    image: service-app
    extra_hosts:
      - proxy-host:host-gateway
    environment:
      PROXY_URL: http://proxy-host
nupanick
  • 754
  • 8
  • 13