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

support buildkit for dockcer-compose #1201

Merged
merged 1 commit into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions modules/docker/docker_compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
type Options struct {
WorkingDir string
EnvVars map[string]string

// Whether ot not to enable buildkit. You can find more information about buildkit here https://docs.docker.com/build/buildkit/#getting-started.
EnableBuildKit bool

// Set a logger that should be used. See the logger package for more info.
Logger *logger.Logger
}
Expand Down Expand Up @@ -46,6 +50,15 @@ func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args .

result := icmd.RunCmd(dockerComposeVersionCmd)

if options.EnableBuildKit {
if options.EnvVars == nil {
options.EnvVars = make(map[string]string)
}

options.EnvVars["DOCKER_BUILDKIT"] = "1"
options.EnvVars["COMPOSE_DOCKER_CLI_BUILD"] = "1"
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this COMPOSE_DOCKER_CLI_BUILD instead of DOCKER_COMPOSE_CLI_BUILD ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I understand:

https://www.docker.com/blog/faster-builds-in-compose-thanks-to-buildkit-support/

As docker-compose passes its environment variables to the Docker CLI, we can also tell the CLI to use BuildKit instead of the default builder. To accomplish that, we can execute this:

$ COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose build

}

if result.ExitCode == 0 {
cmd = shell.Command{
Command: "docker",
Expand Down
26 changes: 26 additions & 0 deletions modules/docker/docker_compose_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package docker

import (
"github.com/stretchr/testify/require"
"testing"
)

func TestDockerComposeWithBuildKit(t *testing.T) {
t.Parallel()

testToken := "testToken"
dockerOptions := &Options{
// Directory where docker-compose.yml lives
WorkingDir: "../../test/fixtures/docker-compose-with-buildkit",

// Configure the port the web app will listen on and the text it will return using environment variables
EnvVars: map[string]string{
"GITHUB_OAUTH_TOKEN": testToken,
},
EnableBuildKit: true,
}
out := RunDockerCompose(t, dockerOptions, "build", "--no-cache")
out = RunDockerCompose(t, dockerOptions, "up")

require.Contains(t, out, testToken)
}
5 changes: 5 additions & 0 deletions test/fixtures/docker-compose-with-buildkit/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# A "Hello, World" Docker image used in automated tests for the docker.Build command.
FROM ubuntu:20.04 as with-secrets

RUN --mount=type=secret,id=github-token echo "$(cat /run/secrets/github-token)" > text.txt
COPY ./bash_script.sh /usr/local/bin/bash_script.sh
4 changes: 4 additions & 0 deletions test/fixtures/docker-compose-with-buildkit/bash_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -e

cat text.txt
11 changes: 11 additions & 0 deletions test/fixtures/docker-compose-with-buildkit/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
services:
test-docker-image:
build:
context: .
secrets:
- github-token
entrypoint: bash_script.sh

secrets:
github-token:
environment: GITHUB_OAUTH_TOKEN