Current Location: Home> Latest Articles> Alternative usage to determine whether it is running in Docker (combined with php_uname())

Alternative usage to determine whether it is running in Docker (combined with php_uname())

M66 2025-06-02

When developing and deploying PHP applications, knowing whether your PHP environment runs in a Docker container can help you make targeted optimizations and tweaks. Usually, a containerized environment provides an isolated runtime environment, so in some cases we need to check whether PHP is running in a Docker container.

This article will introduce how to use PHP's php_uname() function to determine whether your PHP is running in a Docker container.

1. What is the php_uname() function?

php_uname() is a built-in function of PHP, which is mainly used to return system information, similar to the uname command in Unix systems. It will return the operating system name, host name, version and other information of the current system.

The basic usage of the function is as follows:

 php_uname();

This will return a string similar to the following (depending on the operating system):

 Linux myhostname 4.15.0-45-generic #48-Ubuntu SMP Thu Jan 17 16:24:23 UTC 2019 x86_64

This string contains information such as operating system type, host name, kernel version, etc.

2. How to determine whether PHP is running in a Docker container?

Docker containers usually use specific kernel versions, and the /proc/1/cgroup file in the container will contain some Docker-related information. Therefore, it is not easy to directly judge whether PHP is in Docker using php_uname() . But we can judge by combining the system files.

step:

  1. View php_uname() output

First, use php_uname() to output the operating system information:

 echo php_uname();

You will get an output similar to the following:

 Linux myhostname 5.4.0-100-generic #113-Ubuntu SMP Wed Nov 25 13:02:28 UTC 2020 x86_64

This does not directly tell us whether PHP is running in Docker. For further verification, we need to check the characteristics of the Docker container.

  1. Combined with /proc/1/cgroup file

The file system of a Docker container usually contains the /proc/1/cgroup file, and in this file we can find information related to the Docker container. By detecting the content of the file, we can infer whether it is running in the Docker environment.

You can use the following code to check:

 function isRunningInDocker() {
    // Check if it exists /proc/1/cgroup document
    if (is_readable('/proc/1/cgroup')) {
        $cgroup = file_get_contents('/proc/1/cgroup');
        
        // Determine whether it contains docker The logo
        if (strpos($cgroup, 'docker') !== false) {
            return true; // Running in Docker middle
        }
    }
    return false; // 未Running in Docker middle
}

if (isRunningInDocker()) {
    echo "PHP In progress Docker 容器middle运行!";
} else {
    echo "PHP 没有Running in Docker 容器middle。";
}

explain:

  • The code checks the /proc/1/cgroup file and looks for whether there are docker- related tags.

  • If the file contains the Docker identification string, you can make sure that PHP is running in the Docker container.

3. Complete code example

 <?php

// use php_uname() Get operating system information
echo 'Operating system information: ' . php_uname() . "\n";

// Check if it is Docker 容器middle运行
function isRunningInDocker() {
    if (is_readable('/proc/1/cgroup')) {
        $cgroup = file_get_contents('/proc/1/cgroup');
        if (strpos($cgroup, 'docker') !== false) {
            return true; // yes Docker environment
        }
    }
    return false; // 不yes Docker environment
}

if (isRunningInDocker()) {
    echo "PHP In progress Docker 容器middle运行!";
} else {
    echo "PHP 没有Running in Docker 容器middle。";
}
?>

4. Conclusion

Through the above method, you can use php_uname() to obtain operating system information and combine it with checking the Docker-related tags in the /proc/1/cgroup file to determine whether PHP is running in the Docker container. This is a neat and effective way to help you perform specific actions or tweaks in a containerized environment.

Hope this article helps you! If you have more questions, please visit our website.