What are .dockerenv and .dockerinit?

Solution 1:

AFAIK there is no official documentation about them.

Those files where only used by the old and deprecated LXC execution driver. They were hacks required by docker when using LXC to run the containers.

The .dockerinit was a sort of init process. It was the binary run by the lxc-attach command called when starting a container. It was the responsible to setup the environment, the user and the working directory and then run your entrypoint/cmd.

The .dockerenv contained the environment variables defined inside the container. They were used to setup the environment variables properly after the lxc-attach. This file was read by the .dockerinit process.

The new libcontainer/runc driver (the driver enabled by default) does not use these files. You will find them empty in your containers. In fact, the LXC support has been recently removed from the docker development branch.

So maybe these files will eventually disappear in the future, although currently are widely used by applications to detect the presence of docker.

Solution 2:

In order to find out, whether their code is running inside a docker environment, it was popular to test for the existence of either the /.dockerinit or the /.dockerenv file.

Since the dockerinit file has been removed in newer versions, the best idea should now be to check the existence of the dockerenv file.

  • Quoting another source:

    I'm not sure how official /.dockerenv is -- it's pretty much undocumented, but docker/libnetwork#815 seems to imply that it'll be longer-lived

A sample implementation of such a check from our code:

if ! [ -f /.dockerenv ] ; then
  echo "Not running inside docker, exiting to avoid data damage." >&2
  exit 1
fi