-1

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

M.abdelrhman
  • 958
  • 14
  • 24
  • and what is the problem exactly? like are there any errors? – Abdusalam mohamed Jan 29 '22 at 14:46
  • No no errors, i want for example --syncmode full to be specified in golang code above or --rinkeby and i don't how to specify this – M.abdelrhman Jan 29 '22 at 14:50
  • you want to access those arguments?? – Abdusalam mohamed Jan 29 '22 at 14:52
  • yes exactly i want to access them and be able to change their values – M.abdelrhman Jan 29 '22 at 14:53
  • for example sync mode could be snap, full or light i want be able to control which mode the container gonna be in the creation process of the container the same way we do it here docker run --name rinkeby-node ethereum/client-go --rinkeby --syncmode full but with code in my golang application – M.abdelrhman Jan 29 '22 at 14:55
  • alright in that case you can import `os` package and access them using `os.Args` or even better use https://pkg.go.dev/flag – Abdusalam mohamed Jan 29 '22 at 14:59
  • What parts of the [`docker run` command syntax](https://docs.docker.com/engine/reference/commandline/run/) do those options correspond to? Are there corresponding settings in the Go Docker SDK you might set? What have you already tried? – David Maze Jan 29 '22 at 15:39
  • @DavidMaze the corresponding setting in the Docker SDK is what mehdy specified in the accepted answer. – M.abdelrhman Jan 29 '22 at 15:45

1 Answers1

1

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

mehdy
  • 3,174
  • 4
  • 23
  • 44