Java应用程序如何知道它正在Docker容器中运行 [英] How does Java application know it is running within a Docker container

查看:448
本文介绍了Java应用程序如何知道它正在Docker容器中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的应用程序编写了一个DockerFile,主要是为了使其能够在NAS机器上运行(通过Docker).Web界面允许用户遍历文件系统树以查找音乐文件,但是使用Docker时,文件系统树无关紧要,除了/Music卷是用户在NAS上实际的Music文件夹的挂载点.

I have written a DockerFile for my application primarily to allow it to be run within a NAS machine (via Docker). The web interface allows the user to traverse the filesystem tree looking for Music files, but when using Docker the filesystem tree is irrelevant except for the /Music volume which is the mount point for the users actual Music folder on the NAS.

所以我只想显示/Music文件夹而不是整个文件系统树,并且这样做应用程序需要知道它实际上是在Docker而不是实际的本机Linux OS中运行的.

So I only want to display the /Music folder instead of the whole filesystem tree and to do that the application needs to be aware it is actually running within a Docker rather than an actual native Linux OS.

应用程序用Java编写的应用程序知道它在docker中的正确方法是什么.

What is the correct way for the application to know it is in docker, the application is written in Java.

推荐答案

解决方案

只需通过/proc/1/cgroup 检查初始化过程的控制组.

Check control group of the init process simply by /proc/1/cgroup .

  • 如果正常启动,则所有层次结构都具有/
  • 如果它是从docker容器启动的,则它们具有/docker/< container_id> 值.

在docker中运行时/proc/1/cgroup 的值类似于:

When running inside docker /proc/1/cgroup has values similar to :

11:perf_event:/docker/897df2a033d6ab07c357c1ac1f75741bd16474487de83c6d4d98518e5ef52249
10:memory:/docker/897df2a033d6ab07c357c1ac1f75741bd16474487de83c6d4d98518e5ef52249
9:cpuset:/docker/897df2a033d6ab07c357c1ac1f75741bd16474487de83c6d4d98518e5ef52249
8:net_cls,net_prio:/docker/897df2a033d6ab07c357c1ac1f75741bd16474487de83c6d4d98518e5ef52249
7:pids:/docker/897df2a033d6ab07c357c1ac1f75741bd16474487de83c6d4d98518e5ef52249
6:cpu,cpuacct:/docker/897df2a033d6ab07c357c1ac1f75741bd16474487de83c6d4d98518e5ef52249
5:blkio:/docker/897df2a033d6ab07c357c1ac1f75741bd16474487de83c6d4d98518e5ef52249
4:freezer:/docker/897df2a033d6ab07c357c1ac1f75741bd16474487de83c6d4d98518e5ef52249
3:hugetlb:/docker/897df2a033d6ab07c357c1ac1f75741bd16474487de83c6d4d98518e5ef52249
2:devices:/docker/897df2a033d6ab07c357c1ac1f75741bd16474487de83c6d4d98518e5ef52249
1:name=systemd:/docker/897df2a033d6ab07c357c1ac1f75741bd16474487de83c6d4d98518e5ef52249


注意:正如@JanisKirsteins告诉我的那样,如果您在Amazon ec2中运行应用程序,则可能希望将条件更改为 line.contains("/ecs")反而.因为在/proc/1/cgroups 中,您会发现类似于以下模式:/ecs/< uuid>/< uuid>


Note: As @JanisKirsteins informed me, If you run your application in amazon ec2 you might want to change the condition to line.contains("/ecs") instead. because in /proc/1/cgroups you will find pattern similar to: /ecs/<uuid>/<uuid>

在Java中

public static Boolean isRunningInsideDocker() {

        try (Stream < String > stream =
            Files.lines(Paths.get("/proc/1/cgroup"))) {
            return stream.anyMatch(line -> line.contains("/docker"));
        } catch (IOException e) {
            return false;
        }
    }

实时代码检查


更多信息

这篇关于Java应用程序如何知道它正在Docker容器中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