1

I need to reference the host or host network during a docker build, in the Dockerfile, how do I do that? I want to do this, to clone some git repos, or to scp some files to set the image up with.

Its easy to clone a github repo, because the docker will resolve the dns for that. However, I don't have a dns entries for my host network available to the docker image being built.

In fact, I don't even know what the ip address of the host is, never mind getting as far as setting up dns.

user2800708
  • 1,890
  • 2
  • 18
  • 31
  • http://stackoverflow.com/a/24189767/107049 might help – Thomasleveil Jan 12 '16 at 23:00
  • I think I'm going to approach this differently - create .deb artifacts from the sources I need in my image. scp them to the current directory from wherever the CI build puts them. build with that directory as the context - and the install them with dpkg in the docker container. – user2800708 Jan 12 '16 at 23:51

3 Answers3

5

As of docker api 1.25 you can now simply do:

docker build --network=host -f myDockerFile ...

And that will give you access to the host network during build.

dgorissen
  • 6,207
  • 3
  • 43
  • 52
4

What you are trying to do goes against the idea of a Dockerfile.

The intent of a Dockerfile is to provide a "description" of an image while having the warranty of reproducibility. This is why you don't have any host specific in your Dockerfile so it can be built anywhere with the same result.

If you need closer interaction with your host, it means that your result is going to be tight to this host and you should do it at runtime. Look at CMD or ENTRYPOINT to have the container performm certain operations at startup.

creack
  • 116,210
  • 12
  • 97
  • 73
  • I don't want to share these Dockerfiles on Docker Hub and put my sources on GitHub, this code is private. All I am trying to do is get private sources onto an image during build. It will only build on a particular host network, that is true, but the resulting image will still be able to be deployed to the cloud. – user2800708 Jan 12 '16 at 23:53
  • But I think your answer is fair; if it goes against the idea of a Dockerfile I should do what I am trying to do another way. See my comment on the OP, I am going to build some .deb packages, and then put them in the build context, then run 'docker build'. – user2800708 Jan 13 '16 at 10:07
-1

It seems very odd that you won't know the Docker host. I can't even fathom how that's possible.

Just do this by passing in the host information during build.

docker run -e DOCKER_HOST=1.2.3.4 busybox env

Alternatively, you're going to have to use some sort of discovery system such as DNS, etcd, zookeeper, or consul.

taco
  • 1,367
  • 17
  • 32
  • But I want to use 'docker build' not 'docker run'. – user2800708 Jan 13 '16 at 10:06
  • Then your question isn't a docker question. You need a script to modify your Dockerfile before the `docker build` even takes place or you need to use the classic discovery method of DNS. – taco Jan 13 '16 at 10:15