Slides presented during Containers 101 online meetup.
Full recording of the session is available at
https://vimeo.com/133709066
Join future meetup live at :
http://www.meetup.com/Containers-101-online-meetup/
Follow our blog posts at:
blog.codefresh.io
Building a micro-service using Node.js and Docker - Part #1
1. Containers #101
Building a micro-service using
Node.js & Docker
July 15
Recording available at:
https://vimeo.com/133709066
2. About the meetup
• A series of 30 min webinars + 10 min Q&A
• Hands On
• Session are recorded
• Suggested topics and presenters are
welcome
3. About me
• Developer, entrepreneur and technology
enthusiast
• Live in the Silicon Valley since 2008
• Founder and CEO of Codefresh
• Member of the Node.js foundation
@RazielTabib
4. About codefresh
• A Docker platform for development teams.
• Build, Run & Test Docker containers
• Launch any PR, Commit or Branch in
seconds
@codefresh
5. What we will see today
• Create a base Docker image
ubuntu
Node.js
base image
6. What we will see today
• Create a base Docker image
• Build a simple micro-service
ubuntu
Node.js
base image
micro-service
7. What we will see today
• Create a base Docker image
• Build a simple micro-service
• Run our micro-service inside a container
ubuntu
Node.js
base image
micro-service
8. What we will see today
• Create a base Docker image
• Build a simple micro-service
• Run our micro-service inside a container
• Create a Docker image from an existing container
ubuntu
Node.js
base image
micro-service
micro-service image
9. What we will see today
• Create a base Docker image
• Build a simple micro-service
• Run our micro-service inside a container
• Create a Docker image from an existing container
• Publish our image in Docker hub
ubuntu
Node.js
base image
micro-service
micro-service image
10. Terminology
Docker Images
Blueprints of our application
Docker Container
Created from docker images and are real instances of our application
Docker Daemon
Building, running and distributing Docker containers
Docker Client
Run on our local machine and connect to the daemon
Docker Hub
A registry of docker images
https://www.airpair.com/node.js/posts/getting-started-with-docker-for-the-nodejs-dev
11. Create a base image
• Check boot2docker version & status
11
Raziels-Air:~ RazielMac$ boot2docker version
• search, pull & run ubuntu image
Raziels-Air:~ RazielMac$ boot2docker status
Raziels-Air:~ RazielMac$ docker search ubuntu
Raziels-Air:~ RazielMac$ docker run -it ubuntu ./bin/bash
• Install node
Raziels-Air:~ apt-get update
Raziels-Air:~ apt-get install nodejs
Raziels-Air:~ apt-get install nodejs-legacy
ubuntu
Node.js
base image
Raziels-Air:~ apt-get install npm
• Create a Docker image
Raziels-Air:~ docker commit –a “razielt” CONTAINER-ID IMAGE-NAME:tag
12. Build a simple micro-service
• Create a skeleton using express generator
12
Raziels-Air:~ RazielMac$ npm install –g express-generator
• Add an api.js with simple get function
Raziels-Air:~ RazielMac$ express microservice
• Run micro-service locally
Raziels-Air:~ npm install
Raziels-Air:~ npm start
/* GET greeting. */
router.get('/sayhello', function(req, res) {
res.send('Hello meetup members');
});
micro-service
13. Run inside a container
• Get our local directory path
13
Raziels-Air:~ pwd
• Run a container with volume mounting and port mapping
• Copy micro-service to container
Raziels-Air:~ cp –r host /microservice
Raziels-Air:~ cd microservice
ubuntu
Node.js
micro-service
base image
Raziels-Air:~ RazielMac$ docker run –it –v HOSTFOLDER:CONTAINERFOLDER –p HOSTPORT:CONTAINERPORT
IMAGENAME:TAG
Raziels-Air:~ npm start
14. Create & run docker image
• Create an image
14
Raziels-Air:~ docker commit CONTAINERID, IMAGENAME:TAG
• Run micro-service in the background
ubuntu
Node.js
micro-service
base image
Raziels-Air:~ RazielMac$ docker run –d –w /microservice –p 9000:3000 IMAGENAME npm start
micro-service image
15. Push to Docker hub
• Login
15
Raziels-Air:~ docker login
• Push out image
ubuntu
Node.js
micro-service
base image
Raziels-Air:~ RazielMac$ docker tag nodejs-microservice your_dockerhub_user/nodejs-microservice
micro-service image
Raziels-Air:~ RazielMac$ docker push your_dockerhub_user/nodejs-microservice
16. What we did today
• Create base Docker image
• Build a simple micro-service
• Run our micro-service inside a container
• Create a Docker image from an existing container
• Publish our image in Docker hub
ubuntu
Node.js
base image
micro-service
micro-service image