Skip to content

Commit 647313c

Browse files
authored
Add support to copy extra files from the Docker image to the pac… (serverless#366)
Add support to copy extra files from the Docker image to the package
2 parents b53dc6a + 083d46e commit 647313c

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

README.md

+12-13
Original file line numberDiff line numberDiff line change
@@ -402,26 +402,25 @@ custom:
402402
403403
## Native Code Dependencies During Runtime
404404
405-
Some Python packages require extra OS libraries (`*.so` files) at runtime. You need to manually include these files in the root directory of your Serverless package. The simplest way to do this is to commit the files to your repository:
405+
Some Python packages require extra OS libraries (`*.so` files) at runtime. You need to manually include these files in the root directory of your Serverless package. The simplest way to do this is to use the `dockerExtraFiles` option.
406406

407-
For instance, the `mysqlclient` package requires `libmysqlclient.so.1020`. If you use the Dockerfile from the previous section, you can extract this file from the builder Dockerfile:
407+
For instance, the `mysqlclient` package requires `libmysqlclient.so.1020`. If you use the Dockerfile from the previous section, add an item to the `dockerExtraFiles` option in your `serverless.yml`:
408408

409-
1. Extract the library:
410-
```bash
411-
docker run --rm -v "$(pwd):/var/task" sls-py-reqs-custom cp -v /usr/lib64/mysql57/libmysqlclient.so.1020 .
412-
```
413-
(If you get the error `Unable to find image 'sls-py-reqs-custom:latest' locally`, run `sls package` to build the image.)
414-
2. Commit to your repo:
415-
```bash
416-
git add libmysqlclient.so.1020
417-
git commit -m "Add libmysqlclient.so.1020"
409+
```yaml
410+
custom:
411+
pythonRequirements:
412+
dockerExtraFiles:
413+
- /usr/lib64/mysql57/libmysqlclient.so.1020
418414
```
419-
3. Verify the library gets included in your package:
415+
416+
Then verify the library gets included in your package:
417+
420418
```bash
421419
sls package
422420
zipinfo .serverless/xxx.zip
423421
```
424-
(If you can't see the library, you might need to adjust your package include/exclude configuration in `serverless.yml`.)
422+
423+
If you can't see the library, you might need to adjust your package include/exclude configuration in `serverless.yml`.
425424

426425
## Optimising packaging time
427426

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class ServerlessPythonRequirements {
5050
dockerEnv: false,
5151
dockerBuildCmdExtraArgs: [],
5252
dockerRunCmdExtraArgs: [],
53+
dockerExtraFiles: [],
5354
useStaticCache: false,
5455
useDownloadCache: false,
5556
cacheLocation: false,

lib/pip.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ function installRequirements(targetFolder, serverless, options) {
249249
`${process.getuid()}:${process.getgid()}`,
250250
'/var/task'
251251
]);
252+
} else {
253+
// Use same user so --cache-dir works
254+
dockerCmd.push('-u', getDockerUid(bindPath));
255+
}
256+
257+
for (let path of options.dockerExtraFiles) {
258+
pipCmds.push(['cp', path, '/var/task/']);
259+
}
260+
261+
if (process.platform === 'linux') {
252262
if (options.useDownloadCache) {
253263
// Set the ownership of the download cache dir back to user
254264
pipCmds.push([
@@ -258,9 +268,6 @@ function installRequirements(targetFolder, serverless, options) {
258268
dockerDownloadCacheDir
259269
]);
260270
}
261-
} else {
262-
// Use same user so --cache-dir works
263-
dockerCmd.push('-u', getDockerUid(bindPath));
264271
}
265272

266273
if (Array.isArray(options.dockerRunCmdExtraArgs)) {

0 commit comments

Comments
 (0)