Dockerize node.js application

  • View
    689

  • Download
    0

Embed Size (px)

Text of Dockerize node.js application

  • dockerize node.js applicationshipping node.js application with docker

    @SOCAR

  • YOU CAN DOCKERIZE YOUR APPLICATION TOMORROW

  • WHY WHAT HOW

  • WHY WHAT HOW

  • WHY?

    EASIER ENVIRONMENT SETUP

    SINGLE-THREADED in MULTI-CORE

    FAST AND EASY TO ROLLBACK

    MICROSERVICE

    VIRTUALIZATION

  • EASIER ENVIRONMENT SETUP

    Application Server

  • EASIER ENVIRONMENT SETUPTRADITIONAL WAY

    $curl-sLhttps://deb.nodesource.com/setup_6.x|sudo-Ebash-

    $sudoapt-getupdate$sudoapt-getinstallnodejs

    $echodebhttp://nginx.org/packages/debian/squeezenginx>>/etc/apt/sources.list$echodeb-srchttp://nginx.org/packages/debian/squeezenginx>>/etc/apt/sources.list

    $sudoapt-getinstallnginx

    $sudoapt-getinstallntpvimfail2ban

    $vi/etc/nginx/site-enabled/node.conf$nginx-sreload

    $mkdir/home/alma/app&&cd/home/alma/app

    $gitpull&&npminstall

    $npmrunbuild

    $npmrunserve

  • EASIER ENVIRONMENT SETUP

    Application

    Servers

  • EASIER ENVIRONMENT SETUP

    $curl-sLhttps://deb.nodesource.com/setup_6.x|sudo-Ebash-

    $sudoapt-getupdate$sudoapt-getinstallnodejs

    $echodebhttp://nginx.org/packages/debian/squeezenginx>>/etc/apt/sources.list$echodeb-srchttp://nginx.org/packages/debian/squeezenginx>>/etc/apt/sources.list

    $sudoapt-getinstallnginx

    $sudoapt-getinstallntpvimfail2ban

    $vi/etc/nginx/site-enabled/node.conf$nginx-sreload

    $mkdir/home/alma/app&&cd/home/alma/app

    $gitpull&&npminstall

    $npmrunbuild

    $npmrunserveX3

    TRADITIONAL WAY

  • EASIER ENVIRONMENT SETUP

    Application

    Many Servers

  • EASIER ENVIRONMENT SETUP

    TRADITIONAL WAY

    I really didnt want to be a developer to do this

  • SINGLE-THREADED in MULTI-CORE

    MULTICORESingle-threaded

    in

  • SINGLE-THREADED in MULTI-CORECLUSTER

    varcluster=require('cluster');if(cluster.isMaster){varnumWorkers=require('os').cpus().length;console.log('Masterclustersettingup'+numWorkers+'workers...');for(vari=0;i>/etc/apt/sources.list$echodeb-srchttp://nginx.org/packages/debian/squeezenginx>>/etc/apt/sources.list

    $sudoapt-getinstallnginx

    $sudoapt-getinstallntpvimfail2ban

    $vi/etc/nginx/site-enabled/node.conf$nginx-sreload

    $mkdir/home/alma/app&&cd/home/alma/app

    $gitpull&&npminstall

    $npmrunbuild

    $npmrunserve

  • EASIER ENVIRONMENT SETUPDOCKERIZED WAY

    FROMnode:6

    RUN\apt-getupdate--force-yes-y&&\mkdir-p/src

    COPYpackage.json/src/package.json

    RUN\apt-getupdate--force-yes-y&&\apt-getinstall--force-yes-yvim

    RUN\cd/src;npminstall--production

    COPY./src

    RUN\cd/src;npmrunbuild

    EXPOSE3000

    ENVNODE_ENVproductionENVDBliveENVTZAsia/Seoul

    WORKDIR/src

    CMDnpmrunserve

    Dockerfile

  • EASIER ENVIRONMENT SETUPDOCKERIZED WAY

    $dockerbuild-tnode-docker.

    $dockerimages

    REPOSITORYTAGIMAGEIDCREATEDSIZEnode-dockerlatest08a5b1c92fcfAboutaminuteago654.5MB

  • EASIER ENVIRONMENT SETUPOFFICIAL NODE Dockerfile

    FROMbuildpack-deps:jessie

    RUNgroupadd-rnode&&useradd-r-gnodenode

    #gpgkeyslistedathttps://github.com/nodejs/nodeRUNset-ex\&&forkeyin\9554F04D7259F04124DE6B476D5A82AC7E37093B94AE36675C464D64BAFA68DD7434390BDBE9B9C5\0034A06D9D9B0064CE8ADF6BF1747F4AD2306D93FD3A5288F042B6850C66B31F09FE44734EB7990E\71DCFD284A79C3B38668286BC97EC7A07EDE3FC1DD8F2338BAE7501E3DD5AC78C273792F7D83545D\B9AE9905FFD7803F25714661B63B535A4C206CA9C4F0DFFF4E8C1A8236409D08E73BC641CC11F4C8\;do\gpg--keyserverha.pool.sks-keyservers.net--recv-keys"$key";\done

    ENVNPM_CONFIG_LOGLEVELinfoENVNODE_VERSION6.9.1

    RUNcurl-SLO"https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.xz"\&&curl-SLO"https://nodejs.org/dist/v$NODE_VERSION/SHASUMS256.txt.asc"\&&gpg--batch--decrypt--outputSHASUMS256.txtSHASUMS256.txt.asc\&&grep"node-v$NODE_VERSION-linux-x64.tar.xz\$"SHASUMS256.txt|sha256sum-c-\&&tar-xJf"node-v$NODE_VERSION-linux-x64.tar.xz"-C/usr/local--strip-components=1\&&rm"node-v$NODE_VERSION-linux-x64.tar.xz"SHASUMS256.txt.ascSHASUMS256.txt\&&ln-s/usr/local/bin/node/usr/local/bin/nodejs

    CMD["node"]

    Dockerfile

  • EASIER ENVIRONMENT SETUPDOCKERIZED WAY

    $dockerimages

    REPOSITORYTAGIMAGEIDCREATEDSIZEnode-dockerlatest08a5b1c92fcfAboutaminuteago654.5MB

    $dockerrun--namedocker-instance-1--port80:3000node-docker

  • SINGLE-THREADED in MULTI-COREEASY TO SCALE

    $dockerimages

    REPOSITORYTAGIMAGEIDCREATEDSIZEnode-dockerlatest08a5b1c92fcfAboutaminuteago654.5MB

    $dockerrun--namedocker-instance-1--port80:3000node-docker

    $dockerrun--namedocker-instance-2--port81:3000node-docker

    $dockerrun--namedocker-instance-3--port82:3000node-docker

    .

    .

    .

  • SINGLE-THREADED in MULTI-COREEASY TO SCALE

    http{upstreamapplication{least_conn;serverlocalhost:80max_fails=3fail_timeout=60s;serverlocalhost:81max_fails=3fail_timeout=60s;serverlocalhost:82max_fails=3fail_timeout=60s;}

    server{listen80;

    location/{proxy_passhttp://application;proxy_http_version1.1;proxy_set_headerUpgrade$http_upgrade;proxy_set_headerConnection'upgrade';proxy_set_headerHost$host;proxy_cache_bypass$http_upgrade;}}}

    nginx.conf

  • MICROSERVICEDOCKER-COMPOSE

    version:'2'

    services:application:container_name:applicationimage:node-docker

    docker-compose.yml

  • MICROSERVICEDOCKER-COMPOSE

    version:'2'

    services:application:container_name:applicationimage:node-docker

    redis:container_name:redisimage:redis

    docker-compose.yml

  • MICROSERVICEDOCKER-COMPOSE

    version:'2'

    services:application:container_name:applicationimage:node-docker

    redis:container_name:redisimage:redis

    nginx:container_name:nginximage:nginxports:-80:80-443:443

    docker-compose.yml

  • MICROSERVICEDOCKER-COMPOSE

    version:'2'

    services:application:container_name:applicationimage:node-docker

    application-alt:container_name:applicationimage:node-docker

    redis:container_name:redisimage:redis

    nginx:container_name:nginximage:nginxports:-80:80-443:443

    docker-compose.yml

  • MICROSERVICEDOCKER-COMPOSE

    version:'2'

    services:application:container_name:applicationimage:node-docker

    application-alt:container_name:applicationimage:node-docker

    redis:container_name:redisimage:redis

    nginx:container_name:nginxbuild:./nginxrestart:alwayslinks:-application-1:application-application-2:application-altports:-80:80-443:443depends_on:-application-application-alt

    docker-compose.yml

  • FAST AND EASY TO ROLLBACKBUILD WITH TAG

    $dockerbuild-tnode-docker.

    $dockerimages

    REPOSITORYTAGIMAGEIDCREATEDSIZEnode-dockerlatest08a5b1c92fcfAboutanhourago654.5M

    $dockerbuild-tnode-docker:1982.

    $dockerimages

    REPOSITORYTAGIMAGEIDCREATEDSIZEnode-dockerlatest08a5b1c92fcfAboutanhourago654.5MBnode-docker198210394kdfksf1Aboutaminuteago654.5MB

  • FAST AND EASY TO ROLLBACKBUILD WITH TAG

    $dockerbuild-tnode-docker.

    $dockerimages

    REPOSITORYTAGIMAGEIDCREATEDSIZEnode-dockerlatest08a5b1c92fcfAboutanhourago654.5M

    $dockerbuild-tnode-docker:1982.

    $dockerimages

    REPOSITORYTAGIMAGEIDCREATEDSIZEnode-dockerlatest08a5b1c92fcfAboutanhourago654.5MBnode-docker198210394kdfksf1Aboutaminuteago654.5MB

  • FAST AND EASY TO ROLLBACK

  • FAST AND EASY TO ROLLBACKDOCKER HUB

    https://hub.docker.com/r/library/node/tags/

  • WHY WHAT HOW

  • YOU CAN DOCKERIZE YOUR APPLICATION TOMORROW

  • $npminit-f

    $npmi-Sexpressuuid

    DOCKERIZESTART WITH EXPRESS

  • DOCKERIZESTART WITH EXPRESS

    varexpress=require('express');varuuid=require('uuid');

    varapp=express();varid=uuid.v4();varport=3000;

    app.get('/',function(req,res){res.send(id)});

    app.listen(port,function(){console.log('Exampleapplisteningonport:'+port);});

    index.js

  • DOCKERIZEBUILD DOCKER IMAGE

    node_modules/

    .dockerignore

  • DOCKERIZE

    FROMnode:6

    COPYpackage.json/src/package.json

    RUNcd/src;npminstall

    COPY./src

    EXPOSE3000

    WORKDIR/src

    CMDnodeindex.js

    Dockerfile

    BUILD DOCKER IMAGE

  • $dockerbuild--tagnode-nginx:test.

    SendingbuildcontexttoDockerdaemon7.168kBStep1:FROMnode:66:Pullingfromlibrary/node357ea8c3d80b:Alreadyexists52befadefd24:Alreadyexists3c0732d5313c:Pullcompleteceb711c7e301:Pullcomplete868b1d0e2aad:Pullcomplete61d10f626f84:PullcompleteDigest:sha256:12899eea666e85f23e9850bd3c309b1ee28dd0869f554a7a6895fc962d9094a3Status:Downloadednewerimagefornode:6--->800da22d0e7bStep2:COPYpackage.json/src/package.json--->7f3344975b1eRemovingintermediatecontainerae1d0482e982Step3:RUNcd/src;npminstall--production--->Runningin222a0585301b

    ...

    Successfullybuilt08a5b1c92fcf

    BUILD DOCKER IMAGEDOCKERIZE

  • $dockerrun--namenode-nginx-instance-p3000:3000node-nginx:test

    Exampleapplisteningonport:3000

    $dockerps

    CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES30179995521cnode-nginx:test"/bin/sh-c'nodeind"AboutaminuteagoUpAboutaminute0.0.0.0:3000->3000/tcpnode-nginx-instance

    BUILD DOCKER IMAGEDOCKERIZE

  • $dockerrun-d--namenode-nginx-instance-0-p3000:3000node-nginx:test

    $dockerrun-d--namenode-nginx-instance-1-p3001:3000node-nginx:test

    $dockerrun-d--namenode-nginx-instance-2-p3002:3000node-nginx:test

    RUN 3 DOCKER CONTAINERDOCKERIZE

  • worker_processes1;