From 32df7d6c662d075b9d7ff259c7fce94293a53441 Mon Sep 17 00:00:00 2001 From: Heri Sim Date: Thu, 14 Dec 2017 19:16:03 +0700 Subject: [PATCH] Fix Windows support for dockerizePip Fix docker volume bind path format, in order to support `Docker for Windows` when using sls/nodejs/docker from Powershell or from Windows Subsystem for Linux (WSL). --- lib/pip.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/pip.js b/lib/pip.js index e19d8fb4..58f4cb48 100644 --- a/lib/pip.js +++ b/lib/pip.js @@ -50,9 +50,29 @@ function installRequirements() { this.serverless.cli.log(`Docker Image: ${this.options.dockerImage}`); + // Check docker server os type from 'docker version' + let volPath; + options = [ + 'version', '--format', '{{with .Server}}{{.Os}}{{end}}' + ]; + const ps = spawnSync(cmd, options, {'timeout': 2000, 'encoding': 'utf-8'}); + if (ps.error) { + if (ps.error.code === 'ENOENT') { + throw new Error('docker not found! Please install it.'); + } + throw new Error(ps.error); + } + if (ps.status !== 0) { + throw new Error(ps.stderr); + } else if (ps.stdout.trim() === 'windows') { + volPath = this.servicePath.replace(/\\/g, '/').replace(/^\/mnt\//, '/'); + } else { + volPath = this.servicePath; + } + options = [ 'run', '--rm', - '-v', `${this.servicePath}:/var/task:z`, + '-v', `${volPath}:/var/task:z`, ]; if (process.platform === 'linux') options.push('-u', `${process.getuid()}:${process.getgid()}`);