How can I get docker's container name from inside the container?
I can't use "inspect" because I have to use a script from inside the container to get information from a JSON url.
How can I get docker's container name from inside the container?
I can't use "inspect" because I have to use a script from inside the container to get information from a JSON url.
If you mean the Container ID its available in the env as the hostname variable. It should be interchangeable with the name for most operations.
env
HOSTNAME=5252eb24b296
TERM=xterm
....
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5252eb24b296        ubuntu:14.04        "bash"              23 seconds ago      Up 22 seconds                           test
you can use cut -c9- < /proc/1/cpuset
If you want the container name rather than container id you can do a reverse DNS lookup on eth0 of the container.
dig -x `ifconfig eth0 | grep 'inet' | awk '{print $2}'` +short | cut -d'.' -f1
This gives you the friendly name rather than the id.
UPDATE: Only works if you have ifconfig and dig and other tools installed.
I think, most reliable way to use combination of --cidfile and -v options.
docker run --cidfile=/tmp/container.id -v /tmp/container.id:/tmp/container.id ${IMAGE}
If you will start container this way, you can read /tmp/container.id from inside of your container.
if you have docker inside container,
(you need to bind -v /var/run/docker.sock:/var/run/docker.sock)
and the hostname was not modified you can get it via:
docker inspect -f '{{.Name}}' $HOSTNAME
but this solution can be used only in edge cases.
In the case of docker-compose we had a / in front of the hostname so using cut we removed that.
docker inspect -f '{{ .Name }}' "$HOSTNAME" | cut -c 2-