How can I get Docker Linux container information from within the container itself?
I've found out that the container id can be found in /proc/self/cgroup
So you can get the id with :
cat /proc/self/cgroup | grep -o -e "docker-.*.scope" | head -n 1 | sed "s/docker-\(.*\).scope/\\1/"
How to get docker container ID from within the container with cgroup v2
The --cgroupns host
fix is effective, but not available if you don't control the container's creation. Further, this docker run
option is not available in the API or docker compose (https://github.com/compose-spec/compose-spec/issues/148).
But... good news - the container ID is still exposed via /proc/self/mountinfo:
678 655 254:1 /docker/containers/7a0144cee1256c539fab790199527b7051aff1b603ebcf7ed3fd436440ef3b3a/resolv.conf /etc/resolv.conf rw,relatime - ext4 /dev/vda1 rw
679 655 254:1 /docker/containers/7a0144cee1256c539fab790199527b7051aff1b603ebcf7ed3fd436440ef3b3a/hostname /etc/hostname rw,relatime - ext4 /dev/vda1 rw
680 655 254:1 /docker/containers/7a0144cee1256c539fab790199527b7051aff1b603ebcf7ed3fd436440ef3b3a/hosts /etc/hosts rw,relatime - ext4 /dev/vda1 rw
Here's a Python snippet that'll parse it:
with open( '/proc/self/mountinfo' ) as file:
line = file.readline().strip()
while line:
if '/docker/containers/' in line:
containerID = line.split('/docker/containers/')[-1] # Take only text to the right
containerID = containerID.split('/')[0] # Take only text to the left
break
line = file.readline().strip()
Credit goes to richgriswold: https://community.toradex.com/t/python-nullresource-error-when-running-torizoncore-builder-build/15240/4
how to get container name from inside? docker.io
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
How to get the image name of a docker container from inside the container
This type of information/metadata is not passed into the container by default. There are 2 reasons for this.
- if every metadata about the container was passed into the container, it could potentially pose a security risk when someone breaks into it
- container shouldn't need this kind of information anyway
The reason why you can get access to container's ID from within the container is simply because it is used as the container's hostname by default. If you would specify hostname when running the container with container run --hostname ...
, you wouldn't have access to the ID either.
I don't know why you need this information but the only way to get it when you are inside of the container is to first pass it to the container in one way or another. For example via an environment variable.
If you don't have access to this information from the outside of the container (which seems strange if you are able to use docker exec
), you will not get it from the running container.
Get Docker container id from container name
In Linux:
sudo docker ps -aqf "name=containername"
Or in OS X, Windows:
docker ps -aqf "name=containername"
where containername
is your container name.
To avoid getting false positives, as @llia Sidorenko notes, you can use regex anchors like so:
docker ps -aqf "name=^containername$"
explanation:
-q
for quiet. output only the ID-a
for all. works even if your container is not running-f
for filter.^
container name must start with this string$
container name must end with this string
How to get the IP address of the docker host from inside a docker container
/sbin/ip route|awk '/default/ { print $3 }'
As @MichaelNeale noticed, there is no sense to use this method in Dockerfile
(except when we need this IP during build time only), because this IP will be hardcoded during build time.
Related Topics
Understanding Linux /Proc/Pid/Maps or /Proc/Self/Maps
Using Awk to Print All Columns from the Nth to the Last
How to Know the Script File Name in a Bash Script
How to Escape Single Quotes in Bash/Grep
How to Open a New Tab in Gnome Terminal from Command Line
"/Usr/Bin/Ld: Cannot Find -Lz"
How to Pipe Initial Input into Process Which Will Then Be Interactive
Using Grep to Get the Next Word After a Match in Each Line
How to Run a Script At a Certain Time on Linux
How to Analyze a Program'S Core Dump File With Gdb When It Has Command-Line Parameters
How to Install Latest Version of Git on Centos 8.X/7.X/6.X
Tool to Trace Local Function Calls in Linux
Hello, World in Assembly Language With Linux System Calls
How to Get Cmake to Find My Alternative Boost Installation
How to Set Linux Environment Variables With Ansible
How to Open a Socket and Pass It to Another Process in Linux