Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

348
Visualizações
Docker Golang API create a container with files from memory

Currently, to get files into a container using the golang api I first must create the container and then use the CopyToContainer function (Example Below).

Is it possible to create a container and specify files for it to have at create time, without having the files first on the file system?

Example 1)

func main() {
    cli, err := client.NewEnvClient()
    if err != nil {
        panic(err)
    }

    resp, err := cli.ContainerCreate(context.Background(),
        &container.Config{
            Image: "alpine",
            Cmd:   []string{"ls", "/"},
        }, nil, nil, "testContainer")
    if err != nil {
        panic(err)
    }

    fmt.Printf("Created: %v\n", resp.ID)

    cli.CopyToContainer(context.Background(), resp.ID, "/", getTar(),types.CopyToContainerOptions{})
}

func getTar() io.Reader {
    ...
}

EDIT:
- Code spacing.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I found this solution, but I can't get the file to show up in the container. Try it and let me know if it works for you!

var buf bytes.Buffer
tw := tar.NewWriter(&buf)
err = tw.WriteHeader(&tar.Header{
    Name: filename,            // filename
    Mode: 0777,                // permissions
    Size: int64(len(content)), // filesize
})
if err != nil {
    return nil, fmt.Errorf("docker copy: %v", err)
}
tw.Write([]byte(content))
tw.Close()

// use &buf as argument for content in CopyToContainer
cli.CopyToContainer(context.Background(), resp.ID, "/", &buf,types.CopyToContainerOptions{})
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda