Kenan
Assistant Engineer
Assistant Engineer
  • UID621
  • Fans0
  • Follows0
  • Posts55
Reads:801Replies:0

DevOps and Alibaba Cloud Docker (II)

Created#
More Posted time:Dec 28, 2016 14:24 PM
In this article, I will introduce the ContainerOps practice and experience of using Alibaba Cloud Docker through a simple example.
The first E2E ContainerOps example
From the perspective of DevOps, the essence is the process from development to deployment. The steps of traditional DevOps are roughly as follows.
 
The procedures of ContainerOps are roughly as follows.
 
We can make the first E2E scenario a little simple. We want to realize automatic updating of a deployed and launched application.
Taking a Node.js application as an example. This application uses Express to make a simple dynamic website and uses Mocha as the unit testing framework. The procedures required are as follows:

0. Prepare the Node.js environment
Install nvm (Node.js version manager)
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash

Install the Node.js of a specified version
nvm install v5.7.1 (You can use nvm ls-remote command to view the versions available)

Install Yeoman (A project scaffolding set of Node.js)
npm install -g yo

Install node-mocha seed
npm install -g generator-node-mocha
1. Prepare the Node.js application
In this article, Yeoman scaffolding is used and the seed is node-mocha.
yo node-mocha //Generate a public template



Install the Express framework
npm install express --save-dev
Add the demo_server.js file in the lib directory
var text = require('./demo')
var express = require('express');
var app = express();

app.get('/', function (req, res) {
res.send('Hello World! ' + text.awesome() );
});

var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;

console.log('Example app listening at http://%s:%s', host, port);
});
Try to run the project in the root directory of the project
node lib/demo_server.js

Visit localhost:3000 in the browser and you will see:


Run unit testing. Run the command in the root directory

npm test

You can get the result as below, indicating all the testing results have been passed



2. Containerization in Docker
Note: Make sure to delete the
node_modules line in the .gitignore file of the directory. Otherwise, you
cannot run the Docker.
Next we will add the Dockerfile.

FROM node:5.7.1-slim
MAINTAINER Ringtail

ADD ./ /workspace

EXPOSE 3000
EXPOSE 22

ENTRYPOINT ["node","/workspace/lib/demo_server.js"]

The current application is simple. We can build it using Alibaba Cloud hub. For general applications, it
is recommended you validate the build locally before using automatic building.
It is suggested that you check overseas servers for building as it will provide robust acceleration for scenarios that
require mirror sources.

After the building, construct the application through the compose templates in Docker.

express:
image: 'registry.aliyuncs.com/ringtail/nodejs-demo:0.1'
expose:
- '22'
- '3000'
restart: always
labels:
aliyun.routing.port_3000: express

After Docker is started, we can see the corresponding address
Visit the address and you will see the following:
 
Next, we will start to integrate the hub and Docker. Under the Docker application, select to create a
trigger。
After the trigger creation, we can see a string of special URL is generated
Back to the hub, we fill the hook trigger address in the hub as a webhook
By now, we have integrated the hub and re-deployment. Let’s verify it. Modify our source code and commit
it to the master node

You will find the hub has begun the automatic building
  
After the building is complete, view the application deployment log and you will find the application
has been re-deployed

Now, open the browser and verify it
 
So far, the first E2E scenario capable
of semi-automated deployment has been established. In the next article, we will
add a CI server for continuous integration and this demo will be used as the
base project.

Related resources
Nvm: https://github.com/creationix/nvm
Yeoman: http://yeoman.io/
Node-mocha: https://www.npmjs.com/package/generator-node-mocha
Project address: https://github.com/ringtail/containerops
Guest