Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Network_Mode : "host" work in docker-compose.yml file

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?

like image 408
The Great Avatar asked Jun 10 '19 06:06

The Great


People also ask

What is Network_mode host in Docker compose?

Docker network host, also known as Docker host networking, is a networking mode in which a Docker container shares its network namespace with the host machine. The application inside the container can be accessed using a port at the host's IP address (e.g., port 80).

How does Docker machine manage virtual hosts that use Docker?

Docker Engine accepts docker commands from the CLI, such as docker run <image> , docker ps to list running containers, docker image ls to list images, and so on. Docker Machine is a tool for provisioning and managing your Dockerized hosts (hosts with Docker Engine on them).

What is host network mode in Docker containers?

Use host networking If you use the host network mode for a container, that container’s network stack is not isolated from the Docker host (the container shares the host’s networking namespace), and the container does not get its own IP-address allocated.

How do I use a specific network in Docker Compose?

If you use the docker-compose command, use network_mode instead. If you want to use a particular network on a common build, use [network] as mentioned in the second yaml file example. The syntax for using built-in networks such as host and none is a little different.

How to use network host in Docker Swarm?

We can also use this network in Docker swarm as well by passing option ‘–network host’ to the ‘docker service create’. If we have to run a container that needs to handle a large range of ports because ‘userland-proxy’ is not created for each port.

How to make a host service accessible to a docker container?

Another way to make a host service accessible to a docker container is to listen on a unix socket, which can be mounted in the container.


2 Answers

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

like image 168
joekrom Avatar answered Nov 15 '22 13:11

joekrom


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.

like image 32
k-lusine Avatar answered Nov 15 '22 12:11

k-lusine