Skip to content

Allow arbitrary extra args to be passed to docker build and dockerized pip install steps #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 24, 2019
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class ServerlessPythonRequirements {
dockerImage: null,
dockerFile: null,
dockerEnv: false,
dockerBuildCmdExtraArgs: [],
dockerRunCmdExtraArgs: [],
useStaticCache: false,
useDownloadCache: false,
cacheLocation: false,
Expand Down
14 changes: 12 additions & 2 deletions lib/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 11 additions & 1 deletion lib/pip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}

Expand Down