How to speed up running docker-container with node.js? - node.js

I develop application with node.js and docker. My deploy process includes few steps:
build docker-image
run docker-container
I have 19 seconds to running of my node_js container. But i want to reduce the time to 1-5 seconds at least.
Most of run container time it takes npm install.
It is possible to speed up running my node.js application?
Dockerfile
FROM ubuntu:14.04
MAINTAINER Vovan
# docker build --build-arg NODE_VERSION=any_version .
ARG NODE_VERSION=4.4.2
ARG EXTERNAL_GIT_URI=local_gitlab
ARG EXTERNAL_GIT_PORT=22
# variables
ENV TERM=xterm
# use bash instead of sh
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
# essential system updates and soft
RUN apt-get update -qq \
&& apt-get install -yq \
ntp \
build-essential \
curl \
mc \
nano \
git \
&& curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash \
# add nvm as term command
&& . ~/.nvm/nvm.sh \
#
&& nvm install ${NODE_VERSION} \
&& nvm alias default ${NODE_VERSION}
# home directory
RUN useradd -m -d /home/app -s /bin/bash app -u9999
# copy keys in image
ADD for_gitlab /root/.ssh/for_gitlab
ADD config /root/.ssh/config
RUN chmod 600 /root/.ssh/* \
&& ssh-keyscan -p ${EXTERNAL_GIT_PORT} ${EXTERNAL_GIT_URI} > /root/.ssh/known_hosts
# copy script and make it executable
WORKDIR /
RUN mkdir config
ADD starter.sh /config
CMD ["/config/starter.sh"]
RUN chmod +x /config/starter.sh
WORKDIR /home/app
starter.sh
APP_DIR=/home/app/${GIT_REPO_NAME}
GIT_CLONE_URI=git#local_gitlab:${GIT_GROUP}/${GIT_REPO_NAME}.git
cd /home/app
git clone ${GIT_CLONE_URI} --depth 1 --branch ${GIT_REPO_BRANCH}
# permissions
find ${APP_DIR} -type f -exec chmod 644 {} \;
find ${APP_DIR} -type d -exec chmod 755 {} \;
chown -R app:app ${APP_DIR}
#
. ~/.nvm/nvm.sh
nvm use default
cd ${APP_DIR} && npm install
find ${APP_DIR}/node_modules -type f -exec chmod 644 {} \;
find ${APP_DIR}/node_modules -type d -exec chmod 755 {} \;
node ${SCRIPT}

You can conditinally run npm install by checking if package.json has been modified since the last the installation. Here I'm checking if package.json modified time is newer than node_modules modified time :
[ package.json -nt node_modules ] && npm install ;

Related

FATAL tini (7) exec /docker-entrypoint.sh failed: No such file or directory

I'm getting this error when I run my Dockerfile.
[FATAL tini (7)] exec /docker-entrypoint.sh failed: No such file or directory
The docker build command runs fine but when I try to run it I get this issue.
I have confirmed that docker-entrypoint.sh file does exist in the root via linux find -name "docker-entrypoint.sh".
It's not a permissions issue since I have set the full read/write permissions for the file via chmod 777 command.
Any idea what I'm missing here. Below is a snippet from my Dockerfile
ARG base_image_name=tomcat
ARG base_image_version=9-jdk11-openjdk-slim
FROM $base_image_name:$base_image_version
ARG component_exe
COPY docker-entrypoint.sh $component_exe /
ENV APP_ROOT=/usr/local/tomcat
ENV component_runtime=${APP_ROOT}/webapps/$component_exe
ENV APP_USER=uat
# Install all required packages and set appropriate access permissions
RUN \
apt-get -y update && \
apt-get -y upgrade && \
apt-get install jq bash ca-certificates tini && \
adduser --disabled-password --gecos "" ${APP_USER} && \
mkdir -p ${APP_ROOT}/temp && \
mkdir -p ${APP_ROOT}/bin && \
mkdir -p ${APP_ROOT}/webapps && \
chmod 777 ${APP_ROOT}/webapps && \
chown -R ${APP_USER}:root ${APP_ROOT} && \
chmod 777 /docker-entrypoint.sh && \
chmod -R g=u ${APP_ROOT} && \
find / -name *.war && \
find / -name *tini* && \
find / -name *-entrypoint.sh && \
mv /$component_exe /usr/local/tomcat/webapps && \
chown -R :root /usr/local/openjdk-11/lib/security/cacerts && \
chmod 660 /usr/local/openjdk-11/lib/security/cacerts
EXPOSE 8080/tcp
ENTRYPOINT ["/usr/bin/tini", "--", "/docker-entrypoint.sh"]
# Start Tomcat Server.
CMD ["catalina.sh", "run"]

How to include shell script suite in docker file

I am trying to create a docker image for BBMAP (https://sourceforge.net/projects/bbmap/files/latest/download) shell script suite available online along with samtools and picard.jar. I was able to run samtools and picard, but for some reason I am not able to add bbmap below. Can someone please let me know what I am missing here?
I am trying to add shell scripts to bin to run them as executables.
In my docker file below, this is where I need help:
&& wget -q https://sourceforge.net/projects/bbmap/files/latest/download \
&& tar -xjvf /tmp/download \
&& cp -av /tmp/bbmap/* /usr/bin/ \
Dockerfile:
FROM openjdk:8-jre
LABEL maintainer="AN <XXX#wustl.edu>"
LABEL org.label-schema.schema-version="1.0"
# LABEL org.label-schema.build-date=$BUILD_DATE
LABEL org.label-schema.name="an/samtofastq"
LABEL org.label-schema.description="Image for Reverting .bam"
ENV SAMTOOLS_VERSION 1.9
ENV PICARD_VERSION 2.20.8
WORKDIR /tmp
RUN apt-get update -y \
&& apt-get install --no-install-recommends -y \
make \
gcc \
g++ \
libz-dev \
libbz2-dev \
liblzma-dev \
ncurses-dev \
bc \
libnss-sss \
time \
&& cd /tmp \
&& wget -q https://github.com/samtools/samtools/releases/download/${SAMTOOLS_VERSION}/samtools-${SAMTOOLS_VERSION}.tar.bz2 \
&& tar xjvf samtools-${SAMTOOLS_VERSION}.tar.bz2 \
&& cd /tmp/samtools-${SAMTOOLS_VERSION}/ \
&& make \
&& cp -av /tmp/samtools-${SAMTOOLS_VERSION}/samtools /usr/bin/ \
&& wget -q -O /usr/bin/picard.jar https://github.com/broadinstitute/picard/releases/download/${PICARD_VERSION}/picard.jar \
&& wget -q https://sourceforge.net/projects/bbmap/files/latest/download \
&& tar -xjvf /tmp/download \
&& cp -av /tmp/bbmap/* /usr/bin/ \
&& ln -sf /usr/share/zoneinfo/America/Chicago /etc/localtime \
&& echo "America/Chicago" > /etc/timezone \
&& dpkg-reconfigure --frontend noninteractive tzdata \
&& apt-get clean all \
&& rm -rfv /var/lib/apt/lists/* /tmp/* /var/tmp/*
# COPY ./entrypoint.sh /usr/local/bin/
ENV PICARD /usr/bin/picard.jar
## Add ENV for Shell scripts from BBMAP
# ENV DEMUX /usr/bin/demuxbyname.sh
# ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["/bin/bash"]
This is the error I am getting:
tar (child): /tmp/download: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
Earlier in your script, you run:
cd /tmp/samtools-${SAMTOOLS_VERSION}/
So when you run this:
wget -q https://sourceforge.net/projects/bbmap/files/latest/download
The resulting file is:
/tmp/samtools-${SAMTOOLS_VERSION}/download
That means that when you run your tar command:
tar -xjvf /tmp/download
...it doesn't find the file because it isn't in /tmp.
Either change the path on the tar command, or add a cd /tmp to your script before downloading the file. Or pass something like -O /tmp/download.tar to your wget invocation.

./asinstall not found in while docker build using dockerfile?

I am trying to build docker image using docker file . The docker file will contain aerospike database creation.
RUN wget -O aerospike.tgz 'https://www.aerospike.com/download/server/latest/artifact/ubuntu18'
RUN tar -xvf aerospike.tgz
RUN cd aerospike-server-community-*-ubuntu18*
RUN ./asinstall
FROM ubuntu:18.04
RUN apt-get update && apt-get install -q -y curl python2.7 python
RUN TEMPDIR=$(mktemp -d) && \
cd $TEMPDIR && \
curl -L 'https://www.aerospike.com/download/server/latest/artifact/ubuntu18' | tar xzv --strip-components 1 && \
./asinstall && \
cd / && \
rm -rf $TEMPDIR
seems to work.

build image from tarball

I create a tar archive from within my working dir
tar -chv . --exclude='build/tarball.tar' > build/tarball.tar
and would like to create an image from this
sudo docker build - < build/tarball.tar
This is my dockerfile
FROM node:8.11.3-slim
RUN apt-get update && apt-get install -y
ENV GOSU_VERSION 1.10
RUN set -x \
&& dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')" \
&& wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch" \
&& wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc" \
&& export GNUPGHOME="$(mktemp -d)" \
&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \
&& gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \
&& rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc \
&& chmod +x /usr/local/bin/gosu \
&& gosu nobody true
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
# node-user comes from node-docker-image
RUN chown -R node:node /usr/src/app
EXPOSE 8080
ENV NODE_ENV production
CMD ["gosu","node","npm","start"]
However starting the images failes because node throws an error:
Error: Cannot find module '..'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
If I create the image from the directory itself it works, why doesn't it work if I point to the tar archive?
Depending on your docker version, I think you might want to be using docker import:
docker import build/tarball.tar mynode:latest

Node not found in alpine docker

I have the following Dockerfile:
FROM alpine:3.3
RUN apk update \
&& apk add curl tar git gzip
RUN curl --retry 3 --retry-delay 20 --show-error --location --remote-name --silent "https://nodejs.org/dist/v6.2.0/node-v6.2.0-linux-x64.tar.gz" \
&& tar -xzf "node-v6.2.0-linux-x64.tar.gz" -C /usr/local --strip-components=1 --same-owner \
&& rm -rf "node-v6.2.0-linux-x64.tar.gz" \
&& ls -la /usr/local/bin && env \
&& /usr/local/bin/node -v \
&& npm cache clear
Building the image gives me:
Sending build context to Docker daemon 13.51 MB
Step 1 : FROM alpine:3.3
---> 3e467a6273a3
Step 2 : RUN apk update && apk add curl tar git gzip # bzip2 build-essential libfreetype6 libfreetype6-dev libfontconfig1 libfontconfig1-dev python python-dev python-pip python-virtualenv libkrb5-devENV NODE_VERSION=6.2.0
---> Using cache
---> 65f46657024a
Step 3 : RUN curl --retry 3 --retry-delay 20 --show-error --location --remote-name --silent "https://nodejs.org/dist/v6.2.0/node-v6.2.0-linux-x64.tar.gz" && tar -xzf "node-v6.2.0-linux-x64.tar.gz" -C /usr/local --strip-components=1 --same-owner && rm -rf "node-v6.2.0-linux-x64.tar.gz" && ls -la /usr/local/bin && env && /usr/local/bin/node -v && npm cache clear
---> Running in 32967b91e2dd
total 26828
drwxrwxr-x 2 500 500 4096 May 17 19:40 .
drwxr-xr-x 10 root root 4096 May 24 14:28 ..
-rwxrwxr-x 1 500 500 27459658 May 17 19:40 node
lrwxrwxrwx 1 500 500 38 May 17 19:40 npm -> ../lib/node_modules/npm/bin/npm-cli.js
HOSTNAME=27c9668b3d5e
SHLVL=1
HOME=/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
/bin/sh: /usr/local/bin/node: not found
The command '/bin/sh -c curl --retry 3 --retry-delay 20 --show-error --location --remote-name --silent "https://nodejs.org/dist/v6.2.0/node-v6.2.0-linux-x64.tar.gz" && tar -xzf "node-v6.2.0-linux-x64.tar.gz" -C /usr/local --strip-components=1 --same-owner && rm -rf "node-v6.2.0-linux-x64.tar.gz" && ls -la /usr/local/bin && env && /usr/local/bin/node -v && npm cache clear' returned a non-zero code: 127
Build image failed
How is it possible for node to not be found?
It's installed in the correct directory, it's in the path, and has execution permission...
There is an official and up to date node package for alpine, you don't need to install it manually, just add this line in your Dockerfile:
RUN apk add nodejs=6.2.0-r0
Or you could use an existing nodejs alpine image:
FROM mhart/alpine-node:6.2.0
On a side note, the whole point of using an alpine based image is to get rid of the clutter, installing git, tar et al. is a waste of space IMHO.

Resources