Ciao

Using Docker for Node.js Testing and Deployment

Taron Foxworth

Fox

Lead Sales Engineer 

The Problems

Client side

The Problems

Node.js

The Problems

  Developers have different environments

  Those environments are not the same as production

Clear definition of ... environment

Well... It work on my machine

We can do much, much beter

Docker

Build, Ship and Run Any App, Anywhere 

 

Portable deployment across machines,

even clouds

Component re-use

Containers

Immutable, Fluid Infrastructures

Sanity

FROM ubuntu

# verify gpg and sha256: http://nodejs.org/dist/v0.10.30/SHASUMS256.txt.asc
# gpg: aka "Timothy J Fontaine (Work) <tj.fontaine@joyent.com>"
# gpg: aka "Julien Gilli <jgilli@fastmail.fm>"
RUN gpg --keyserver pool.sks-keyservers.net --recv-keys 7937DFD2AB06298B2293C3187D33FF9D0246406D 114F43EE0176B71C7BC219DD50A3051F888C628D

ENV NODE_VERSION 0.12.2
ENV NPM_VERSION 2.9.1

RUN curl -SLO "http://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz" \
	&& curl -SLO "http://nodejs.org/dist/v$NODE_VERSION/SHASUMS256.txt.asc" \
	&& gpg --verify SHASUMS256.txt.asc \
	&& grep " node-v$NODE_VERSION-linux-x64.tar.gz\$" SHASUMS256.txt.asc | sha256sum -c - \
	&& tar -xzf "node-v$NODE_VERSION-linux-x64.tar.gz" -C /usr/local --strip-components=1 \
	&& rm "node-v$NODE_VERSION-linux-x64.tar.gz" SHASUMS256.txt.asc \
	&& npm install -g npm@"$NPM_VERSION" \
	&& npm cache clear

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm install
COPY . /usr/src/app

CMD [ "npm", "start" ]

DockerFile

docker build -t my-node-app .
docker run my-node-app

Build and Running

You've defined the EXACT enviroment

You application would start

Now, Sharing

docker run redis

Docker Hub

Example Images

Easily Try Things

$: docker tag IMAGE_ID my-company/my-node-app
$: docker push my-company/my-node-app

Docker Hub

Now we have a completely sharable enviroment

$: docker ps -a #get image id

$: docker login
bob $: docker pull my-company/my-node-app
bob $: docker run username/my-node-app
$: docker tag IMAGE_ID my-company/my-node-app:1.0.1

Git but for Env

Now, We can version control our environment too

bob $: docker pull my-company/my-node-app:1.0.1
bob $: docker run username/my-node-app:1.0.1
$: docker tag IMAGE_ID my-company/my-node-app:latest

A Better Dockerfile

FROM node:0.12.2

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm install
COPY . /usr/src/app

CMD [ "npm", "start" ]

Make use of prebuilt images

Even Better

FROM username/my-company-node:latest

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm install
COPY . /usr/src/app

CMD [ "npm", "start" ]

Base image for you company

Deploying Containers

Code Happy

😃

Level up 

web:
 build: .
 command: python app.py
 ports:
   - "5000:5000"
 volumes:
   - .:/code
 links:
   - redis
 environment:
   - PYTHONUNBUFFERED=1
redis:
 image: redis:latest
 command: redis-server --appendonly yes
$: docker-compose up

Docker Compose

Immutable contianers

docker run --rm -v $(pwd):/usr/src/myapp -w /usr/src/myapp golang:1.4 go build hello.go

-rm : Go away

-v : Mounting

-w : Working Directory

docker run --rm -v $(pwd):/usr/src/myapp -w /usr/src/myapp node:0.12.2 npm install && npm test

Accurate testing .... 😎

Docker vs. VM

What's Next..?

Security

Scheduling & Orchestration

Service Discovery

Questions?

Using Docker for Node.js Testing and Deployment

By Taron Foxworth

Using Docker for Node.js Testing and Deployment

The technical community faces many challenges everyday. One of these challenges is the ability of managing, moving, and duplication of environments. We develop in different environments than our applications actually run and node.js and node modules can behave differently across operation systems. With all the different tools available, Docker can provide a duplicatable environment for testing our node.js Applications. Join me in this session to learn more about how Docker will abstract the differences across environments creating a more reliable staging while also laying the foundation for production deployments.

  • 1,639
Loading comments...

More from Taron Foxworth