Friday, January 02, 2015

Running your applications in a Docker.io Container

In my previous post, I described how you can build a template application to use as a start point for your node.js applications.

In this post, we will learn how to run that application in a Docker.io container.

So, what is docker.io ?
Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications. Consisting of Docker Engine, a portable, lightweight runtime and packaging tool, and Docker Hub, a cloud service for sharing applications and automating workflows, Docker enables apps to be quickly assembled from components and eliminates the friction between development, QA, and production environments. As a result, IT can ship faster and run the same app, unchanged, on laptops, data center VMs, and any cloud.
How to build your docker image:

In order to build the docker image, we need to create the "recipe" for the image in a Dockerfile.
FROM centos:centos6

MAINTAINER Luciano Resende 

# Enable EPEL repository for GIT, Node.js and npm
RUN rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

# Install Git, Node.js and npm
RUN yum install -y git nodejs npm

# Checkout node-app-template from github
RUN git clone https://github.com/lresende/node-app-template /opt/node-app-template

# Install app dependencies
RUN ls /opt/node-app-template
RUN cd /opt/node-app-template; npm install

# Node app is running on port 3000
EXPOSE 3000

# Define what to run when container is started
CMD ["node", "/opt/node-app-template/bin/www"]
Now that we have the "recipe" for building the docker image, we can build it with
sudo docker build --rm --no-cache -t node-app .
To run the application, we need to start a docker container based on the image we have just created. Note that, when we start the container, we are redirecting the public port 8080 to the exposed internal port 3000.
sudo docker run -p 8080:3000 -d node-app
Now, we are ready to access the application, just start your browser and point it to
http://localhost:8080
Hope this helps you get started with Docker containers.

All the source code is also available in the github repository: node-app-container.

No comments:

Post a Comment