Files
runtime/apps/runner/pkg/docker/container_configs.go
zeekay ea80dc3638 feat: Add new Dockerfiles and improve containerization
- Add main Dockerfile for production builds
- Add Dockerfile.runtime for secure code execution
- Add Dockerfile.runtime.fixed with improved configuration
- Include .dockerignore for build optimization
- Add integration documentation and publishing guides
- Update API and CLI components for better runtime support
- Enhance sandbox and container management features
2025-08-16 23:03:39 -05:00

123 lines
3.3 KiB
Go

// Copyright 2025 Hanzo Industries Inc.
// SPDX-License-Identifier: AGPL-3.0
package docker
import (
"context"
"errors"
"fmt"
"github.com/hanzoai/runner/cmd/runner/config"
"github.com/hanzoai/runner/pkg/api/dto"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/container"
)
func (d *DockerClient) getContainerConfigs(ctx context.Context, sandboxDto dto.CreateSandboxDTO, volumeMountPathBinds []string) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) {
containerConfig := d.getContainerCreateConfig(sandboxDto)
hostConfig, err := d.getContainerHostConfig(ctx, sandboxDto, volumeMountPathBinds)
if err != nil {
return nil, nil, nil, err
}
networkingConfig := d.getContainerNetworkingConfig(ctx)
return containerConfig, hostConfig, networkingConfig, nil
}
func (d *DockerClient) getContainerCreateConfig(sandboxDto dto.CreateSandboxDTO) *container.Config {
envVars := []string{
"RUNTIME_SANDBOX_ID=" + sandboxDto.Id,
"RUNTIME_SANDBOX_SNAPSHOT=" + sandboxDto.Snapshot,
"RUNTIME_SANDBOX_USER=" + sandboxDto.OsUser,
}
for key, value := range sandboxDto.Env {
envVars = append(envVars, fmt.Sprintf("%s=%s", key, value))
}
return &container.Config{
Hostname: sandboxDto.Id,
Image: sandboxDto.Snapshot,
// User: sandboxDto.OsUser,
Env: envVars,
Entrypoint: sandboxDto.Entrypoint,
AttachStdout: true,
AttachStderr: true,
}
}
func (d *DockerClient) getContainerHostConfig(ctx context.Context, sandboxDto dto.CreateSandboxDTO, volumeMountPathBinds []string) (*container.HostConfig, error) {
var binds []string
binds = append(binds, fmt.Sprintf("%s:/usr/local/bin/runtime:ro", d.daemonPath))
// Mount the plugin if available
if d.computerUsePluginPath != "" {
binds = append(binds, fmt.Sprintf("%s:/usr/local/lib/runtime-computer-use:ro", d.computerUsePluginPath))
}
if len(volumeMountPathBinds) > 0 {
binds = append(binds, volumeMountPathBinds...)
}
hostConfig := &container.HostConfig{
Privileged: true,
ExtraHosts: []string{"host.docker.internal:host-gateway"},
Resources: container.Resources{
CPUPeriod: 100000,
CPUQuota: sandboxDto.CpuQuota * 100000,
Memory: sandboxDto.MemoryQuota * 1024 * 1024 * 1024,
MemorySwap: sandboxDto.MemoryQuota * 1024 * 1024 * 1024,
},
Binds: binds,
}
containerRuntime := config.GetContainerRuntime()
if containerRuntime != "" {
hostConfig.Runtime = containerRuntime
}
filesystem, err := d.getFilesystem(ctx)
if err != nil {
return nil, err
}
if filesystem == "xfs" {
hostConfig.StorageOpt = map[string]string{
"size": fmt.Sprintf("%dG", sandboxDto.StorageQuota),
}
}
return hostConfig, nil
}
func (d *DockerClient) getContainerNetworkingConfig(_ context.Context) *network.NetworkingConfig {
containerNetwork := config.GetContainerNetwork()
if containerNetwork != "" {
return &network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{
containerNetwork: {},
},
}
}
return nil
}
func (d *DockerClient) getFilesystem(ctx context.Context) (string, error) {
info, err := d.apiClient.Info(ctx)
if err != nil {
return "", err
}
for _, driver := range info.DriverStatus {
if driver[0] == "Backing Filesystem" {
return driver[1], nil
}
}
return "", errors.New("filesystem not found")
}