diff --git a/README.md b/README.md index 4f34a38f..690bb46b 100644 --- a/README.md +++ b/README.md @@ -270,6 +270,18 @@ custom: - --compile ``` +### Extra Docker arguments + +You can specify extra arguments to be passed to [docker build](https://docs.docker.com/engine/reference/commandline/build/) during the build step, and [docker run](https://docs.docker.com/engine/reference/run/) during the dockerized pip install step: + +```yaml +custom: + pythonRequirements: + dockerizePip: true + dockerBuildCmdExtraArgs: ["--build-arg", "MY_GREAT_ARG=123"] + dockerRunCmdExtraArgs: ["-v", "${env:PWD}:/my-app"] +``` + ### Customize requirements file name [Some `pip` workflows involve using requirements files not named diff --git a/index.js b/index.js index b5200285..6f98bcb4 100644 --- a/index.js +++ b/index.js @@ -48,6 +48,8 @@ class ServerlessPythonRequirements { dockerImage: null, dockerFile: null, dockerEnv: false, + dockerBuildCmdExtraArgs: [], + dockerRunCmdExtraArgs: [], useStaticCache: false, useDownloadCache: false, cacheLocation: false, diff --git a/lib/docker.js b/lib/docker.js index db2e81b5..46bbe028 100644 --- a/lib/docker.js +++ b/lib/docker.js @@ -25,11 +25,21 @@ function dockerCommand(options) { /** * Build the custom Docker image * @param {string} dockerFile + * @param {string[]} extraArgs * @return {string} The name of the built docker image. */ -function buildImage(dockerFile) { +function buildImage(dockerFile, extraArgs) { const imageName = 'sls-py-reqs-custom'; - const options = ['build', '-f', dockerFile, '-t', imageName, '.']; + const options = ['build', '-f', dockerFile, '-t', imageName]; + + if (Array.isArray(extraArgs)) { + options.push(...extraArgs); + } else { + throw new Error('dockerRunCmdExtraArgs option must be an array'); + } + + options.push('.'); + dockerCommand(options); return imageName; } diff --git a/lib/pip.js b/lib/pip.js index 323a92fb..45c72059 100644 --- a/lib/pip.js +++ b/lib/pip.js @@ -180,7 +180,10 @@ function installRequirements(targetFolder, serverless, options) { serverless.cli.log( `Building custom docker image from ${options.dockerFile}...` ); - dockerImage = buildImage(options.dockerFile); + dockerImage = buildImage( + options.dockerFile, + options.dockerBuildCmdExtraArgs + ); } else { dockerImage = options.dockerImage; } @@ -259,6 +262,13 @@ function installRequirements(targetFolder, serverless, options) { // Use same user so --cache-dir works dockerCmd.push('-u', getDockerUid(bindPath)); } + + if (Array.isArray(options.dockerRunCmdExtraArgs)) { + dockerCmd.push(...options.dockerRunCmdExtraArgs); + } else { + throw new Error('dockerRunCmdExtraArgs option must be an array'); + } + dockerCmd.push(dockerImage); }