Skip to content

Commit f39e166

Browse files
authored
Allow arbitrary extra args to be passed to docker build and dockerized pip install steps (#358)
Allow arbitrary extra args to be passed to docker build and dockerized pip install steps
2 parents 8a6c1a9 + e92624b commit f39e166

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,18 @@ custom:
251251
- --compile
252252
```
253253

254+
### Extra Docker arguments
255+
256+
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:
257+
258+
```yaml
259+
custom:
260+
pythonRequirements:
261+
dockerizePip: true
262+
dockerBuildCmdExtraArgs: ["--build-arg", "MY_GREAT_ARG=123"]
263+
dockerRunCmdExtraArgs: ["-v", "${env:PWD}:/my-app"]
264+
```
265+
254266

255267
### Customize requirements file name
256268
[Some `pip` workflows involve using requirements files not named

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class ServerlessPythonRequirements {
4848
dockerImage: null,
4949
dockerFile: null,
5050
dockerEnv: false,
51+
dockerBuildCmdExtraArgs: [],
52+
dockerRunCmdExtraArgs: [],
5153
useStaticCache: false,
5254
useDownloadCache: false,
5355
cacheLocation: false,

lib/docker.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,21 @@ function dockerCommand(options) {
2525
/**
2626
* Build the custom Docker image
2727
* @param {string} dockerFile
28+
* @param {string[]} extraArgs
2829
* @return {string} The name of the built docker image.
2930
*/
30-
function buildImage(dockerFile) {
31+
function buildImage(dockerFile, extraArgs) {
3132
const imageName = 'sls-py-reqs-custom';
32-
const options = ['build', '-f', dockerFile, '-t', imageName, '.'];
33+
const options = ['build', '-f', dockerFile, '-t', imageName];
34+
35+
if (Array.isArray(extraArgs)) {
36+
options.push(...extraArgs);
37+
} else {
38+
throw new Error('dockerRunCmdExtraArgs option must be an array');
39+
}
40+
41+
options.push('.');
42+
3343
dockerCommand(options);
3444
return imageName;
3545
}

lib/pip.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ function installRequirements(targetFolder, serverless, options) {
180180
serverless.cli.log(
181181
`Building custom docker image from ${options.dockerFile}...`
182182
);
183-
dockerImage = buildImage(options.dockerFile);
183+
dockerImage = buildImage(
184+
options.dockerFile,
185+
options.dockerBuildCmdExtraArgs
186+
);
184187
} else {
185188
dockerImage = options.dockerImage;
186189
}
@@ -259,6 +262,13 @@ function installRequirements(targetFolder, serverless, options) {
259262
// Use same user so --cache-dir works
260263
dockerCmd.push('-u', getDockerUid(bindPath));
261264
}
265+
266+
if (Array.isArray(options.dockerRunCmdExtraArgs)) {
267+
dockerCmd.push(...options.dockerRunCmdExtraArgs);
268+
} else {
269+
throw new Error('dockerRunCmdExtraArgs option must be an array');
270+
}
271+
262272
dockerCmd.push(dockerImage);
263273
}
264274

0 commit comments

Comments
 (0)