Skip to content

Conversation

@crazy-max
Copy link
Member

@crazy-max crazy-max commented Nov 22, 2021

fixes #856

targets groups are not correctly rendered while being printed with bake --print so output cannot be reused.

// docker-bake.hcl
group "default" {
  targets = ["image"]
}

target "image" {
  inherits = ["image"]
  output = ["type=docker"]
}
// docker-bake.json
{
  "group": {
    "default": {
      "targets": [
        "image"
      ]
    }
  },
  "target": {
    "image": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "output": [
        "type=docker"
      ]
    }
  }
}
# docker-compose.yaml
services:
  addon:
    build:
      context: .
      dockerfile: ./Dockerfile
      args:
        CT_ECR: foo
        CT_TAG: bar
    image: ct-addon:bar
    environment:
      - NODE_ENV=test
      - AWS_ACCESS_KEY_ID=dummy
      - AWS_SECRET_ACCESS_KEY=dummy

  aws:
    build:
      dockerfile: ./aws.Dockerfile
      args:
        CT_ECR: foo
        CT_TAG: bar
    image: ct-fake-aws:bar

$ docker buildx bake -f docker-compose.yml -f docker-bake.hcl --print
{
  "group": {
    "default": {
      "targets": [
        "image"
      ]
    }
  },
  "target": {
    "image": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "output": [
        "type=docker"
      ]
    }
  }
}
$ docker buildx bake -f docker-bake.json --print
{
  "group": {
    "default": {
      "targets": [
        "image"
      ]
    }
  },
  "target": {
    "image": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "output": [
        "type=docker"
      ]
    }
  }
}
$ docker buildx bake -f docker-compose.yml -f docker-bake.hcl addon --print
{
  "group": {
    "default": {
      "targets": [
        "addon"
      ]
    }
  },
  "target": {
    "addon": {
      "context": ".",
      "dockerfile": "./Dockerfile",
      "args": {
        "CT_ECR": "foo",
        "CT_TAG": "bar"
      },
      "tags": [
        "ct-addon:bar"
      ]
    }
  }
}
$ docker buildx bake -f docker-compose.yml -f docker-bake.hcl addon aws --print
{
  "group": {
    "default": {
      "targets": [
        "addon",
        "aws"
      ]
    }
  },
  "target": {
    "addon": {
      "context": ".",
      "dockerfile": "./Dockerfile",
      "args": {
        "CT_ECR": "foo",
        "CT_TAG": "bar"
      },
      "tags": [
        "ct-addon:bar"
      ]
    },
    "aws": {
      "context": ".",
      "dockerfile": "./aws.Dockerfile",
      "args": {
        "CT_ECR": "foo",
        "CT_TAG": "bar"
      },
      "tags": [
        "ct-fake-aws:bar"
      ]
    }
  }
}
$ docker buildx bake -f docker-compose.yml -f docker-bake.hcl addon aws image --print
{
  "group": {
    "default": {
      "targets": [
        "addon",
        "aws",
        "image"
      ]
    }
  },
  "target": {
    "addon": {
      "context": ".",
      "dockerfile": "./Dockerfile",
      "args": {
        "CT_ECR": "foo",
        "CT_TAG": "bar"
      },
      "tags": [
        "ct-addon:bar"
      ]
    },
    "aws": {
      "context": ".",
      "dockerfile": "./aws.Dockerfile",
      "args": {
        "CT_ECR": "foo",
        "CT_TAG": "bar"
      },
      "tags": [
        "ct-fake-aws:bar"
      ]
    },
    "image": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "output": [
        "type=docker"
      ]
    }
  }
}

cc @eitsupi

Signed-off-by: CrazyMax [email protected]

@crazy-max crazy-max requested a review from tonistiigi November 22, 2021 18:19
Copy link
Member

@tonistiigi tonistiigi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@crazy-max crazy-max force-pushed the bake-json branch 2 times, most recently from 9ebe9fd to be0620f Compare November 23, 2021 07:43
@crazy-max crazy-max requested a review from tonistiigi November 23, 2021 07:48
@crazy-max crazy-max added this to the v0.7.1 milestone Nov 23, 2021
@eitsupi
Copy link
Contributor

eitsupi commented Nov 23, 2021

Don't you also need a test case for a non "default" group name?

@crazy-max crazy-max force-pushed the bake-json branch 2 times, most recently from 64fa5fc to 6ea4b44 Compare November 23, 2021 21:07
bake/bake.go Outdated
}

func ReadTargets(ctx context.Context, files []File, targets, overrides []string, defaults map[string]string) (map[string]*Target, []*Group, error) {
func ReadTargets(ctx context.Context, files []File, inp *Input, targets []string, overrides []string, defaults map[string]string) (map[string]*Target, map[string]*Group, map[string]build.Options, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for changing this signature? Can't you just add the group setting and leave the rest of the function as is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought TargetsToBuildOpt had some impact on groups context but it's actually only against targets.

This comment was marked as outdated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, I don't mind the map return but as this is going to be picked to release branch probably better to keep the change minimal. We can follow up with the type change in another PR that will not be picked later.

@crazy-max crazy-max force-pushed the bake-json branch 4 times, most recently from f670c3c to ed08b82 Compare November 24, 2021 07:05
@crazy-max crazy-max requested a review from tonistiigi November 24, 2021 07:07
Copy link
Member

@tonistiigi tonistiigi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to handle

target "default" {}

case

@crazy-max crazy-max force-pushed the bake-json branch 2 times, most recently from ec3f9da to fb9381c Compare November 24, 2021 20:26
@tonistiigi
Copy link
Member

#15 127.5 bake/bake_test.go:453:2: ineffectual assignment to `m` (ineffassign)
#15 127.5 	m, g, err = ReadTargets(ctx, []File{fTargetImage}, []string{"default"}, nil, nil)
#15 127.5 	^

Signed-off-by: CrazyMax <[email protected]>
@tonistiigi tonistiigi merged commit ab73275 into docker:master Nov 24, 2021
@crazy-max crazy-max deleted the bake-json branch November 25, 2021 07:57
@crazy-max crazy-max removed this from the v0.7.1 milestone Nov 25, 2021
@crazy-max crazy-max added this to the v0.8.0 milestone Jan 26, 2022
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 this pull request may close these issues.

bake --print show wrong docker-bake.json

3 participants