-1

I am looking for a simple but reliable way to detect from shell (bash) if is running inside a container or not, regardless if that container happens to be run under docker, lxc, pods,...

I need this in order to perform to gather to collect output of systemctl status "*". Mainly I want to avoid a noisy failure like Failed to get D-Bus connection: Operation not permitted which will almost for sure happen inside containers.

The desired outcome is to return success and no output when inside containers and to run systemctl in the other cases.

There are several similar questions but I found none that works in this use case, most of the answers are few years old and they just do fail to deliver.

sorin
  • 161,544
  • 178
  • 535
  • 806
  • You can check this : https://stackoverflow.com/questions/20010199/how-to-determine-if-a-process-runs-inside-lxc-docker – Philippe Feb 01 '20 at 15:38
  • 2
    You're focusing on the wrong problem. You don't care if you are in a container; you care if it is meaningful to run `systemctl status "*"`. – chepner Feb 01 '20 at 15:41

3 Answers3

1

Following @chepner comment with which I 100% agree, what about the following KISS implementation ?

Have a look at man systemctl and the return names and values for is-system-running if you want to be more precise and make sure you avoid some more possible errors.

# Check if systemctl command is available at all
if which systemctl 2>&1 > /dev/null; then
    # Check that systemctl isn't offline
    if [ ! "$(systemctl is-system-running)" == "offline" ]; then
      systemctl status '*'
    fi
fi

Tested against:

  • my local ubuntu machine: status displayed
  • a docker container with systemd fully running: status displayed
  • a docker container with systemd installed but not running: empty output
  • a docker container without systemd installed: empty output
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
  • See [Why is testing "$?" to see if a command succeeded or not, an anti-pattern?](/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) – tripleee Feb 01 '20 at 18:27
  • 1
    @tripleee fixed. My initial intent was to test against several return status but it was not necessary in this case (and I should have captured the return on the same line according to best practice ;)) – Zeitounator Feb 01 '20 at 18:40
1

I'm using if [ -f /.dockerenv ]; then echo "inside container"; else echo "not in container"; fi for docker runtime.

Egor Stambakio
  • 17,836
  • 5
  • 33
  • 35
0

The message from "systemctl status" simply says that no systemd daemon is running. Some script have come to check that - cat /proc/1/cmdline | grep systemd - on a real machine. Otherwise not.

However, newer approches like Redhat "podman --systemd=true" will defeat that, and the systemctl commands work again. Same for my docker-systemctl-replacement script which works everywhere.

So I guess you have the wrong question here. The container may be fine for "systemctl" commands somehow.

Guido U. Draheim
  • 3,038
  • 1
  • 20
  • 19