Skip to content

Commit 69031d8

Browse files
alexjurkiewiczdschep
authored andcommitted
Add custom Dockerfile workflow example (#228)
As per #101.
1 parent 3e09806 commit 69031d8

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,57 @@ For usage of `dockerizePip` on Windows do Step 1 only if running serverless on w
266266
1. [Installing the Docker client on Windows Subsystem for Linux (Ubuntu)](https://medium.com/@sebagomez/installing-the-docker-client-on-ubuntus-windows-subsystem-for-linux-612b392a44c4)
267267
268268
269+
## Native Code Dependencies During Build
270+
271+
Some Python packages require extra OS dependencies to build successfully. To deal with this, replace the default image (`lambci/lambda:python3.6`) with a `Dockerfile` like:
272+
273+
```dockerfile
274+
# AWS Lambda execution environment is based on Amazon Linux 1
275+
FROM amazonlinux:1
276+
277+
# Install Python 3.6
278+
RUN yum -y install python36 python36-pip
279+
280+
# Install your dependencies
281+
RUN curl -s https://bootstrap.pypa.io/get-pip.py | python3
282+
RUN yum -y install python3-devel mysql-devel gcc
283+
284+
# Set the same WORKDIR as default image
285+
RUN mkdir /var/task
286+
WORKDIR /var/task
287+
```
288+
289+
Then update your `serverless.yml`:
290+
291+
```yaml
292+
custom:
293+
pythonRequirements:
294+
dockerFile: Dockerfile
295+
```
296+
297+
## Native Code Dependencies During Runtime
298+
299+
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:
300+
301+
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:
302+
303+
1. Extract the library:
304+
```bash
305+
docker run --rm -v "$(pwd):/var/task" sls-py-reqs-custom cp -v /usr/lib64/mysql57/libmysqlclient.so.1020 .
306+
```
307+
(If you get the error `Unable to find image 'sls-py-reqs-custom:latest' locally`, run `sls package` to build the image.)
308+
2. Commit to your repo:
309+
```bash
310+
git add libmysqlclient.so.1020
311+
git commit -m "Add libmysqlclient.so.1020"
312+
```
313+
3. Verify the library gets included in your package:
314+
```bash
315+
sls package
316+
zipinfo .serverless/xxx.zip
317+
```
318+
(If you can't see the library, you might need to adjust your package include/exclude configuration in `serverless.yml`.)
319+
269320
## Contributors
270321
* [@dschep](https://github.com/dschep) - Lead developer & maintainer
271322
* [@azurelogic](https://github.com/azurelogic) - logging & documentation fixes

0 commit comments

Comments
 (0)