How to do host-specific customization on docker images

Let me explain what I want with a silly example:

After I do “docker pull” to download an image to my host, I want to create a file /etc/myname on this image to have the exact name of this host. As a result, all containers running this image on this host can find the hostname by reading /etc/myname.

  • Kubernetes on Mesos, no suitable offer available
  • Best way to reduce the size of a custom Docker image
  • docker can't not start postgresql server with a existing data directory
  • Cron containers for docker - how do they actually work?
  • Kafka producer throws 'TimeoutException: Batch Expired' exception
  • Access Docker forwarded port on Mac
  • Plus, I want the file /etc/myname to be shared across all contains on this host. I know I can easily create this file separately in each container, but that’s not what I want.

    (Again, this is just a silly example. I don’t actually need to store the hostname. I want to store a large amount of host-specific data in a shared file, without using a shared volume).

    I can do that by manually creating the file myself, where $dir is the top-most layer of the image:

    dir=17024e41f8b6c958c5c9e60bffa8b6c8b2da5a1235b6e18085d5059f9602f605
    echo $HOSTNAME > /var/lib/docker/aufs/diff/$dir/etc/myname
    

    But is there a less hacky way to do this?

  • Why does DockerFile ADD not dereference symbolic links?
  • How to install nvm in a Dockerfile?
  • How to use Docker Swarm On Mesos?
  • Docker compose: is it possible to mount a local file to a remote daemon?
  • How do you map port on host inside swarm mode on Windows?
  • Boot2Docker: how to access container with Bridged Networking
  • One Solution collect form web for “How to do host-specific customization on docker images”

    The easiest way to do this would be to use a shared volume, and that is in fact the only way to do it currently. I assume you know about bind mounting in docker, but I’ll show here just in case.

    To the docker run command, as well as passing -v <volume name>:<path in container> you can also pass <path on host>:<path in container>. So you could have your metadata in the same place on each host and then bind mount it into the containers.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.