title: How does this Docker command, which makes a small GoLang container, work?
body: <p>New to docker here.  I followed the instructions <a href="https://www.iron.io/an-easier-way-to-create-tiny-golang-docker-images/" rel="nofollow noreferrer">here</a> to make a slim &amp; trim container for my Go Project.  I do not fully understand what it's doing though, hopefully someone can enlighten me.</p>
  
  <p>Specifically there are two steps to generating this docker container.</p>
  
  <ol>
  <li><code>docker run --rm -it -v "$GOPATH":/gopath -v "$(pwd)":/app -e "GOPATH=/gopath" -w /app golang:1.8 sh -c 'CGO_ENABLED=0 go build -a --installsuffix cgo --ldflags="-s" -o hello'</code></li>
  <li><code>docker build -t myDockerImageName .</code></li>
  </ol>
  
  <p>The DockerFile itself just contains</p>
  
  <pre><code>FROM iron/base
  WORKDIR /app
  COPY hello /app/
  ENTRYPOINT ["./hello"]
  </code></pre>
  
  <p>I understand (in a broad sense) that the 1st step is compiling the go program and statically linking the C-dependencies (and doing all this inside an unnamed docker container).  The 2nd step just generates the docker image according to the instructions in the DockerFile.</p>
  
  <p>What I don't understand is why the first command starts with <code>docker run</code> (why does it need to be run inside a docker container?  Why are we not just generating the Go binary outside of it, and then copying it in?)</p>
  
  <p>And if it's being run inside a docker container, how is binary generated in the docker container being dropped on my local machine's file system?(e.g. why do I need to copy the binary back into the image - as it seems to be doing on line 3 of the DockerFile?)</p>