Docker Community Forums

Share and learn in the Docker community.

Get container ID from django app running inside that container


(Rajagola) #1

Hi,

I have a django app running inside a container. Can I have a way/approach to get that container ID using python code/module inside that django app.

Like some module inside the django app say container.py

container.py

def get_this_container_id()
return this_container_id

This is required for logging, to say from which container the django logs are generated from.

Thanks
Raja


(Ranjandas) #2

Hi,

It would be nice if you can explain on what kind of logging system you are using and how are you planning to send logs from and to where.

Have you looked at the docker Logging-Driver? Using logging driver you can send the logs of your container to central log servers like Syslog where it will automatically have the container id + the log.

Eg: This is a log collected in a syslog server from a container that was started to ping google 3 times.


Oct  1 12:10:29 dev docker/69731620d6e1[1037]: PING google.com (216.58.220.142) 56(84) bytes of data.#015
Oct  1 12:10:29 dev docker/69731620d6e1[1037]: 64 bytes from google.com (216.58.220.142): icmp_seq=1 ttl=61 time=20.5 ms#015
Oct  1 12:10:30 dev docker/69731620d6e1[1037]: 64 bytes from google.com (216.58.220.142): icmp_seq=2 ttl=61 time=19.3 ms#015
Oct  1 12:10:31 dev docker/69731620d6e1[1037]: 64 bytes from google.com (216.58.220.142): icmp_seq=3 ttl=61 time=24.9 ms#015
Oct  1 12:10:31 dev docker/69731620d6e1[1037]: #015
Oct  1 12:10:31 dev docker/69731620d6e1[1037]: #015
Oct  1 12:10:31 dev docker/69731620d6e1[1037]: --- google.com ping statistics ---#015
Oct  1 12:10:31 dev docker/69731620d6e1[1037]: 3 packets transmitted, 3 received, 0% packet loss, time 2004ms#015
Oct  1 12:10:31 dev docker/69731620d6e1[1037]: rtt min/avg/max/mdev = 19.301/21.623/24.989/2.439 ms#015

Here you can see it includes the timestamp, the docker host name, container ID and the log.

You can find more details here. https://docs.docker.com/reference/logging/overview/

Regards


(Jeff Anderson) #3

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


(Rajagola) #4

Thanks for the replies…

Basically, I have two 2 types of logs here.

  1. Docker container logs (stdout/stderr)
  2. application logs (django app inside the docker container. like log_info(“some user action”))

For docker logs, ranjandas suggests to use docker Logger driver. I will check this out.
For django app logs(app inside container), Jeff suggests to use a mechanism to log to stdout and will be taken care by docker logging.

Please comment…