copyDockerConfigIntoContainer takes the client configuration and copies it into the container. The path should be an absolute path in the container, commonly root/.docker/config.json.
(ctx context.Context, apiClient client.APIClient, containerID string, configPath string, config *configfile.ConfigFile)
| 401 | // The path should be an absolute path in the container, commonly |
| 402 | // /root/.docker/config.json. |
| 403 | func copyDockerConfigIntoContainer(ctx context.Context, apiClient client.APIClient, containerID string, configPath string, config *configfile.ConfigFile) error { |
| 404 | var configBuf bytes.Buffer |
| 405 | if err := config.SaveToWriter(&configBuf); err != nil { |
| 406 | return fmt.Errorf("saving creds: %w", err) |
| 407 | } |
| 408 | |
| 409 | // We don't need to get super fancy with the tar creation. |
| 410 | var tarBuf bytes.Buffer |
| 411 | tarWriter := tar.NewWriter(&tarBuf) |
| 412 | _ = tarWriter.WriteHeader(&tar.Header{ |
| 413 | Name: configPath, |
| 414 | Size: int64(configBuf.Len()), |
| 415 | Mode: 0o600, |
| 416 | }) |
| 417 | |
| 418 | if _, err := io.Copy(tarWriter, &configBuf); err != nil { |
| 419 | _ = tarWriter.Close() |
| 420 | return fmt.Errorf("writing config to tar file for config copy: %w", err) |
| 421 | } |
| 422 | |
| 423 | if err := tarWriter.Close(); err != nil { |
| 424 | return fmt.Errorf("closing tar for config copy failed: %w", err) |
| 425 | } |
| 426 | |
| 427 | _, err := apiClient.CopyToContainer(ctx, containerID, client.CopyToContainerOptions{ |
| 428 | DestinationPath: "/", |
| 429 | Content: &tarBuf, |
| 430 | }) |
| 431 | if err != nil { |
| 432 | return fmt.Errorf("copying config.json into container failed: %w", err) |
| 433 | } |
| 434 | |
| 435 | return nil |
| 436 | } |
no test coverage detected
searching dependent graphs…