Update Docker detection to be cgroup based

This ensures a more stable detection strategy since .dockerenv might be not around for the long term.
This commit is contained in:
Philipp Muens 2017-01-09 11:15:12 -08:00
parent ce8bbc4885
commit 2d2ba4521d
2 changed files with 10 additions and 8 deletions

View File

@ -303,9 +303,9 @@ class Utils {
// wrap in try catch to make sure that missing permissions won't break anything
let isDockerContainer = false;
try {
const dockerenvFile = '.dockerenv';
const dockerenvFilePath = path.join(this.getRootPath(), dockerenvFile);
isDockerContainer = this.fileExistsSync(dockerenvFilePath) || false;
const cgroupFilePath = path.join(this.getRootPath(), 'proc', '1', 'cgroup');
const cgroupFileContent = fs.readFileSync(cgroupFilePath).toString();
isDockerContainer = !!cgroupFileContent.match(/docker/);
} catch (exception) {
// do nothing
}

View File

@ -374,13 +374,15 @@ describe('Utils', () => {
describe('when detecting Docker containers', () => {
let rootPathStub;
let rootDirPath;
let dockerenvFilePath;
let cgroupFilePath;
beforeEach(() => {
rootDirPath = testUtils.getTmpDirPath();
dockerenvFilePath = path.join(rootDirPath, '.dockerenv');
cgroupFilePath = path.join(rootDirPath, 'proc', '1', 'cgroup');
const cgroupFileContent = '6:devices:/docker/3601745b3bd54d9780436faa5f0e4f72';
fse.mkdirsSync(rootDirPath);
fse.ensureFileSync(dockerenvFilePath);
fse.ensureFileSync(cgroupFilePath);
fs.writeFileSync(cgroupFilePath, cgroupFileContent);
rootPathStub = sinon.stub(utils, 'getRootPath').returns(rootDirPath);
});
@ -404,8 +406,8 @@ describe('Utils', () => {
})
);
it('should not throw error if .dockerenv file is not accessible', () => {
fs.chmodSync(dockerenvFilePath, '000');
it('should not throw error if cgroup file is not accessible', () => {
fs.chmodSync(cgroupFilePath, '000');
fs.chmodSync(rootDirPath, '400');
utils.logStat(serverless).then(() => {