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.

  • Using GPU from a docker container?
  • Can't connect to container from docker host
  • docker command attach running slow
  • Cache docker images on Travis CI
  • Why is it not possible to call locale-gen with sudo inside a docker container?
  • How to reach a linked service in docker-compose?
  • 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:

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

    But is there a less hacky way to do this?

  • Docker : Unable to resolve the name of the machine host?
  • Docker machine using generic driver: x509: certificate signed by unknown authority when trying to add remote host to local machine
  • Docker execute ant script after tomcat starts?
  • How to measure containers start-up and end time from some API?
  • Docker 1.0 CentOS 6.5 “unable to mount sys as readonly” when running image
  • Using the Spotify Maven Docker plugin, how can I change the file permissions of a resource that I add to a Docker container?
  • 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.