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

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