2014/07/31

docker on Linux Mint 初體驗 (resolve problem: Cannot connect to the Docker daemon. Is 'docker -d' running on this host?)


[ Quick Solution Mode ]
$  sudo docker -d                   # check docker daemon is running
# if not and you see the error message like
"stat /sbin/apparmor_parser: no such file or directory ()
[916c0d18] -job initserver() = ERR (1)"

$ sudo apt-get install apparmor     # install apparmor package
$ sudo service docker start         # start up docker service (daemon)
$ sudo docker run -i -t ubuntu /bin/bash     # it should be worked

[ Verbose Mode ]
今年 COSCUP 上一直聽到一個很火紅的東西就是 docker,
他有點像 VM ,但運作原理和速度卻跟我們一般熟悉的 VM 大不相同,
簡單利用 VM 的概念來對應 docker,
可以想像成 VMWare, VMPlayer, VirtualBox, docker 是同一層,
用來承載另一個系統,
而用 VMWare, VirtualBox 產生的 VMs, 對應 docker 則稱為 images,

而 docker 運用一些機制(lxc, cgroup, aufs...), 來作到 images 之間
能像一個 VM 一般獨立運作執行, 能擁有自己的環境空間,
且同時又是直接使用實際的硬體資源,
而不像以前的 VM 一樣只能使用預先分配的資源,
使用機制的進一步觀念請參考最下面附的連結.

既然已簡單了解 docker 是什麼,
那就趕快來跟著官方教學來 step by step 囉...

Installation steps please follow docker user guide (choose 'Ubuntu' OS)
If you try to verify the docker installation has worked, you see the message "Cannot connect to the Docker daemon. Is 'docker -d' running on this host?", you may try this...
$ sudo docker run -i -t ubuntu /bin/bash
2014/07/31 19:38:42 Cannot connect to the Docker daemon. Is 'docker -d' running on this host?
# as the user guide, try to verify that the installation has worked
# by downloading the ubuntu image and launching a container.

# Two way to check 'docker -d' is running
# way I (if running, you should see "/usr/bin/docker -d")
$ ps aux | grep docker

