5

For a small project, I want an application in a docker container to connect to the localhost of the machine. An answer to this question: From inside of a Docker container, how do I connect to the localhost of the machine? tells me the preferred way is to use --net="host" in the docker run command.

I use a compose file to start the container. And this question told me the net option was renamed to network_mode: "host".

Here is the beginning of the compose file

version: '3.6'
services:
  shiny:
    image: paulrougieux/eutradeflows
    deploy:
      restart_policy:
        condition: on-failure
    network_mode: "host"
    ports:
      - "3838:3838"

When I start this file

 sudo docker stack deploy -c stackshiny.yml shiny

I get the error:

Ignoring unsupported options: network_mode

For information

$ sudo docker version
Client:
 Version:   18.04.0-ce
Server:
 Engine:
  Version:  18.04.0-ce

How do I enable connection to a database on the host from a docker container?

Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110

2 Answers2

7

As stated in the docs for docker-compose file network_mode:

Notes This option is ignored when deploying a stack in swarm mode with a (version 3) Compose file. network_mode: "host" cannot be mixed with links.

The network_mode cannot be used when deploying on docker swarm using docker stack deploy. This is not new with version 18.04 but is rather older.

The network_mode can only be used with docker-compose when deploying the container on the local machine using docker-compose up.

yamenk
  • 46,736
  • 10
  • 93
  • 87
  • But I'm not deploying in a swarm mode, I just want to use this container on one server. I thought `docker stack deploy` had replaced `docker-compose` for all purposes. – Paul Rougieux Apr 26 '18 at 09:42
  • 3
    @PaulRougieux `docker stack` is only for docker swarm. https://docs.docker.com/engine/reference/commandline/stack_deploy/#extended-description – yamenk Apr 26 '18 at 09:43
  • [This docker issue](https://github.com/docker/for-mac/issues/1031) may be related. OK I see the output of `sudo docker network ls` tells me the scope is `swarm`. – Paul Rougieux Apr 26 '18 at 10:04
2

I was able to get around some of the limitations of the docker stack deploy by specifying the mode in the ports section:

for example in your docker-compose.yml file:

ports:
  # Bypass the routing mesh by setting mode to host
  - target: 8080
    published: 80
    protocol: tcp
    mode: host

This is covered in the section "Bypassing the routing mesh" here: https://docs.docker.com/engine/swarm/ingress/

And listed as an option in the docker compose-spec in the section "ports long syntax" here: https://github.com/compose-spec/compose-spec/blob/master/spec.md

user3788120
  • 447
  • 4
  • 4