2016年11月29日 星期二

[docker, installation] 最簡單安裝 docker 方法 -- sudo curl -sSL https://get.docker.io/ | sh

http://mqjing.blogspot.com/2016/11/docker-installation-docker-sudo-curl.html

[docker] How to install docker on Ubuntu/CentOS



Virtual Machine
Docker

Google doc: This document.
如果你在網路上常常看到別人談論 docker 有多好, 有多棒. 有多少好處? 多多少少都會想這又是新的東西. 要安裝一定很麻煩. 其實只要一行指令, 再加上 2 分鐘自動安裝.
搞定!

sudo curl -sSL https://get.docker.io/ | sh

docker 真的很好用. 光是保持系統乾淨這項優點, 就值得把你的所有程式開發與執行環境全部使用 docker 處理.  
Enjoy & Good Luck
Jing

Installation

# 一行指令
sudo curl -sSL https://get.docker.io/ | sh


# 設定 docker user
sudo usermod -aG docker {your-name}

e.g.
sudo usermod -aG docker jing

Verification

docker -v

其他複雜的安裝方法


CentOS

# Install docker
yum -y update
yum -y install docker docker-registry

# Start Docker and Make Sure Docker Starts on Boot
systemctl enable docker.service
systemctl start docker.service
systemctl status docker.service

# download & run a docker container
docker pull centos
docker run -i -t centos /bin/bash

Verification

Reference

Ubuntu
Latest release version
# for https transport
[ -e /usr/lib/apt/methods/https ] || {
 apt-get update
 apt-get install apt-transport-https
}

# for apt-key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9

# update the apt-repository
sudo sh -c "echo deb https://get.docker.io/ubuntu docker main\
> /etc/apt/sources.list.d/docker.list"
sudo apt-get update

# install the docker
sudo apt-get install lxc-docker



Install script

#!/bin/bash
# run this script as a root

function install_docker(){
   # for https transport
   [ -e /usr/lib/apt/methods/https ] || {
     sudo apt-get -y update
     sudo apt-get -y install apt-transport-https
   }

   # for apt-key
   sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9

   # update the apt-repository
   sh -c "echo deb https://get.docker.io/ubuntu docker main\
> /etc/apt/sources.list.d/docker.list"
   sudo apt-get -y update

   # install the docker
   sudo apt-get -y install lxc-docker
}

install_docker


Verification
# jump into the container
sudo docker run -i -t ubuntu /bin/bash    
(try ubuntu 14.04 LTS? sudo docker run -i -t ubuntu:14.04 /bin/bash)


Use Docker
# list how many images
sudo docker images

# look for more images


# list containers
sudo docker ps -la

# run a command in a container
sudo docker run [container] command
ex:
sudo docker run ubuntu:14.04 echo "hello world"

# Install package
sudo docker run ubuntu:14.04 apt-get install ssh

Creating an image
Step 1: List the image status
sudo docker ps -l

Step 2: Create new image
sudo docker commit
ex:

Check
sudo docker images
 

Networking
(edit)

References




[nodejs, async, promise] 最簡單的方式使用 promise

http://mqjing.blogspot.com/2016/11/nodejs-async-promise-promise.html

How to use promise
GitHub: Download
(This picture was taken from here)
Google doc: This document.
'use strict';

// 定義一個高耗時非同步 function: c 毫秒後, 才會 resolving 輸入的 x
function LongTimeTask(x, c) {

 // 高耗時非同步工作
 return new Promise(resolve => {
   setTimeout(() => {
     resolve(x);
   }, c);
 });
}



function main() {
 // Step 1: Run the first LongTimeTask which takes 4 seconds
 let xPromise1 = LongTimeTask("First", 4000);
 xPromise1.then(function (x) {
    // 四秒後, 才會執行到這裡
    console.log(x);
 });

 // Step 2: Run the second LongTimeTask whick takes 2 seconds
 let xPromise2 = LongTimeTask("Second", 1000);
 xPromise2.then(function (x) {
    // 一秒鐘後, 就會執行到這裡
    console.log(x);
 });

 // Step 3: 希望先執行 Third_1 task, 然後才是 Third_2 Task
 //               在這裡使用巢狀的執行
 let xPromise3 = LongTimeTask("Third_1", 4000);
 xPromise3.then(function (x) {
    // 四秒鐘, 會執行到這裡
    console.log(x);
   return LongTimeTask("Thrid_2", 2000);
 }).then( v => {
    // 當 Third_1 執行完成後, 再執行這裡
    console.log(v);
 });
}


main();






References

  1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function


[async, new command] 最簡單的方式安裝使用 async/await

http://mqjing.blogspot.com/2016/11/async-new-command-asyncawait.html

How to use async/await
GitHub: Download

Google doc: This document.
非同步處理是學習使用 node.js 的核心, 現在有新指令 async/await 即將發佈. 將大幅改善程式碼處理非同步的問題.  網路上有一堆範例程式, 但是怎麼執行呢? 要怎麼嘗鮮使用呢?  現在來示範一下, 我完全參考這篇的做法.

因為 V8 已經把 async/await 等新的處理非同步功能 build 進 night build 了. 只要拉 v8 nightbuild code, 然後就可以享受 async/await 的最新功能. 你不需要再像以前一樣, 弄一堆煩死人的 babel 相關的套件.

下面提供兩種方式, 讓你玩新指令.
  1. Docker Version: 只要三個指令, 完全攻略自動安裝完成. 讓你專注在 js code 上.
  2. 手動 Version: 一步一步安裝必要套件與設定, 讓你知道 Docker 版本的 script 是怎麼做的.

Enjoy & Good Luck.
Jing.

Docker Version

# pull the example
cd ./example/async_new_function

# Step 1:  設定使用 nightbuild 版本 node js
. ./01_docker_init.sh

# Step 2: 寫 code
vi example.js

# Step 3: Run
. ./docker_run.sh
E.g.


Manual Version

Step 1:  設定使用 nightbuild 版本 node js
#!/bin/bash

function install_nightbuild(){
 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash

 # load nvm
 export NVM_DIR="/root/.nvm"
 [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"


 NVM_NODEJS_ORG_MIRROR=https://nodejs.org/download/nightly
 nvm install 7
 nvm use 7
}

install_nightbuild


E.g.


Step 2: 寫 code
'use strict';

// app.js
const timeout = function (delay) {  
 return new Promise((resolve, reject) => {
   setTimeout(() => {
     resolve()
   }, delay)
 })
}

async function timer () {  
 console.log('timer started')
 await Promise.resolve(timeout(100));
 console.log('timer finished')
}

timer()


Step 3: Run
node --harmony-async-await example.js

Verification

Reference