Search code examples
dockergodocker-api

how to specify sync-mode and network type while creating docker container using docker go pkg in Golang application?


I am trying to create docker container from my golang application using Docker Engine SDKs and Docker API

this is the command i want to implement in my application:

docker run --name rinkeby-node ethereum/client-go --rinkeby --syncmode full

this is the code i am using

    ctx := context.Background()
    cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
    if err != nil {
        panic(err)
    }
    imageName := "ethereum/client-go"
    out, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{})
    if err != nil {
        panic(err)
    }
    defer out.Close()
    io.Copy(os.Stdout, out)


    resp, err := cli.ContainerCreate(ctx, &container.Config{
        Image: imageName,
    }, nil, nil, nil, "containerName")
    if err != nil {
        panic(err)
    }

    if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
        panic(err)
    }
    fmt.Println(resp.ID) 

now i want to specify the syncmode to full and the network to rinkeby


Solution

  • The --syncmode full and --rinkeby flags are the CMD arguments.

    So when calling you're calling ContainerCreate method inside of container.Config add this:

    Cmd: []string{"--syncmode", "full", "--rinkeby"}
    

    For a complete example see this