mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
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:
parent
ce8bbc4885
commit
2d2ba4521d
@ -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
|
||||
}
|
||||
|
||||
@ -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(() => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user