# way II
$  sudo docker -d
2014/07/31 19:39:28 docker daemon: 1.1.2 d84a070; execdriver: native; graphdriver: 
[916c0d18] +job initserver()
[916c0d18.initserver()] Creating server
[916c0d18] +job init_networkdriver()
[916c0d18] +job serveapi(unix:///var/run/docker.sock)
2014/07/31 19:39:28 Listening for HTTP on unix (/var/run/docker.sock)
[916c0d18] -job init_networkdriver() = OK (0)
2014/07/31 19:39:28 WARNING: Your kernel does not support cgroup swap limit.
Error loading docker apparmor profile: exec: "/sbin/apparmor_parser": stat /sbin/apparmor_parser: no such file or directory ()
[916c0d18] -job initserver() = ERR (1)
2014/07/31 19:39:28 Error loading docker apparmor profile: exec: "/sbin/apparmor_parser": stat /sbin/apparmor_parser: no such file or directory ()

# the error message finger out the not-installed package 'apparmor'
$ sudo apt-get install apparmor

# check it is worked
$ sudo docker -d
2014/07/31 19:41:27 docker daemon: 1.1.2 d84a070; execdriver: native; graphdriver: 
[abe22d40] +job serveapi(unix:///var/run/docker.sock)
[abe22d40] +job initserver()
[abe22d40.initserver()] Creating server
[abe22d40] +job init_networkdriver()
2014/07/31 19:41:27 Listening for HTTP on unix (/var/run/docker.sock)
[abe22d40] -job init_networkdriver() = OK (0)
2014/07/31 19:41:27 WARNING: Your kernel does not support cgroup swap limit.
2014/07/31 19:41:27 Local (127.0.0.1) DNS resolver found in resolv.conf and containers can't use it. Using default external servers : [8.8.8.8 8.8.4.4]
Loading containers: : done.
[abe22d40.initserver()] Creating pidfile
[abe22d40.initserver()] Setting up signal traps
[abe22d40] -job initserver() = OK (0)
[abe22d40] +job acceptconnections()
[abe22d40] -job acceptconnections() = OK (0)
# Ctrl + C to exit

$ sudo service docker start                # start up docker service (daemon)
docker start/running, process 13955

$ ps aux | grep docker
root     13955  1.2  0.1 331860  6376 ?  Ssl  02:05   0:00 /usr/bin/docker -d

$ sudo docker run -i -t ubuntu /bin/bash   # now try to run a image again, it should be ok
Unable to find image 'ubuntu' locally
Pulling repository ubuntu
ba5877dc9bec: Download complete 
511136ea3c5a: Download complete 
9bad880da3d2: Download complete 
25f11f5fb0cb: Download complete 
ebc34468f71d: Download complete 
2318d26665ef: Download complete 
還搞不清楚什麼是 docker 和他的原理嗎~
可以參考這幾篇說明喔...
5分鐘弄懂Docker!
docker 原理簡介

還有 slideshare 的投影片
Docker: an insider view



[ Reference ]
https://groups.google.com/forum/#!topic/docker-user/_BQ89Zi04-I
https://www.digitalocean.com/community/questions/start-docker-error-message

使用 git stash 保存工作狀態 ( Save working status by `git stash` )


[ Quick Solution Mode ]
$ git stash                # save working status
$ git stash save "$name"   # save working status with a specific name ($name)
$ git stash list           # List the stashes that you have
$ git stash pop            # popup and remove the latest stash
$ git stash apply          # get the latest stash but keep the stash
$ git stash drop stash@{x} # drop a record named stash@{x}
$ git stash show stash@{x} # show changed files named stash@{x}
$ git stash show -p stash@{x}    # show stash@{x} detail diff 
$ git stash --include-untracked  # to stash untracked file

[ Verbose Mode ]
使用 git 做版本控管時,
有一個很好用的功能,
就是用 git stash 來暫存你的變更,
這麼做有什麼好處跟目的?

思考一下開發時可能遇到的情形...
開發到一半,臨時需要切換到另一個 branch 開發,
有可能是突然想到另一個 branch bug的解法,
或被 assign 先處理另一件事,
或是想暫時恢復未 commmit 前的狀態, 看看修改前的狀況,
不管如何,就是有需要切換 branch 或恢復成未修改的狀態,
但目前的功能做一半,又不好 commit 一個半成品,

這個情況就可以用 stash 來將目前所有的修改儲存起來,(有點像暫存區)
等到需要時再取出來, 繼續開發,
`git satsh` 的 help 的說明如下...
`git stash` help:
usage: git stash list []
   or: git stash show []
   or: git stash drop [-q|--quiet] []
   or: git stash ( pop | apply ) [--index] [-q|--quiet] []
   or: git stash branch []
   or: git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
         [-u|--include-untracked] [-a|--all] []]
   or: git stash clear
可以查 Git Manual, 取得更詳細的說明
$ git stash --help or man git stash
GIT-STASH(1)                      Git Manual                      GIT-STASH(1)

NAME
       git-stash - Stash the changes in a dirty working directory away

SYNOPSIS
       git stash list []
       git stash show []
       git stash drop [-q|--quiet] []
       git stash ( pop | apply ) [--index] [-q|--quiet] []
       git stash branch  []
       git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
                    [-u|--include-untracked] [-a|--all] []]
       git stash clear
       git stash create

簡單來說, 要保存到暫存區,
可以執行
$ git stash                 # 自動保存
# OR
$ git stash save "$name"    # 存成自訂的名字, 好處是可讀性高, 如果保存了好幾筆記錄,
                              才不會看不出哪個是哪個

$ git stash list            # 保存後, 可用此來查看暫存區內保存了哪些暫存記錄
$ git stash show            # 用來查看某筆暫存記錄變更的檔案和內容

