如何使用node.js加速运行docker-container?

时间:2016-04-21 06:44:07

标签: node.js deployment docker dockerfile

我使用node.js和docker开发应用程序。我的部署过程包括几个步骤:

  1. build docker-image
  2. 运行docker-container
  3. 我有19秒的时间来运行我的node_js容器。但我希望至少将时间减少到1-5秒。 大多数运行容器时间需要npm install

    可以加快运行我的node.js应用程序吗?

    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}
    

1 个答案:

答案 0 :(得分:0)

您可以通过检查自上次安装后是否已修改npm install来自定运行package.json。在这里,我要检查package.json修改时间是否比修改时间node_modules更新:

[ package.json -nt node_modules ] && npm install ;