title: Docker In Docker IN Docker Compose (Networking Issues)
body: <p>I literally cannot explain to you how hard it is to wrap my head around wanting to create a docker container that will be running docker that will be running my web server that will have access to my outer docker host.</p>
  
  <p>So what I know so far:</p>
  
  <ul>
  <li>I have a dockerfile that when executed outside of a docker
  compose and tested with I can see that it connects to my host docker
  daemon!</li>
  <li>Next I move into my docker-compose setup and I understand I must be
  having an issue because of the networking that occurs inside of
  docker-compose files. But I have 0 docker-compose networking
  experience, so I ask how can I also attach my container to the
  default network once inside of this setup?</li>
  </ul>
  
  <p>My Bash Script (Entrypoint):</p>
  
  <pre><code>#!/bin/sh
  set -e
  
  
  # first arg is `-f` or `--some-option`
  if [ "${1#-}" != "$1" ]; then
      set -- docker "$@"
  fi
  
  # if our command is a valid Docker subcommand, let's invoke it through Docker instead
  # (this allows for "docker run docker ps", etc)
  if docker help "$1" &gt; /dev/null 2&gt;&amp;1; then
      set -- docker "$@"
  fi
  
  # if we have "--link some-docker:docker" and not DOCKER_HOST, let's set DOCKER_HOST automatically
  if [ -z "$DOCKER_HOST" -a "$DOCKER_PORT_2375_TCP" ]; then
      export DOCKER_HOST='tcp://docker:2375'
  fi
  
  if [ "$1" = 'dockerd' ]; then
      cat &gt;&amp;2 &lt;&lt;-'EOW'
           Hey there!  It looks like you're trying to run a Docker daemon.
             You probably should use the "dind" image variant instead, something like:
               docker run --privileged --name some-overlay-docker -d docker:stable-dind --storage-driver=overlay
             See https://hub.docker.com/_/docker/ for more documentation and usage examples.
      EOW
      sleep 3
  fi
  
  docker version
  
  dotnet ApiServer.dll
  </code></pre>
  
  <p>My compose file:</p>
  
  <pre><code>version: '3.4'
  
  services:
    apiserver:
      image: ${DOCKER_REGISTRY-}apiserver
      build:
        context: ./
        dockerfile: ./ApiServer/Dockerfile
      volumes:
        - /var/run/docker.sock:/tmp/docker.sock
  </code></pre>
  
  <p>Lastly my dockerfile: (Shouldn't really need this but here it is)</p>
  
  <pre><code>FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-alpine AS base
  
  COPY /ApiServer/modprobe.sh /usr/local/bin/modprobe
  COPY /ApiServer/docker-entrypoint.sh /usr/local/bin/
  
  RUN dos2unix -u /usr/local/bin/modprobe
  RUN dos2unix -u /usr/local/bin/docker-entrypoint.sh
  
  WORKDIR /app
  EXPOSE 80
  
  FROM mcr.microsoft.com/dotnet/core/sdk:2.2-alpine AS build
  WORKDIR /src
  COPY ["ApiServer/ApiServer.csproj", "ApiServer/"]
  COPY ["SharedLibs/SharedLibs.csproj", "SharedLibs/"]
  RUN dotnet restore "ApiServer/ApiServer.csproj"
  COPY . .
  WORKDIR "/src/ApiServer"
  RUN dotnet build "ApiServer.csproj" -c Release -o /app
  
  FROM build AS publish
  RUN dotnet publish "ApiServer.csproj" -c Release -o /app
  
  FROM base AS docker
  ## Docker Setup
  RUN apk add --no-cache \
          ca-certificates
  
  # set up nsswitch.conf for Go's "netgo" implementation (which Docker explicitly uses)
  # - https://github.com/docker/docker-ce/blob/v17.09.0-ce/components/engine/hack/make.sh#L149
  # - https://github.com/golang/go/blob/go1.9.1/src/net/conf.go#L194-L275
  # - docker run --rm debian:stretch grep '^hosts:' /etc/nsswitch.conf
  RUN [ ! -e /etc/nsswitch.conf ] &amp;&amp; echo 'hosts: files dns' &gt; /etc/nsswitch.conf
  
  ENV DOCKER_CHANNEL stable
  ENV DOCKER_VERSION 18.09.3
  # TODO ENV DOCKER_SHA256
  # https://github.com/docker/docker-ce/blob/5b073ee2cf564edee5adca05eee574142f7627bb/components/packaging/static/hash_files !!
  # (no SHA file artifacts on download.docker.com yet as of 2017-06-07 though)
  
  RUN set -eux; \
      \
  # this "case" statement is generated via "update.sh"
      apkArch="$(apk --print-arch)"; \
      case "$apkArch" in \
          x86_64) dockerArch='x86_64' ;; \
          armhf) dockerArch='armel' ;; \
          aarch64) dockerArch='aarch64' ;; \
          ppc64le) dockerArch='ppc64le' ;; \
          s390x) dockerArch='s390x' ;; \
          *) echo &gt;&amp;2 "error: unsupported architecture ($apkArch)"; exit 1 ;;\
      esac; \
      \
      if ! wget -O docker.tgz "https://download.docker.com/linux/static/${DOCKER_CHANNEL}/${dockerArch}/docker-${DOCKER_VERSION}.tgz"; then \
          echo &gt;&amp;2 "error: failed to download 'docker-${DOCKER_VERSION}' from '${DOCKER_CHANNEL}' for '${dockerArch}'"; \
          exit 1; \
      fi; \
      \
      tar --extract \
          --file docker.tgz \
          --strip-components 1 \
          --directory /usr/local/bin/ \
      ; \
      rm docker.tgz; \
      \
      dockerd --version; \
      docker --version
  
  FROM docker AS final
  WORKDIR /app
  COPY --from=publish /app .
  
  ENTRYPOINT ["../usr/local/bin/docker-entrypoint.sh"]
  </code></pre>