Skip to content

Commit ec1ab51

Browse files
authored
Merge pull request #10 from myrmex-org/amazonlinux2
Added Amazon Linux 2 image to support node 10.16
2 parents e9fdd85 + a05a7b5 commit ec1ab51

File tree

11 files changed

+190
-92
lines changed

11 files changed

+190
-92
lines changed

Dockerfile

Lines changed: 0 additions & 74 deletions
This file was deleted.

README.md

100644100755
Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ module may not be compatible with the Amazon Lambda execution environment.
88
A common solution to this problem is to build the package on an EC2 instance using an Amazon Linux AMI and then to
99
deploy it in Amazon Lambda. Building serverless applications, it is ironic to be obliged to use an EC2 server to deploy code.
1010

11-
This docker image is based on the [Amazon Linux 1](https://hub.docker.com/_/amazonlinux/) image and contains `gcc`,
12-
`python 2.7`, `python 3.6`, `python 3.7`, `pip`, `node 6.10`, `node 8.10` to create packages for Amazon Lambda.
11+
This docker image is based on the [Amazon Linux](https://hub.docker.com/_/amazonlinux/) image and contains `gcc`.
1312

14-
Using the docker image `myrmex/lambda-packager`, you can avoid errors like these during execution in Amazon Lambda:
13+
The docker image `myrmex/lambda-packager:1` contains `python 2.7`, `python 3.6`, `python 3.7`, `pip`, `node 6.10`, `node 8.10` to create packages for Amazon Lambda.
14+
15+
The docker image for `myrmex/lambda-packager:2` contains `node 10.16` to create packages for Amazon Lambda.
16+
17+
Using the docker images from `myrmex/lambda-packager`, you can avoid errors like these during execution in Amazon Lambda:
1518

1619
```
1720
/var/lang/lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /var/task/node_modules/bcrypt/lib/binding/bcrypt_lib.node)
@@ -24,18 +27,24 @@ Module version mismatch. Expected 46, got 48.
2427
## Usage
2528

2629
You can use a docker volume to mount the code of the Lambda in a container. The directory where `npm install` or
27-
`pip install` is executed inside the container is `/data`. By default, the installation will be performed for node
28-
6.10.
30+
`pip install` is executed inside the container is `/data`. By default, the installation will be performed for node.
2931

3032
```bash
31-
docker run -v `pwd`:/data myrmex/lambda-packager
33+
docker run -v `pwd`:/data myrmex/lambda-packager:1
3234
```
3335

3436
The image does not create the zip archive for your. It only installs the dependencies in an environment compatible with
3537
Lambda. You will still have to zip the result and deploy it in Amazon Lambda.
3638

3739
For a node package, take care that `node_module` does not already exist before running the command.
3840

41+
## Building images locally
42+
43+
```bash
44+
docker build -t myrmex/lambda-packager:1 -f al1.Dockerfile .
45+
docker build -t myrmex/lambda-packager:2 -f al2.Dockerfile .
46+
```
47+
3948
### Managing permissions
4049

4150
The user account that performs `npm install` or `pip install` inside the container may have a `uid/gid` that differs from the
@@ -51,37 +60,48 @@ docker run -e HOST_UID=`id -u` -e HOST_GID=`id -g` -v `pwd`:/data myrmex/lambda-
5160

5261
The `RUNTIME` environment variable allows to choose the runtime.
5362

63+
### Node 10.16
64+
65+
Node 10.16 is the default runtime for `Amazon Linux 2` and does not require any special configuration.
66+
67+
```bash
68+
docker run -v `pwd`:/data myrmex/lambda-packager:2
69+
```
5470

5571
#### Node 8.10
5672

57-
Node 8.10 is the default runtime and does not require any special configuration.
73+
Node 8.10 is the default runtime for `Amazon Linux` and does not require any special configuration.
74+
75+
```bash
76+
docker run -v `pwd`:/data myrmex/lambda-packager:1
77+
```
5878

5979
#### Node 6.10
6080

6181
```bash
62-
docker run -e RUNTIME=node6 -v `pwd`:/data myrmex/lambda-packager
82+
docker run -e RUNTIME=node6 -v `pwd`:/data myrmex/lambda-packager:1
6383
```
6484

6585
#### Python 3.7
6686

6787
Python 3.7 is the default Python 3 runtime and accepts the values `python3` or `python3.7`
6888

6989
```bash
70-
docker run -e RUNTIME=python3.7 -v `pwd`:/data myrmex/lambda-packager
90+
docker run -e RUNTIME=python3.7 -v `pwd`:/data myrmex/lambda-packager:1
7191
```
7292

7393
#### Python 3.6
7494

7595
```bash
76-
docker run -e RUNTIME=python3.6 -v `pwd`:/data myrmex/lambda-packager
96+
docker run -e RUNTIME=python3.6 -v `pwd`:/data myrmex/lambda-packager:1
7797
```
7898

7999
#### Python 2.7
80100

81101
Accepts the values `python` or `python2`.
82102

83103
```bash
84-
docker run -e RUNTIME=python -v `pwd`:/data myrmex/lambda-packager
104+
docker run -e RUNTIME=python -v `pwd`:/data myrmex/lambda-packager:1
85105
```
86106

87107
### Default command

al1.Dockerfile

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
FROM amazonlinux:1
2+
3+
MAINTAINER Alexis N-o "[email protected]"
4+
5+
ENV DEFAULT_USER=myrmex
6+
ENV NODE_VERSION_6 6.10.3
7+
ENV NODE_VERSION_8 8.10.0
8+
ENV PYTHON_VERSION_3_6 3.6.8
9+
ENV PYTHON_VERSION_3_7 3.7.3
10+
11+
# Install gcc add utilities to manage users and permissions
12+
RUN yum install -y gcc-c++ util-linux shadow-utils zlib-devel openssl-devel libffi-devel
13+
14+
# Add scripts to install node and python versions
15+
COPY /install-node.sh /install-node.sh
16+
COPY /install-python.sh /install-python.sh
17+
18+
# Install node v6 and node v8 as commands "node6" and "node8"
19+
# Command "node" defaults to v8
20+
RUN /install-node.sh ${NODE_VERSION_6} &&\
21+
/install-node.sh ${NODE_VERSION_8}
22+
23+
# Install python 3.6 and python 3.7 including pip, python 2.7 is already available
24+
# Command "python3" defaults to 3.7
25+
RUN curl -O https://bootstrap.pypa.io/get-pip.py &&\
26+
python get-pip.py &&\
27+
/install-python.sh ${PYTHON_VERSION_3_6} &&\
28+
/install-python.sh ${PYTHON_VERSION_3_7}
29+
30+
# Add a script to modify the UID / GID for the default user if needed
31+
COPY /usr/local/bin/change-uid /usr/local/bin/change-uid
32+
RUN chmod +x /usr/local/bin/change-uid
33+
34+
# Add a non root user
35+
RUN useradd $DEFAULT_USER -m -d /home/$DEFAULT_USER/ -s /bin/bash
36+
37+
# Create working directory
38+
RUN mkdir /data && chown $DEFAULT_USER:$DEFAULT_USER /data
39+
WORKDIR /data
40+
41+
# Add entrypoint
42+
COPY /al1.entrypoint.sh /entrypoint.sh
43+
RUN chmod +x /entrypoint.sh
44+
ENTRYPOINT ["/entrypoint.sh"]
45+
46+
# Add default command
47+
COPY /cmd.sh /cmd.sh
48+
RUN chmod +x /cmd.sh
49+
CMD ["/cmd.sh"]
File renamed without changes.

al2.Dockerfile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
FROM amazonlinux:2
2+
3+
MAINTAINER Alexis N-o "[email protected]"
4+
5+
ENV DEFAULT_USER=myrmex
6+
ENV NODE_VERSION_10 10.16.3
7+
8+
# Install gcc add utilities to manage users and permissions
9+
RUN yum install -y gcc-c++ util-linux shadow-utils zlib-devel openssl-devel libffi-devel tar
10+
11+
# Add a script to install node versions
12+
COPY /install-node.sh /install-node.sh
13+
14+
# Install node v10
15+
# Command "node" defaults to v10
16+
RUN /install-node.sh ${NODE_VERSION_10}
17+
18+
# Add a script to modify the UID / GID for the default user if needed
19+
COPY /usr/local/bin/change-uid /usr/local/bin/change-uid
20+
RUN chmod +x /usr/local/bin/change-uid
21+
22+
# Add a non root user
23+
RUN useradd $DEFAULT_USER -m -d /home/$DEFAULT_USER/ -s /bin/bash
24+
25+
# Create working directory
26+
RUN mkdir /data && chown $DEFAULT_USER:$DEFAULT_USER /data
27+
WORKDIR /data
28+
29+
# Add entrypoint
30+
COPY /al2.entrypoint.sh /entrypoint.sh
31+
RUN chmod +x /entrypoint.sh
32+
ENTRYPOINT ["/entrypoint.sh"]
33+
34+
# Add default command
35+
COPY /cmd.sh /cmd.sh
36+
RUN chmod +x /cmd.sh
37+
CMD ["/cmd.sh"]

al2.entrypoint.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
set -e
3+
4+
if [ `whoami` == "root" ]; then
5+
echo "Using node v10 runtime"
6+
7+
if [ "$HOST_UID" != "" ]; then
8+
# If the user is root and a HOST_UID environment variable exists,
9+
# we change the myrmex user UID and execute the command as the myrmex user
10+
if [ "$HOST_UID" != `id -u $DEFAULT_USER` ]; then
11+
echo "Changing $DEFAULT_USER UID to $HOST_UID"
12+
change-uid $HOST_UID $HOST_GID >/dev/null
13+
fi
14+
fi
15+
16+
fi
17+
18+
exec $@

cmd.sh

100644100755
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
#!/bin/bash
22
set -e
33

4-
if [ "$RUNTIME" == "python" ] || [ "$RUNTIME" == "python2" ] || [ "$RUNTIME" == "python2.7" ]; then
5-
su $DEFAULT_USER -c "pip install --requirement requirements.txt --upgrade --target ."
6-
elif [ "$RUNTIME" == "python3" ] || [ "$RUNTIME" == "python3.6" ] || [ "$RUNTIME" == "python3.7" ]; then
7-
su $DEFAULT_USER -c "pip3 install --requirement requirements.txt --upgrade --target ."
8-
else
9-
su $DEFAULT_USER -c "npm install --production"
10-
fi
4+
su $DEFAULT_USER -c "npm install --production"

examples/node/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@myrmex/node-packager-example",
3+
"version": "1.0.0",
4+
"description": "A module to test Myrmex Lambda packager",
5+
"main": "index.js",
6+
"dependencies": {
7+
"pebo": "0.0.2"
8+
},
9+
"devDependencies": {},
10+
"scripts": {
11+
"test": "echo \"Error: no test specified\" && exit 1"
12+
},
13+
"author": "",
14+
"license": "ISC"
15+
}

install-node.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
set -e
3+
4+
NODE_VERSION=$1
5+
NODE_SHORT_VERSION="$(cut -d'.' -f1 <<<${NODE_VERSION})"
6+
7+
# Install node version
8+
cd /opt
9+
curl -O https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.gz
10+
tar xvzf node-v${NODE_VERSION}-linux-x64.tar.gz
11+
rm node-v${NODE_VERSION}-linux-x64.tar.gz
12+
ln -s /opt/node-v${NODE_VERSION}-linux-x64/bin/node /usr/local/bin/node${NODE_SHORT_VERSION}
13+
14+
# Set as default node version
15+
ln -fs /opt/node-v${NODE_VERSION}-linux-x64/bin/node /usr/local/bin/node
16+
ln -fs /opt/node-v${NODE_VERSION}-linux-x64/bin/npm /usr/local/bin/npm
17+
18+
# Update npm to version 4
19+
/opt/node-v${NODE_VERSION}-linux-x64/bin/npm install -g npm@4

install-python.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
set -e
3+
4+
PYTHON_VERSION=$1
5+
PYTHON_MAJOR_VERSION="$(cut -d'.' -f1 <<<${PYTHON_VERSION})"
6+
PYTHON_MINOR_VERSION="$(cut -d'.' -f1-2 <<<${PYTHON_VERSION})"
7+
8+
# Install python version
9+
curl -O https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz
10+
tar zxvf Python-${PYTHON_VERSION}.tgz
11+
cd Python-${PYTHON_VERSION}
12+
./configure --prefix=/opt/python-${PYTHON_VERSION}
13+
make
14+
make install
15+
ln -s /opt/python-${PYTHON_VERSION}/bin/python${PYTHON_MAJOR_VERSION} /usr/bin/python${PYTHON_MINOR_VERSION}
16+
ln -s /opt/python-${PYTHON_VERSION}/bin/pip${PYTHON_MAJOR_VERSION} /usr/bin/pip${PYTHON_MINOR_VERSION}
17+
18+
# Set as default python major version
19+
ln -fs /opt/python-${PYTHON_VERSION}/bin/python${PYTHON_MAJOR_VERSION} /usr/bin/python${PYTHON_MAJOR_VERSION}
20+
ln -fs /opt/python-${PYTHON_VERSION}/bin/pip${PYTHON_MAJOR_VERSION} /usr/bin/pip${PYTHON_MAJOR_VERSION}

0 commit comments

Comments
 (0)