Get container ID from django app running inside that container

By default, the hostname inside a container will be the short id of that container:

$ docker run -it python:3 python
Python 3.4.3 (default, Aug 23 2015, 01:11:10)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket; print(socket.gethostname())
954ced8d1832

Here’s the container in my ‘docker ps’ output:

$ docker ps
CONTAINER ID        IMAGE                        COMMAND                  CREATED              STATUS              PORTS                    NAMES
954ced8d1832        python:3                     "python"                 About a minute ago   Up About a minute                            ecstatic_saha

Also, as ranjandas pointed out, you could potentially configure your logging mechanism inside the container to log to stdout. At that point, you can use docker’s logging mechanism, and configure it to go to a syslog server. The container id will be handled at that level.

/Jeff

1 Like