Node on Docker

March 2020 JaxNode

Docker Facts

  • Project started out dotCloud
  • Written in Go
  • Using Linux Isolation features of namespaces and cgroups¬†
  • Offer services like Docker Hub and Docker Cloud
  • Windows 2016 Server is added Docker

Docker

  • Uses hyper-V on Windows
  • Use xhyve on Mac OS X
  • Remove need for Oracle VirtualBox
  • Containers also on Windows

Docker terms

  • Docker Engine
  • Docker Machine
  • Docker Compose
  • Docker Swarm
  • Docker Hub
  • Docker Cloud

Common Docker Commands

  • docker build
  • docker run
  • docker run -i -t
  • docker run -d
  • docker attach
  • docker ps
  • docker ps -a
  • docker commit

Docker-Machine

  • docker-machine ls
  • docker-machine create
  • eval "$(docker-machine env default)"
  • docker-machine start
  • docker-machine stop
  • docker-machine kill

Docker Compose

  • Organize your containers and services
  • docker-compose.yml
  • 'docker-compose up' to deploy containers
  • Gives an abstraction for deploying multiple services

Docker Swarm

  • Combine a pool of Docker hosts into 1
  • Can create Clusters
  • Uses Docker API
  • Discovery Services
  • Scheduling

Docker Hub

  • A host for containers
  • Publish your containers
  • Search Public repositories
  • Pull down to build containers

Docker Cloud

  • Deploy to your own cloud
  • Define a node
  • Define cluster of nodes of same type
  • Cluster Nodes have to by of the same provider
  • You can use with AWS, Azure, Digital Ocean or your own hosts

Building Containers

  • Start with base image
  • Use Dockerfile to define and set up
  • Can run and commit, but better to use Dockerfile¬†
  • For Nodejs apps, split the npm install and adding code step into separate steps

Dockerfile

FROM node:14.4.0

RUN mkdir /src
COPY package.json /src
WORKDIR /src 
RUN npm install

ENV foo bar
 
# Add your source files
COPY . /src  
CMD ["npm","start"] 

Dockerfile commands

  • FROM specify base container image
  • ADD/COPY lets you add files to container
  • RUN will let you run a Bash command
  • CMD takes an array of commands
  • ENV sets up an environment variable
  • EXPOSE exposes a port
  • USER to set the user
  • WORKDIR to set the directory your working dir

Docker Build

bash > docker build -t username/container:version /src

Docker run

bash > docker run -d -p 80:3000 username/container:version

Running Multiple Containers

  • Test Dependencies
  • Simulate Persistence Services
  • Run Tests on Mock Data

Common Scenario

  • Run HAProxy or Nginx
  • Use a database like MySQL or Mongo
  • Use Cache like Redis or HBase
  • Search with Elasticsearch

Honorable Mentions

  • Kubernetes
  • Mesos
  • Heroku

Demo

Code

  • https://github.com/jaxnode-UG/jaxnodechat
  • https://github.com/jaxnode-UG/node6es2015
  • https://slides.com/davidfekke/node6docker

Questions

Contact

  • David Fekke @ gmail dot com
  • Twitter @JaxNode
  • Skype: davidfekke