Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get container name from inside? docker.io

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.

like image 580
Mariano DAngelo Avatar asked Nov 17 '14 18:11

Mariano DAngelo


People also ask

How would you describe a Docker container?

A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.


2 Answers

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
like image 101
Usman Ismail Avatar answered Sep 24 '22 15:09

Usman Ismail


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.

like image 39
Amir Razmjou Avatar answered Sep 21 '22 15:09

Amir Razmjou