Skip to content

Europa: Set image metadata in a Docker build #1339

Closed

Description

@shykes

Problem

The new Docker Build API in Europa (universe.dagger.io/docker.#Build) lacks the ability to set Docker image metadata. As compared to Dockerfile features:

Feature Dockerfile operation CUE operation
Set default working directory WORKDIR <PATH> Not available
Set default user USER <ID> Not available
etc. etc. etc.

This feature is tricky to implement, because it is a form of override, and CUE is designed to make overrides difficult. For good reason - abuse of overrides is a huge cause of runaway configuration complexity. But in this case, well, that’s just how Docker images work: 99% of builds involve 1) pulling a base image with a config outside of our control, then 2) overriding some fields in that config. Supporting this pattern is a must-have for adoption.

Proof of concept

Thanks to @helderco, we have a working proof of concept. This can live in the universe.dagger.io/docker package.

Latest proof-of-concept:

#Set: {
    input: #Image
    config: #ImageConfig
    output: #Image & {
        rootfs: input.rootfs
        "config": {
            for field, value in input.config {
                // doesn't exist in config, copy away
                if config[field] == _|_ {
                    "\(field)": value
                }
            }
            for field, value in config {
                // replace any field that don't need merging
                if !list.Contains(["Env", "Labels"], field) {
                    "\(field)": value
                }
                // handle special Env case
                if field == "Env" {
                    Env: (_#MergeEnvs & {
                        a: input.config.Env
                        b: config.Env
                    }).output
                }
                // handle special Labels case
                if field == "Labels" {
                    Labels: (_#MergeMaps & {
                        a: input.config.Labels
                        b: config.Labels
                    }).output
                }
            }
        }
    }
}

_#MergeEnvs: {
    a: [...string]
    b: [...string]
    _a_map: (_#ListToMap & { input: a }).output
    _b_map: (_#ListToMap & { input: b }).output
    _reconcile: (_#MergeMaps & { "a": _a_map, "b": _b_map }).output
    "output": [
        for field, value in _reconcile {
            "\(field)=\(value)"
        }
    ]
}

_#ListToMap: {
    sep: string | *"="
    input: [...string]
    "output": [string]: string
    "output": {
        for value in input {
            let pair = strings.Split(value, sep)
            "\(pair[0])": pair[1]
        }
    }
}

_#MergeMaps: {
    a: [string]: string
    b: [string]: string
    "output": {
        for field, value in a {
            if b[field] == _|_ {
                "\(field)": value
            }
        }
        for field, value in b {
            "\(field)": value
        }
    }
}

Resources

Activity

  1. changed the title [-]Europa: override Docker image metadata in CUE[/-] [+]Europa: Set Docker image metadata[/+] on Jan 4, 2022
  2. changed the title [-]Europa: Set Docker image metadata[/-] [+]Europa: Set image metadata when building a Docker image[/+] on Jan 4, 2022
  3. changed the title [-]Europa: Set image metadata when building a Docker image[/-] [+]Europa: Set image metadata in a Docker build[/+] on Jan 4, 2022
  4. added this to the milestone on Jan 4, 2022
  5. helderco commented on Jan 5, 2022

    @helderco
    Contributor

    Thanks @shykes, but this POC is outdated. The most current comments come from #1227.

    The latest POC merges correctly Env and Labels.

    #Set: {
        input: #Image
        config: #ImageConfig
        output: #Image & {
            rootfs: input.rootfs
            "config": {
                for field, value in input.config {
                    // doesn't exist in config, copy away
                    if config[field] == _|_ {
                        "\(field)": value
                    }
                }
                for field, value in config {
                    // replace any field that don't need merging
                    if !list.Contains(["Env", "Labels"], field) {
                        "\(field)": value
                    }
                    // handle special Env case
                    if field == "Env" {
                        Env: (_#MergeEnvs & {
                            a: input.config.Env
                            b: config.Env
                        }).output
                    }
                    // handle special Labels case
                    if field == "Labels" {
                        Labels: (_#MergeMaps & {
                            a: input.config.Labels
                            b: config.Labels
                        }).output
                    }
                }
            }
        }
    }
    
    _#MergeEnvs: {
        a: [...string]
        b: [...string]
        _a_map: (_#ListToMap & { input: a }).output
        _b_map: (_#ListToMap & { input: b }).output
        _reconcile: (_#MergeMaps & { "a": _a_map, "b": _b_map }).output
        "output": [
            for field, value in _reconcile {
                "\(field)=\(value)"
            }
        ]
    }
    
    _#ListToMap: {
        sep: string | *"="
        input: [...string]
        "output": [string]: string
        "output": {
            for value in input {
                let pair = strings.Split(value, sep)
                "\(pair[0])": pair[1]
            }
        }
    }
    
    _#MergeMaps: {
        a: [string]: string
        b: [string]: string
        "output": {
            for field, value in a {
                if b[field] == _|_ {
                    "\(field)": value
                }
            }
            for field, value in b {
                "\(field)": value
            }
        }
    }

    https://cuelang.org/play/?id=T8K1SMLw1HC#cue@export@cue

  6. shykes commented on Jan 5, 2022

    @shykes
    ContributorAuthor

    Thanks @shykes, but this POC is outdated. The most current comments come from #1227.

    Thanks, updated.

  7. helderco commented on Jan 5, 2022

    @helderco
    Contributor

    As I said:

    [...] looking at the spec (are we supporting dockerfile as well?), I don't think you want to merge all maps/lists, just a few. You want to combine the Env lists and Labels maps, but not a Cmd list, for example. Anything other then those specific ones that need merging, would just be replaced.

    I think this is doable in pure cue. I've implemented the map merging, but not lists. For Env you can make a temporary struct out of it, merge it, and spit out a list in the end. As for OnBuild, you'd just append, even if it causes duplicates.

    Besides Env, Labels and OnBuild, seems to me everything else should be replaced (easy). Only Env requires a bit more cue code to do the merging.

    Originally posted by @helderco in #1227 (comment)

  8. modified the milestones: , next on Jan 7, 2022
  9. shykes commented on Jan 10, 2022

    @shykes
    ContributorAuthor

    @helderco just confirming if you’re interested in turning your POC into a pull request, or if you’d prefer someone else to do it? Since you started the implementation, you have priority if you want your “name in the credits”, but perfectly happy to assign someone else too.

  10. helderco commented on Jan 10, 2022

    @helderco
    Contributor

    I’m happy to do it 😄

  11. jlongtine commented on Jan 31, 2022

    @jlongtine
    Contributor

    @helderco With #1505, this gets easier. What else do we need to do to get this over the line? I'm happy to help out with this, if need be.

  12. helderco commented on Jan 31, 2022

    @helderco
    Contributor

    It’s actually ready to push, I’m just missing a good example for the PR description. Let me just take care of my kid and I’ll push it in a moment.

  13. jlongtine commented on Jan 31, 2022

    @jlongtine
    Contributor

    Sounds good, @helderco! Thanks for helping out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions