Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the container ulimits for reatime #2694

Merged
merged 7 commits into from
Sep 20, 2024
27 changes: 27 additions & 0 deletions internal/utils/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
Expand All @@ -30,6 +31,7 @@ import (
"github.com/docker/docker/errdefs"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/go-units"
"github.com/go-errors/errors"
"github.com/spf13/viper"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -246,6 +248,22 @@ func DockerPullImageIfNotCached(ctx context.Context, imageName string) error {

var suggestDockerInstall = "Docker Desktop is a prerequisite for local development. Follow the official docs to install: https://docs.docker.com/desktop"

func getUlimitFromEnvRlimit(envVariable string) *units.Ulimit {
if strings.HasPrefix(envVariable, "RLIMIT_NOFILE=") {
parts := strings.SplitN(envVariable, "=", 2)
if len(parts) == 2 {
if rlimitNofile, err := strconv.Atoi(parts[1]); err == nil {
return &units.Ulimit{
Name: "nofile",
Soft: int64(rlimitNofile),
Hard: int64(rlimitNofile),
}
}
}
}
return nil
}

func DockerStart(ctx context.Context, config container.Config, hostConfig container.HostConfig, networkingConfig network.NetworkingConfig, containerName string) (string, error) {
// Pull container image
if err := DockerPullImageIfNotCached(ctx, config.Image); err != nil {
Expand All @@ -263,6 +281,15 @@ func DockerStart(ctx context.Context, config container.Config, hostConfig contai
config.Labels[composeProjectLabel] = Config.ProjectId
// Configure container network
hostConfig.ExtraHosts = append(hostConfig.ExtraHosts, extraHosts...)
// For containers with required NOFILE limit environment ensure
// the container ulimit is a match
for _, env := range config.Env {
if ulimit := getUlimitFromEnvRlimit(env); ulimit != nil {
hostConfig.Resources.Ulimits = append(hostConfig.Resources.Ulimits, ulimit)
break
}
}

avallete marked this conversation as resolved.
Show resolved Hide resolved
if networkId := viper.GetString("network-id"); len(networkId) > 0 {
hostConfig.NetworkMode = container.NetworkMode(networkId)
} else if len(hostConfig.NetworkMode) == 0 {
Expand Down