rimelek
(Ákos Takács)
March 6, 2024, 5:09pm
3
You can’t really do that, but you can check some facts in the environment which can indicate the process is running in a container. Even if you do that, it could be different everywhre. As I suggested in this topic
It can be useful, I don’t disagree. And I also have to admit I didn’t really think the beginning of my previous reply through. I should have written we usually don’t need that, but I also know that it is indeed important for JVM to handle resources properly.
So you only want to support Docker containers? Should dcli be compatible with podman, LXC containers? Maybe Kubernetes?
I don’t know how JVM detects whether it is running in a container or not, but detecing it with Cgroup v1 and Cgroup v…
I recommend setting an environment variable as you would when running systemd in a container or check whether you can do something in the environment or not.
You can also find this link in the mentioned topic:
opened 12:12AM - 21 Feb 24 UTC
closed 01:24AM - 21 Feb 24 UTC
status/0-triage
kind/feature
### Description
I’m the maintainer of the dcli package which is intended to rep… lace bash using dart as an alternate scripting language.
https://pub.dev/packages/dcli
DCli has a feature that allow users to run privileged code by running sudo.
When running in a docker container we don’t need to use sudo (it doesn’t exist anyway).
As such, dcli needs a generic way to detect if it is running in a docker container.
Previously we have used the presence of ‘/.dockerenv’ to determine if we are in a docker container.
It however looks like that when running under buildx the .dockerenv file no longer exists.
As such I’m looking of a reliable way of detecting if dcli is running in a docker container.
Is there any official/correct way to determine if you are running in a docker container?
You can see from this thread there are a few suggested ways but these are all rather hacky.
https://forums.docker.com/t/detect-you-are-running-in-a-docker-container-buildx/139673
We need a reliable, stable and documented method to determine if we are running in a container.
For the likes of dcli, relying of an environment variable being injected (or any other user action) isn't sustainable as the user is unlikely to know the requirement and dcli needs to behave correctly 'out of the box'.
The user in the other topic opened this issue as I suggested, but I suspected what the answer would be. The issue was closed and a project member basically suggested what I did.