Detail nodejs to create a minimum docker image
- 2020-06-12 11:33:17
- OfStack
Using docker to run the service, you can have 1 environment, exact control over the running resources of the service (cpu, memory), easy port and network Settings, and use mirror storage to manage and distribute code. More and more developers are choosing to run their services on docker.
Many nodejs users use the default node image when using docker. But don't you think it's too big? now
node:6.10.1
The mirror image volume has reached 666M, in fact, to achieve the same function, only 43.5ES11en is enough. Small size means lower resource consumption, faster download speeds, and smaller transmission bandwidth. Here's how to create a minimalist node image.
FROM: Sets the alpine base image
The lightest operating system currently available under docker is alpine, with one alpine being less than 5M. node default image depends on the base image of debian, debian:jessie volume has been 123M, so to reduce the size, the first thing is to switch from the base image to alpine.
FROM alpine:3.5
RUN: Set up the node user
adduser -D -u 1000 node
Setting the node user is optional. After adding a user to the run container, it is possible to specify the identity of the node user to run the service
RUN: Install the node compiler tool
apk add --no-cache \
libstdc++ \
&& apk add --no-cache --virtual .build-deps \
binutils-gold \
curl \
g++ \
gcc \
gnupg \
libgcc \
linux-headers \
make \
python
apk add --no-cache
Instead of using a local cache of the installation package database, get installation package information directly from a remote location. This eliminates the need to get the installation package database through apk update
apk add --virtual .build-deps
Package all packages for this installation into one virtual package named.build-ES56en. The advantage of this is that it can be passed
apk del .build-deps
The 1 key clears these packages
RUN: Import the node source package public key
for key in \
9554F04D7259F04124DE6B476D5A82AC7E37093B \
94AE36675C464D64BAFA68DD7434390BDBE9B9C5 \
FD3A5288F042B6850C66B31F09FE44734EB7990E \
71DCFD284A79C3B38668286BC97EC7A07EDE3FC1 \
DD8F2338BAE7501E3DD5AC78C273792F7D83545D \
B9AE9905FFD7803F25714661B63B535A4C206CA9 \
C4F0DFFF4E8C1A8236409D08E73BC641CC11F4C8 \
56730D5401028683275BD23C23EFEFE93C4CFFFE \
; do \
gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
done
These public keys will be used to verify the nodejs source file we downloaded from curl
RUN: Download and verify the node source file
curl -SLO "https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION.tar.xz" \
&& curl -SLO "https://nodejs.org/dist/v$NODE_VERSION/SHASUMS256.txt.asc" \
&& gpg --batch --decrypt --output SHASUMS256.txt SHASUMS256.txt.asc \
&& grep " node-v$NODE_VERSION.tar.xz\$" SHASUMS256.txt | sha256sum -c -
$NODE_VERSION:
node version, as 6.10.1
RUN: Compile and install node
tar -xf "node-v$NODE_VERSION.tar.xz" \
&& cd "node-v$NODE_VERSION" \
&& ./configure \
&& make -j$(getconf _NPROCESSORS_ONLN) \
&& make install
If npm is not required, you can replace the third behavior
&& ./configure --without-npm
$NODE_VERSION
: refers to node version, as 6.10.1
RUN: clean up
apk del .build-deps \
&& cd .. \
&& rm -Rf "node-v$NODE_VERSION" \
&& rm "node-v$NODE_VERSION.tar.xz" SHASUMS256.txt.asc SHASUMS256.txt
$NODE_VERSION:
node version, as 6.10.1
CMD: Set the mirror entry to node
CMD [ "node" ]
The above steps are required to create an nodejs image. The following steps are added as needed
Install yarn
Install dependencies
apk add --no-cache --virtual .build-deps-yarn curl gnupg
Import the public key
for key in \
6A010C5166006599AA17F08146C2130DFD2497F5 \
; do \
gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
done
Download check
curl -fSL -o yarn.js "https://yarnpkg.com/downloads/$YARN_VERSION/yarn-legacy-$YARN_VERSION.js" \
&& curl -fSL -o yarn.js.asc "https://yarnpkg.com/downloads/$YARN_VERSION/yarn-legacy-$YARN_VERSION.js.asc" \
&& gpg --batch --verify yarn.js.asc yarn.js \
&& rm yarn.js.asc
$YARN_VERSION:
Refers to yarn version, such as 0.22
The installation
apk add --no-cache \
libstdc++ \
&& apk add --no-cache --virtual .build-deps \
binutils-gold \
curl \
g++ \
gcc \
gnupg \
libgcc \
linux-headers \
make \
python
0
Clean up the
apk add --no-cache \
libstdc++ \
&& apk add --no-cache --virtual .build-deps \
binutils-gold \
curl \
g++ \
gcc \
gnupg \
libgcc \
linux-headers \
make \
python
1
c + + plug-in
To support c++ plug-ins, install python,make,g++
apk add --no-cache \
libstdc++ \
&& apk add --no-cache --virtual .build-deps \
binutils-gold \
curl \
g++ \
gcc \
gnupg \
libgcc \
linux-headers \
make \
python
2
headers file
Some c++ modules also need to download node-headers files in the process of use, node-ES157en files are unstable in domestic download, it is recommended to integrate them into the mirror, otherwise you may encounter a package compilation for a long time without action.
Refer to nodejs source code download verification procedures for headers file download verification
curl -SLO "https://nodejs.org/dist/v$NODE_VERSION/node-v${NODE_VERSION}-headers.tar.xz"
Install the headers file
mkdir /root/.node-gyp
tar --strip-component 1 -xzf node-v${NODE_VERSION}-heraders.tar.xz /root/.node-gyp/$NODE_VERSION
rm -rf node-v${NODE_VERSION}-headers.tar.xz
$NODE_VERSION:
node, as in 6.10.1
conclusion
Creating a minimum docker image is simple and makes sense