-
Notifications
You must be signed in to change notification settings - Fork 2
/
docker_run_bchsv.go
120 lines (108 loc) · 3.29 KB
/
docker_run_bchsv.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) [2019] [dabank.io]
// [devtools4chains] is licensed under the Mulan PSL v1.
// You can use this software according to the terms and conditions of the Mulan PSL v1.
// You may obtain a copy of Mulan PSL v1 at:
// http://license.coscl.org.cn/MulanPSL
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v1 for more details.
package devtools4chains
import (
"context"
"log"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
)
//bsv const
const (
DockerBSVImage = "bitcoinsv/bitcoin-sv:1.0.1"
)
// DockerBSVInfo .
type DockerBSVInfo struct {
Docker DockerContainerInfo
RPCPort int
RPCUser string
RPCPwd string
}
/**
--name bsvreg -p 18444:18444 bitcoinsv/bitcoin-sv:1.0.1 bitcoind \
-regtest -txindex -excessiveblocksize=0 -maxstackmemoryusageconsensus=0 \
-server -rpcuser=usr -rpcpassword=pwd
*/
// DockerRunBSV 。
func DockerRunBSV(opt *DockerRunOptions) (KillFunc, *DockerBSVInfo, error) {
const (
port = 18444
rpcUser, rpcPwd = "usr", "pwd"
)
if opt == nil {
opt = &DockerRunOptions{}
}
cli, err := client.NewEnvClient()
if err != nil {
return nothing2do, nil, err
}
if opt.Image == nil {
opt.Image = pstring(DockerBSVImage)
}
err = dockerIsImageExists(cli, *opt.Image)
if err != nil {
return nothing2do, nil, err
}
hostConfig := &container.HostConfig{
AutoRemove: opt.AutoRemove,
// "8888/tcp": [{"HostIp": "","HostPort": "8888"}]
PortBindings: nat.PortMap{"18444": []nat.PortBinding{{HostIP: "0.0.0.0", HostPort: "18444"}}},
PublishAllPorts: true,
Mounts: []mount.Mount{ //可用binds
// {Type: "bind", Target: "/work", Source: workPath},
// {Type: "bind", Target: "/mnt/dev/data", Source: dataPath},
// {Type: "bind", Target: "/mnt/dev/config", Source: configPath},
},
}
cont, err := cli.ContainerCreate(context.Background(), &container.Config{
// AttachStderr: true,
// AttachStdout: true,
// Tty: true,
Image: *opt.Image,
Cmd: []string{
"bitcoind",
"-regtest",
"-txindex",
"-excessiveblocksize=0",
"-maxstackmemoryusageconsensus=0",
"-server",
"-rpcuser=" + rpcUser,
"-rpcpassword=" + rpcPwd,
"-rpcport=18444",
},
ExposedPorts: nat.PortSet{"18444": struct{}{}},
}, hostConfig, &network.NetworkingConfig{}, "")
if err != nil {
return nothing2do, nil, err
}
err = cli.ContainerStart(context.Background(), cont.ID, types.ContainerStartOptions{})
if err != nil {
return nothing2do, nil, err
}
log.Printf("container [%s] started\n", *opt.Image)
return func() {
log.Printf("[info] stop container: %s (autoRemove: %v)\n", *opt.Image, opt.AutoRemove)
if e := cli.ContainerStop(context.Background(), cont.ID, nil); e != nil {
log.Println("[Err] stop container error", e)
}
log.Println("[info] container stopped")
}, &DockerBSVInfo{
Docker: DockerContainerInfo{
ListenPorts: []int{port}, //TODO fix ports
},
RPCUser: rpcUser,
RPCPort: port,
RPCPwd: rpcPwd,
}, nil
}