$ git pop                   # 直接取出最後一筆暫存記錄
$ git pop "$name"           # 取出某一筆暫存記錄

$ git drop "$name"          # 取出記錄繼續開發後,就不再需要這筆暫存記錄了,用 drop 來刪除吧
                              eg. git stash drop stash@{1}

查看 MySQL 的存取記錄 (View MySQL access log on MySQL Server)


[ Quick Solution Mode ]
$ sudo vi /etc/mysql/my.cnf
# unmark the following lines: (your could search "mysql.log")
general_log_file        = /var/log/mysql/mysql.log
general_log             = 1

$ sudo service mysql restart
# Now you could view all the MySQL access log
$ cat /var/log/mysql/mysql.log         # view whole file
# OR
$ tail -f /var/log/mysql/mysql.log     # view file output appended data as the file grows

[ Verbose Mode ]
不論是為了什麼原因使用資料庫,
總是有可能希望知道網頁對資料庫下了什麼語法,
或想知道資料庫的查詢,存取記錄,

使用 MySQL 做為資料庫時,
預設是沒有開啟 "存取記錄" 這項功能的,
需要手動開啟,
步驟和作法如上述,
記得修改後要重新啟動 mysql

[ Environment ]
$ mysql --version
mysql  Ver 14.14 Distrib 5.5.35, for debian-linux-gnu (x86_64) using readline 6.3



[ Reference ]
Tsung 還有提供其他方法, 可以參考看看~ http://blog.longwin.com.tw/2007/06/mysql_record_any_sql_command_2007/

2014/07/10

ssh 到Linux 很慢的問題 (slow ssh connection to Linux)


[ Quick Solution Mode ]
eg. ssh tchsu@192.168.1.1    SLOW!!!!    NO~~~ >"<
$ vi /etc/ssh/sshd_conf     # on 192.168.1.1
# add or modify to below:
UseDNS no
GSSAPIAuthentication no

$ sudo service ssh restart

[ Verbose Mode ]
當SSH到某台 Linux 時, 有時候會發現連線速度很慢,
比較常見的問題是因為 target Linux DNS Name 的反解析卡住,
或是在等待GSSAPI 認證

以筆者常使用的環境來說,
通常是 ssh 到 VM 裡的 Linux,
eg. ssh tchsu@92.168.x.x
當發生 ssh 很慢時,
就是因為連線目標的Linux 在做 DNS Name 的反解析,
以VM為例, 若VM的Linux 沒有連上網路時 就會很慢,
因為要等 DNS query timeout,
若有連上網路仍很慢, 就有可能是機房的DNS解析有問題,

Anyway, 要解決這個問題,
就得修改目標Linux 的ssh設定,
讓 SSH 時 不要做 DNS Name 解析,
在 SSH Server (目標 Linux ) 端,
有一份 /etc/ssh/sshd_conf 的文件,
裡面記錄了 SSH Server 相關的配置,
sshd 的 d 指的是 Daemon,
也就是 SSH Server, 你 local 是 SSH Client,
加上或修改成  UseDNS no,
修改完記得 restart ssh
$ sudo service ssh restart

有時候改了DNS 為何還是很慢,
這時有可能是另一個問題,
也就是在嘗試 GSSAPIAuthentication 認證,
這時一樣修改 sshd_conf,
加上或修改成 GSSAPIAuthentication no,


ps.
1. 不知道什麼是GSSAPIAuthentication, 可以 man sshd_conf
GSSAPIAuthentication
Specifies whether user authentication based on GSSAPI is allowed.
The default is Note that this option applies to protocol version 2 only.

2. ssh -v xx@x.x.x.x
verbose mode, 可以看到連線時詳細的過程, 可以作為問題發生的參考

3. 不修改 sshd_conf 的話, 可以將 192.168.x.x 寫進 /etc/resolv.conf