so as the title says, I'm trying to execute a simple command inside entrypoint using golang docker sdk (docker api).
func RunBatch(imageName, containerName string, entrypoint []string, volumes []string) string {
ctx := context.Background()
c := getClient()
cfg := &container.Config{Entrypoint: entrypoint, Tty: true, Image: imageName}
hostCfg := &container.HostConfig{Mounts: make([]mount.Mount, len(volumes))}
netCfg := &network.NetworkingConfig{}
startCfg := types.ContainerStartOptions{}
for i := range volumes {
vols := strings.Split(volumes[i], ":")
hostCfg.Mounts[i] = mount.Mount{
Type: mount.TypeBind,
Source: config.Config.BaseDir + vols[0],
Target: vols[1],
}
}
resp, err := c.ContainerCreate(ctx, cfg, hostCfg, netCfg, containerName)
if err != nil {
log.Fatal().Err(err)
}
err = c.ContainerStart(ctx, resp.ID, startCfg)
if err != nil {
log.Fatal().Err(err)
}
_, err = c.ContainerWait(ctx, resp.ID)
if err != nil {
log.Fatal().Err(err)
}
err = c.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{})
if err != nil {
log.Fatal().Err(err)
}
return resp.ID
}
and the entrypoint I'm passing here is ["touch", "/app/$(date +'%T')"]
but the created file looks like $(date +'%T')
, I've also tried and failed with ${date +'%T'}
and with backqoute as well.
how can I execute those ?!
Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, ENTRYPOINT [ "echo", "$HOME" ]
will not do variable substitution on $HOME
. If you want shell processing then either use the shell form or execute a shell directly, for example: ENTRYPOINT [ "sh", "-c", "echo $HOME" ]
.