Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

435
Views
`docker run` usando la API de Golang (Documentos de Docker)

Estoy tratando de usar el tutorial de Docker para recrear una docker run de Docker. Aquí está el siguiente código del tutorial en línea

 package main import ( "io" "os" "github.com/docker/docker/client" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "golang.org/x/net/context" ) func main() { ctx := context.Background() cli, err := client.NewEnvClient() if err != nil { panic(err) } _, err = cli.ImagePull(ctx, "alpine", types.ImagePullOptions{}) if err != nil { panic(err) } resp, err := cli.ContainerCreate(ctx, &container.Config{ Image: "alpine", Cmd: []string{"echo", "hello world"}, }, nil, nil, "") if err != nil { panic(err) } if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { panic(err) } if _, err = cli.ContainerWait(ctx, resp.ID); err != nil { panic(err) } out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true}) if err != nil { panic(err) } io.Copy(os.Stdout, out) }

El problema que veo con esto es que, si la ventana acoplable 'alpina' no está disponible localmente, no obtiene la última versión y termina arrojando un error. por ejemplo, XXXXX$ go run go_docker.go panic: Error: No such image: alpine

 goroutine 1 [running]: panic(0x27ffa0, 0xc4202afa50) /usr/local/go/src/runtime/panic.go:500 +0x1a1 main.main() /Users/rvenkatesh/go_coding/raghu_test_code/go_docker.go:30 +0x592 exit status 2

Pero cuando ejecuto el equivalente de la línea de comandos, veo

 XXXX$ docker run alpine echo hello world Unable to find image 'alpine:latest' locally latest: Pulling from library/alpine 627beaf3eaaf: Pull complete Digest:sha256:58e1a1bb75db1b5a24a462dd5e2915277ea06438c3f105138f97eb53149673c4 Status: Downloaded newer image for alpine:latest hello world

Intenté mirar a través del cliente Go, ¿debo modificar algo con la función ImagePull? ¡Cualquier ayuda aquí sería apreciada!

Aquí está el enlace a los documentos https://docs.docker.com/engine/api/getting-started/

Actualización: probé el mismo tutorial para la versión de python y funcionó bien. Me pregunto si la página de Golang necesita una actualización.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Image.Pull devuelve un io.Reader que debe leer y cerrar; si no lo hace, la conexión se cerrará antes de extraer la imagen.

Simplemente puede descartar el contenido y cerrarlo, luego el tirón funcionará.

over 4 years ago · Santiago Trujillo Report

0

Estaba teniendo el mismo problema, el "Pull" no parecía estar funcionando. Sin embargo, encontré una solución.

1) Modifique su línea de tracción para

 pullstat, err = cli.ImagePull(ctx, "alpine", types.ImagePullOptions{})

y añadir

 io.Copy(os.StdOut,pullstat)

después del ImagePull

No he intentado hacer un

 io.Copy(nil,pullstat)

pero eso está en mi lista de cosas para probar a continuación.

over 4 years ago · Santiago Trujillo Report

0

El cliente de Docker es de código abierto y está escrito en Go, por lo que puede ver exactamente cómo implementaron su versión. Creo que el código relevante está en la función container/create.go pullImage.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!