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

feature request: make it possible to set the WORKDIR #1260

Open
a-h opened this issue Mar 18, 2024 · 1 comment · May be fixed by #1261
Open

feature request: make it possible to set the WORKDIR #1260

a-h opened this issue Mar 18, 2024 · 1 comment · May be fixed by #1261

Comments

@a-h
Copy link

a-h commented Mar 18, 2024

In the "migrating from dockerfile" example, the base Dockerfile uses the WORKDIR statement https://docs.docker.com/reference/dockerfile/#workdir

However, I don't see a way to set the workdir in the configuration at

func (bo *BuildOptions) LoadConfig() error {
v := viper.New()
if bo.WorkingDirectory == "" {
bo.WorkingDirectory = "."
}
// If omitted, use this base image.
v.SetDefault("defaultBaseImage", configDefaultBaseImage)
const configName = ".ko"
v.SetConfigName(configName) // .yaml is implicit
v.SetEnvPrefix("KO")
v.AutomaticEnv()
if override := os.Getenv("KO_CONFIG_PATH"); override != "" {
file, err := os.Stat(override)
if err != nil {
return fmt.Errorf("error looking for config file: %w", err)
}
if file.Mode().IsRegular() {
v.SetConfigFile(override)
} else if file.IsDir() {
path := filepath.Join(override, ".ko.yaml")
file, err = os.Stat(path)
if err != nil {
return fmt.Errorf("error looking for config file: %w", err)
}
if file.Mode().IsRegular() {
v.SetConfigFile(path)
} else {
return fmt.Errorf("config file %s is not a regular file", path)
}
} else {
return fmt.Errorf("config file %s is not a regular file", override)
}
}
v.AddConfigPath(bo.WorkingDirectory)
if err := v.ReadInConfig(); err != nil {
if !errors.As(err, &viper.ConfigFileNotFoundError{}) {
return fmt.Errorf("error reading config file: %w", err)
}
}
dp := v.GetStringSlice("defaultPlatforms")
if len(dp) > 0 {
bo.DefaultPlatforms = dp
}
if bo.BaseImage == "" {
ref := v.GetString("defaultBaseImage")
if _, err := name.ParseReference(ref); err != nil {
return fmt.Errorf("'defaultBaseImage': error parsing %q as image reference: %w", ref, err)
}
bo.BaseImage = ref
}
if len(bo.BaseImageOverrides) == 0 {
baseImageOverrides := map[string]string{}
overrides := v.GetStringMapString("baseImageOverrides")
for key, value := range overrides {
if _, err := name.ParseReference(value); err != nil {
return fmt.Errorf("'baseImageOverrides': error parsing %q as image reference: %w", value, err)
}
baseImageOverrides[key] = value
}
bo.BaseImageOverrides = baseImageOverrides
}
if len(bo.BuildConfigs) == 0 {
var builds []build.Config
if err := v.UnmarshalKey("builds", &builds); err != nil {
return fmt.Errorf("configuration section 'builds' cannot be parsed")
}
buildConfigs, err := createBuildConfigMap(bo.WorkingDirectory, builds)
if err != nil {
return fmt.Errorf("could not create build config map: %w", err)
}
bo.BuildConfigs = buildConfigs
}
return nil
}

As such, the workdir isn't set, e.g. docker image inspect -f '{{.Config.WorkingDir}}' something_made_with_ko:latest will return nothing.

ko inherits Docker config from a base container, but the default working directory of a Docker container is /, and if it's not set in the base, it will still default to /.

Docker containers that are designed to process data have a location where you'd expect to mount in a volume, e.g. /data. Without being able to set the detfault workingdir, you have to add an extra -w parameter to set it on each use.

docker run -w=/data -v `pwd`:/data  container_name:latest

Whereas, with the workingdir set, you could do:

docker run -v `pwd`:/data  container_name:latest

So, adding the ability to set the workingdir would be useful.

@a-h a-h linked a pull request Mar 18, 2024 that will close this issue
@a-h
Copy link
Author

a-h commented Mar 18, 2024

I've put together a draft PR which I think might resolve this, but I'm not familiar with the codebase and how best to test it end-to-end, so I'm likely to have missed something.

Just to be clear, I would like a review on the PR, around what I'd need to do to get the feature included. But I'd also be happy if someone that knows the product better threw what I'd done away, and implemented it themselves. 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant