lciukaj
(luke76fcec)
February 22, 2023, 12:36pm
1
hi Team.
Do you know by any chance how to get container ID from within the container with cgroup v2? Tried to find a solution in google, problem is well-know (couple of topics on stackoverflow) but no solution as of now.
Perhaps someone has a working command?
thx!
rimelek
(Ákos Takács)
February 22, 2023, 8:32pm
2
Can you share why you need to know the container id inside the container itself?
I remembered an issue wich was related to cgroup v2 and commands in the container, I am not sure that helps, but I leave it here anyway
In aim to check if we are inside of container we need to type (it is taken from the web):
# docker container exec -it ub bash
root@8d6a3b7407c5:/# cat /proc/1/cgroup
0:
‘ub’ is the name of Ubuntu Docker image.
Before I created container like that:
# docker container run -itd --name ub ubuntu:latest bash
8d6a3b7407c5cf0f7c38e1a64ebb9fe0ab61b336cc2e60cdff04c8821145e1c7
As you can see inside of my container there is no any ‘docker’ word after the /. Why?
And now that I shared this link I realized I already answered a question related to getting the container id which might help you as well
I cans ee multiple potential problems:
You should not use $(date +%Y-%m-%d-%H.%M.%S) multiple times, because the time between the two execution can change, so you will refer to a wrong container name.Save the result to a variable, and use the variable multiple times.
I am not sure if yoiu really wanted to do this: pwd & sudo docker ...
If you want to run your container in the background, you can just use the --detach or -d flag, but then you need to run a command in the container which keeps …
You could for example save the cid file in a folder that is mounted into the container so you just read a file in the container that you mounted.