0

I know technically host networking isn't supported MacOS (see https://docs.docker.com/network/host/)

The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.

However it does actually seem to work. E.g. this works just fine:

docker run \
  --name local-mysql \
  -e MYSQL_ROOT_PASSWORD=foo \
  -e MYSQL_DATABASE=baz \
  --network="host" \
  -d mysql:latest

However when I try to conditionally specify the host networking with a bash variable, it doesn't work, and I can't make sense of it. Consider the following test.sh:

#!/bin/bash

echo "Test 1"
docker rm -f local-mysql
docker run \
  --name local-mysql \
  -e MYSQL_ROOT_PASSWORD=foo \
  -e MYSQL_USER=master \
  -e MYSQL_PASSWORD=bar \
  -e MYSQL_DATABASE=baz \
  --network="host" \
  -d mysql:latest
docker ps

sleep 5

echo "Test 2"
export NETWORKING='--network="host"'
docker rm -f local-mysql
docker run \
  --name local-mysql \
  -e MYSQL_ROOT_PASSWORD=foo \
  -e MYSQL_USER=master \
  -e MYSQL_PASSWORD=bar \
  -e MYSQL_DATABASE=baz \
  ${NETWORKING} \
  -d mysql:latest
docker ps

This yields:

% ./test.sh
Test 1
local-mysql
6bbd68f0564943b8fb66ed37f1e639b54719bdb3b88b4e13aeef0a11cae4090b
CONTAINER ID   IMAGE          COMMAND                  CREATED                  STATUS                  PORTS     NAMES
6bbd68f05649   mysql:latest   "docker-entrypoint.s…"   Less than a second ago   Up Less than a second             local-mysql
Test 2
local-mysql
e286028ef9a1a27f4226beb60e766cc163c289239ba506f63a71a35adbc73ef3
docker: Error response from daemon: network "host" not found.
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

I.e. when I hard code --network=host into the docker command, the container starts fine. But the exact same parameter in an environment variable fails to start with network "host" not found.

I'm honestly not sure if this is a failure of bash or docker, but I can't actually figure out what's going wrong.

-- EDIT --

Changing

export NETWORKING='--network="host"'

to

export NETWORKING='--network=host'

works. And for my purposes right now that's enough. But just to be thorough... Why? The working example has quotes in the value (--network="host"), so why does the shell expansion break the non-working example? What if I wanted something like --network="my host"?

DrTeeth
  • 927
  • 2
  • 10
  • 32
  • 2
    In your env variable example your using literally `--network="host"`, including the quotes. Remove the double quotes – jordanm Oct 06 '21 at 15:29
  • Yep. That was dumb... – DrTeeth Oct 06 '21 at 15:42
  • 1
    In the future, you can usually spot this type of error with `set -x` – jordanm Oct 06 '21 at 15:43
  • In a Docker Desktop setup, there's a hidden VM that you don't usually interact with. If you `docker run -p` to publish a port, Docker also knows how to relay that port out to the MacOS host. If you `docker run --network=host` then the container runs on the VM's "host network" but it doesn't actually connect to the host OS. Many of the cases I've seen around SO don't actually require host networking, but have it for cases where they've incorrectly hard-coded `localhost` as a dependency location. – David Maze Oct 06 '21 at 15:46
  • 1
    The shell expansion is what got me. I tend to over-use quoting just in case some value has a space in it or whatever. And honestly I'm still kinda confused as to why my original question *didn't* work, as "Test1" did have quotes in the value. But I don't *need* to figure that out right now anyway. – DrTeeth Oct 06 '21 at 16:01
  • @DavidMaze The question just had a reduced example. In reality I'm using a docker container to build a docker image on the host, so I do need the host networking I think. – DrTeeth Oct 06 '21 at 16:04
  • The only thing you should need is the `-v` bind-mount option to access the host's Docker socket. It does not need to be a privileged container or use the host network. (Note, though, that the container is then free to launch other containers that do have these options.) – David Maze Oct 06 '21 at 16:34
  • @DrTeeth Putting quotes (and escapes etc) in your data (e.g. variables) doesn't work, because the shell parses quotes and escapes before expanding variables; by the time the quotes are part of the command, it's too late for them to do their intended job. Thus, quotes go *around* data, not *in* data. See: [Why does shell ignore quoting characters in arguments passed to it through variables?](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quoting-characters-in-arguments-passed-to-it-through-varia) – Gordon Davisson Oct 06 '21 at 17:42

0 Answers0