Fork me on GitHub 第 3 頁 | Jeff Yang

Docker-compose 2

在 [docker-compose 1] 已經完成安裝compose及執行一個container

現在來測試一次跑多組containers

首先,編譯第一個container所需執行的程式 [Connect to mysql from golang]

更改 main()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
func main() { 
ip := "10.62.8.175:3306"
dbName := "test"
dbString = "root:123456@tcp(" + ip + ")/" + dbName
tableName := "jeff"
runtime.GOMAXPROCS(runtime.NumCPU())
var dbs DBStruct
dbs.dbOpen()
dbs.dbCreate(tableName)
dbs.dbInsert(tableName)
dbs.dbClose()
fmt.Println("===> ",time.Now().Format("20060102150405"))
//getchar()...防止程式執行完後直接跳出
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
}


go build -o goSql goSql.go

再來,編譯第二個container所需執行的程式

更改

1
2
3
4
5
func main() {
...
tableName := "jeff_2"
...
}


go build -o goSql_2 goSql.go

更改 docker-compose.yml


build:
 container_name: jeff
 image: centos
 ports: 
   - 9080:9080
 volumes:
   - /home/jeff/code/:/home/code
 restart: always 
 stdin_open: true
 tty: true
 command: bash -c "/home/code/goSql"
build_2:
 container_name: jeff_2
 image: centos
 ports: 
   - 9081:9080
 volumes:
   - /home/jeff/code/:/home/code
 restart: always 
 stdin_open: true
 tty: true
 command: bash -c "/home/code/goSql_2"

執行

docker-compose up -d

Check…

docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80ac6694738f centos “bash -c /home/code/g" About a minute ago Up 58 seconds 0.0.0.0:9081->9080/tcp jeff_2
a6cb9b81c8e6 centos “bash -c /home/code/g" 4 minutes ago Up 57 seconds 0.0.0.0:9080->9080/tcp jeff

再確認兩筆資料表有寫入資料庫

Docker-compose 1

欲控制多組containers,可以使用現有manage multi-processes軟體如:Supervisor…等
但也許會出現一些問題 http://stackoverflow.com/questions/30034813/best-way-to-manage-docker-containers-with-supervisord

Docker官方提供Compose來執行控制多組containers

https://github.com/docker/compose/

Compose is a tool for defining and running multi-container Docker applications.

安裝…

1
2
3
$ wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
$ sudo easy_install pip
$ sudo pip install -U docker-compose

docker run 轉成 docker-compose yaml 語法…

1
2
3
4
5
6
7
8
9
10
11
12
13
# docker run -itd --name jeff --restart=always -p 9080:9080  -v /home/jeff/code/:/home/code centos

# docker-compose.yml
build:
container_name: jeff
image: centos
ports:
- 9080:9080
volumes:
- /home/jeff/code/:/home/code
restart: always
stdin_open: true
tty: true

同一目錄下執行…


docker-compose up -d

 

Check…


docker inspect jeff

 

Docker 網路連線

CentOS7 下安裝 docker

1
$sudo yum install docker
1
$sudo service docker start
1
$sudo chkconfig docker on

安裝完成後,執行以下指令

1
$docker run -it --name jeff -p 9080:9080 -v /home/jeff/code/:/home/code centos /bin/bash

-v /home/jeff/code/:/home/code 掛載Host主機下目錄

一些問題

  • 當進入container掛載目錄後,執行ls發生錯誤

    [root@a52a9c808ba5 code]# ls: cannot open directory .: Permission denied

    解法:
    在Host掛載目錄下執行

    1
    $chcon -Rt svirt_sandbox_file_t ./code/
  • 容器要想存取外部網路,需要host支援forward

    1
    2
    $sysctl net.ipv4.ip_forward
    net.ipv4.ip_forward = 1

    如果為 0,說明沒有開啟轉發,則需要手動打開。

    1
    $sysctl -w net.ipv4.ip_forward=1
    1
    $service docker restart
  • 進入container後ping外部ip確認是否成功連線

    1
    2
    3
    [root@a52a9c808ba5 /]# ping 10.62.8.175
    PING 10.62.8.175 (10.62.8.175) 56(84) bytes of data.
    64 bytes from 10.62.8.175: icmp_seq=7 ttl=127 time=1.01 ms

參考:

[1] https://github.com/philipz/docker_practice

[2] http://blog.scottlowe.org/2013/09/04/introducing-linux-network-namespaces/

[3] http://www.infoq.com/cn/articles/docker-network-and-pipework-open-source-explanation-practice#rd

Connect to mysql from golang

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main

import (
"database/sql"
"fmt"
"runtime"
_ "github.com/go-sql-driver/mysql"
"time"
)

type DBStruct struct {
db *sql.DB
}

var dbString string

func (dbs *DBStruct) dbOpen() (err error) {
dbs.db, err = sql.Open("mysql", dbString)
if err != nil {
fmt.Println("ERROR dbOpen")
}
return err
}

func (dbs *DBStruct) dbClose() (err error) {
err = dbs.db.Ping()
if err != nil {
fmt.Println(err)
return err
}
defer dbs.db.Close()
return err
}

func (dbs *DBStruct) dbCreate(tableName string) (err error) {
err = nil
err = dbs.db.Ping()
if err != nil {
fmt.Println(err)
return err
}
c0 := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s", tableName)
c1 := fmt.Sprintf("%s ( id INT AUTO_INCREMENT PRIMARY KEY, ", c0)
c2 := fmt.Sprintf("%s LastUptTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP) ", c1)
if _, err = dbs.db.Exec(c2); err != nil {
fmt.Println(err)
return err
}
return err
}

func (dbs *DBStruct) dbInsert(tableName string) (err error) {
err = dbs.db.Ping()
if err != nil {
fmt.Println(err)
return err
}
t := time.Now()
var INSERT string
INSERT = fmt.Sprintf("INSERT INTO %s (LastUptTime) VALUES( '%s') ", tableName, t.Format("20060102150405"))
if _, err = dbs.db.Exec(INSERT); err != nil {
fmt.Println(err)
return err
}
return err
}

func main() {
ip := "10.62.8.175:3306"
dbName := "test"
dbString = "root:123456@tcp(" + ip + ")/" + dbName
tableName := "jeff"
runtime.GOMAXPROCS(runtime.NumCPU())
var dbs DBStruct
dbs.dbOpen()
dbs.dbCreate(tableName)
dbs.dbInsert(tableName)
dbs.dbClose()
}