Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions lib/docker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { spawnSync } = require('child_process');
const isWsl = require('is-wsl');
const fse = require('fs-extra');
const path = require('path');

/**
* Helper function to run a docker command
Expand Down Expand Up @@ -32,35 +34,60 @@ function buildImage(dockerFile) {
return imageName;
}

/**
* Find a file that exists on all projects so we can test if Docker can see it too
* @param {string} servicePath
* @return {string} file name
*/
function findTestFile(servicePath) {
if (fse.pathExistsSync(path.join(servicePath, 'serverless.yml'))) {
return 'serverless.yml';
}
if (fse.pathExistsSync(path.join(servicePath, 'serverless.yaml'))) {
return 'serverless.yaml';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did serverless drop support for serverless.json?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not aware that's an option. Let me add that.

}
if (fse.pathExistsSync(path.join(servicePath, 'serverless.json'))) {
return 'serverless.json';
}
throw new Error(
'Unable to find serverless.yml or serverless.yaml or serverless.json for getBindPath()'
);
}

/**
* Test bind path to make sure it's working
* @param {string} bindPath
* @return {boolean}
*/
function tryBindPath(bindPath) {
function tryBindPath(serverless, bindPath, testFile) {
const options = [
'run',
'--rm',
'-v',
`${bindPath}:/test`,
'alpine',
'ls',
'/test/serverless.yml'
`/test/${testFile}`
];
try {
const ps = dockerCommand(options);
return ps.stdout.trim() === '/test/serverless.yml';
if (process.env.SLS_DEBUG) {
serverless.cli.log(`Trying bindPath ${bindPath} (${options})`);
serverless.cli.log(ps.stdout.trim());
}
return ps.stdout.trim() === `/test/${testFile}`;
} catch (err) {
return false;
}
}

/**
* Get bind path depending on os platform
* @param {object} serverless
* @param {string} servicePath
* @return {string} The bind path.
*/
function getBindPath(servicePath) {
function getBindPath(serverless, servicePath) {
// Determine bind path
if (process.platform !== 'win32' && !isWsl) {
return servicePath;
Expand Down Expand Up @@ -100,9 +127,11 @@ function getBindPath(servicePath) {
bindPaths.push(`/mnt/${drive.toUpperCase()}/${path}`);
bindPaths.push(`${drive.toUpperCase()}:/${path}`);

const testFile = findTestFile(servicePath);

for (let i = 0; i < bindPaths.length; i++) {
const bindPath = bindPaths[i];
if (tryBindPath(bindPath)) {
if (tryBindPath(serverless, bindPath, testFile)) {
return bindPath;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/pip.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function installRequirements(
serverless.cli.log(`Docker Image: ${dockerImage}`);

// Prepare bind path depending on os platform
const bindPath = getBindPath(servicePath);
const bindPath = getBindPath(serverless, servicePath);

cmdOptions = ['run', '--rm', '-v', `"${bindPath}:/var/task:z"`];
if (options.dockerSsh) {
Expand Down