diff --git a/go/common/docker/docker.go b/go/common/docker/docker.go index e299330b48..1726c78349 100644 --- a/go/common/docker/docker.go +++ b/go/common/docker/docker.go @@ -21,8 +21,8 @@ import ( const _networkName = "node_network" -// volumes is a map from volume name to dir name it will have within the container. If a volume doesn't exist this will create it. -func StartNewContainer(containerName, image string, cmds []string, ports []int, envs, devices, volumes map[string]string) (string, error) { +// StartNewContainer - volumes is a map from volume name to dir name it will have within the container. If a volume doesn't exist this will create it. +func StartNewContainer(containerName, image string, cmds []string, ports []int, envs, devices, volumes map[string]string, autoRestart bool) (string, error) { ctx := context.Background() cli, err := client.NewClientWithOpts(client.FromEnv) if err != nil { @@ -95,21 +95,27 @@ func StartNewContainer(containerName, image string, cmds []string, ports []int, "max-file": "3", } + hc := container.HostConfig{ + PortBindings: portBindings, + Mounts: mountVolumes, + Resources: container.Resources{Devices: deviceMapping}, + LogConfig: container.LogConfig{Type: "json-file", Config: logOptions}, + } + + if autoRestart { + hc.RestartPolicy = container.RestartPolicy{Name: "unless-stopped"} + } + // create the container - resp, err := cli.ContainerCreate(ctx, &container.Config{ - Image: image, - Entrypoint: cmds, - Tty: false, - ExposedPorts: exposedPorts, - Env: envVars, - }, - &container.HostConfig{ - PortBindings: portBindings, - Mounts: mountVolumes, - RestartPolicy: container.RestartPolicy{Name: "unless-stopped"}, - Resources: container.Resources{Devices: deviceMapping}, - LogConfig: container.LogConfig{Type: "json-file", Config: logOptions}, + resp, err := cli.ContainerCreate(ctx, + &container.Config{ + Image: image, + Entrypoint: cmds, + Tty: false, + ExposedPorts: exposedPorts, + Env: envVars, }, + &hc, &network.NetworkingConfig{ EndpointsConfig: map[string]*network.EndpointSettings{ _networkName: { diff --git a/go/node/docker_node.go b/go/node/docker_node.go index 78dc27d277..48d750f7e9 100644 --- a/go/node/docker_node.go +++ b/go/node/docker_node.go @@ -130,7 +130,7 @@ func (d *DockerNode) startHost() error { d.cfg.hostP2PPort, } - _, err := docker.StartNewContainer(d.cfg.nodeName+"-host", d.cfg.hostImage, cmd, exposedPorts, nil, nil, nil) + _, err := docker.StartNewContainer(d.cfg.nodeName+"-host", d.cfg.hostImage, cmd, exposedPorts, nil, nil, nil, true) return err } @@ -195,7 +195,7 @@ func (d *DockerNode) startEnclave() error { // we need the enclave volume to store the db credentials enclaveVolume := map[string]string{d.cfg.nodeName + "-enclave-volume": _enclaveDataDir} - _, err := docker.StartNewContainer(d.cfg.nodeName+"-enclave", d.cfg.enclaveImage, cmd, exposedPorts, envs, devices, enclaveVolume) + _, err := docker.StartNewContainer(d.cfg.nodeName+"-enclave", d.cfg.enclaveImage, cmd, exposedPorts, envs, devices, enclaveVolume, true) return err } @@ -222,7 +222,7 @@ func (d *DockerNode) startEdgelessDB() error { //dbVolume := map[string]string{d.cfg.nodeName + "-db-volume": "/data"} //_, err := docker.StartNewContainer(d.cfg.nodeName+"-edgelessdb", d.cfg.edgelessDBImage, nil, nil, envs, devices, dbVolume) - _, err := docker.StartNewContainer(d.cfg.nodeName+"-edgelessdb", d.cfg.edgelessDBImage, nil, nil, envs, devices, nil) + _, err := docker.StartNewContainer(d.cfg.nodeName+"-edgelessdb", d.cfg.edgelessDBImage, nil, nil, envs, devices, nil, true) return err } diff --git a/testnet/launcher/eth2network/docker.go b/testnet/launcher/eth2network/docker.go index 0a2b5d4e4e..bed408d44a 100644 --- a/testnet/launcher/eth2network/docker.go +++ b/testnet/launcher/eth2network/docker.go @@ -48,7 +48,7 @@ func (n *Eth2Network) Start() error { // keep a volume of binaries to avoid downloading volume := map[string]string{"eth2_bin": "/home/obscuro/go-obscuro/integration/.build/eth2_bin/"} - _, err := docker.StartNewContainer("eth2network", "testnetobscuronet.azurecr.io/obscuronet/eth2network:latest", cmds, exposedPorts, nil, nil, volume) + _, err := docker.StartNewContainer("eth2network", "testnetobscuronet.azurecr.io/obscuronet/eth2network:latest", cmds, exposedPorts, nil, nil, volume, false) return err } diff --git a/testnet/launcher/faucet/docker.go b/testnet/launcher/faucet/docker.go index c6d9fd734a..33cca7dcbf 100644 --- a/testnet/launcher/faucet/docker.go +++ b/testnet/launcher/faucet/docker.go @@ -32,7 +32,7 @@ func (n *DockerFaucet) Start() error { "--serverPort", fmt.Sprintf("%d", n.cfg.faucetPort), } - _, err := docker.StartNewContainer("faucet", n.cfg.dockerImage, cmds, []int{n.cfg.faucetPort}, nil, nil, nil) + _, err := docker.StartNewContainer("faucet", n.cfg.dockerImage, cmds, []int{n.cfg.faucetPort}, nil, nil, nil, false) return err } diff --git a/testnet/launcher/fundsrecovery/docker.go b/testnet/launcher/fundsrecovery/docker.go index c6211807a4..d04a747c8d 100644 --- a/testnet/launcher/fundsrecovery/docker.go +++ b/testnet/launcher/fundsrecovery/docker.go @@ -52,7 +52,7 @@ func (n *FundsRecovery) Start() error { `, n.cfg.l1HTTPURL, n.cfg.l1privateKey), } - containerID, err := docker.StartNewContainer("recover-funds", n.cfg.dockerImage, cmds, nil, envs, nil, nil) + containerID, err := docker.StartNewContainer("recover-funds", n.cfg.dockerImage, cmds, nil, envs, nil, nil, false) if err != nil { return err } diff --git a/testnet/launcher/gateway/docker.go b/testnet/launcher/gateway/docker.go index 7be1786b72..cf11fbfeb6 100644 --- a/testnet/launcher/gateway/docker.go +++ b/testnet/launcher/gateway/docker.go @@ -35,7 +35,7 @@ func (n *DockerGateway) Start() error { "--logPath", "sys_out", } - _, err := docker.StartNewContainer("gateway", n.cfg.dockerImage, cmds, []int{n.cfg.gatewayHTTPPort, n.cfg.gatewayWSPort}, nil, nil, nil) + _, err := docker.StartNewContainer("gateway", n.cfg.dockerImage, cmds, []int{n.cfg.gatewayHTTPPort, n.cfg.gatewayWSPort}, nil, nil, nil, true) return err } diff --git a/testnet/launcher/l1contractdeployer/docker.go b/testnet/launcher/l1contractdeployer/docker.go index 071a889351..ce9aeb01d5 100644 --- a/testnet/launcher/l1contractdeployer/docker.go +++ b/testnet/launcher/l1contractdeployer/docker.go @@ -58,7 +58,7 @@ func (n *ContractDeployer) Start() error { `, n.cfg.l1HTTPURL, n.cfg.privateKey), } - containerID, err := docker.StartNewContainer("hh-l1-deployer", n.cfg.dockerImage, cmds, ports, envs, nil, nil) + containerID, err := docker.StartNewContainer("hh-l1-deployer", n.cfg.dockerImage, cmds, ports, envs, nil, nil, false) if err != nil { return err } diff --git a/testnet/launcher/l2contractdeployer/docker.go b/testnet/launcher/l2contractdeployer/docker.go index 6c185f4f93..5b0d9a5839 100644 --- a/testnet/launcher/l2contractdeployer/docker.go +++ b/testnet/launcher/l2contractdeployer/docker.go @@ -82,7 +82,7 @@ func (n *ContractDeployer) Start() error { `, n.cfg.l1HTTPURL, n.cfg.l1privateKey, n.cfg.l2PrivateKey, n.cfg.hocPKString, n.cfg.pocPKString), } - containerID, err := docker.StartNewContainer("hh-l2-deployer", n.cfg.dockerImage, cmds, ports, envs, nil, nil) + containerID, err := docker.StartNewContainer("hh-l2-deployer", n.cfg.dockerImage, cmds, ports, envs, nil, nil, false) if err != nil { return err }