Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Custom dockerfiles and build contexts #352

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
33 changes: 26 additions & 7 deletions pkg/builder/docker/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/deis/duffle/pkg/builder"
"github.com/deis/duffle/pkg/duffle/manifest"

"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/image/build"
"github.com/docker/docker/api/types"
Expand All @@ -23,22 +22,23 @@ import (
"github.com/docker/docker/pkg/fileutils"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/term"

"github.com/sirupsen/logrus"

"golang.org/x/net/context"
)

const (
// DockerignoreFilename is the filename for Docker's ignore file.
DockerignoreFilename = ".dockerignore"
// DockerFilename is the default Dockerfile name
DockerFilename = "Dockerfile"
)

// Component contains all information to build a container image
type Component struct {
name string
Image string
Dockerfile string
contextDir string
Copy link
Member

Choose a reason for hiding this comment

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

Any particular reason why this is not exposed as the rest of the fields here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is just that I don't see why it should be exposed... Any particular reason any of these fields should be exposed ? :)

Copy link
Member

Choose a reason for hiding this comment

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

At some point in the future we want to vendor out the Duffle build packages for other tools - and those tools might want to to something different with the build context.

BuildContext io.ReadCloser

dockerBuilder dockerBuilder
Expand Down Expand Up @@ -68,10 +68,23 @@ func (dc Component) Digest() string {

// NewComponent returns a new Docker component based on the manifest
func NewComponent(c *manifest.Component, cli *command.DockerCli) *Component {
var (
contextDir string
dockerfile = DockerFilename
)

if v, ok := c.Configuration["context"]; ok {
contextDir = v
}
if v, ok := c.Configuration["dockerfile"]; ok {
dockerfile = v
}

return &Component{
name: c.Name,
// TODO - handle different Dockerfile names
Dockerfile: "Dockerfile",
Dockerfile: dockerfile,
contextDir: contextDir,
dockerBuilder: dockerBuilder{DockerClient: cli},
}
}
Expand All @@ -83,7 +96,14 @@ type dockerBuilder struct {

// PrepareBuild archives the component directory and loads it as Docker context
func (dc *Component) PrepareBuild(ctx *builder.Context) error {
if err := archiveSrc(filepath.Join(ctx.AppDir, dc.name), dc); err != nil {
ctxDir := dc.contextDir
switch {
case ctxDir == "":
ctxDir = filepath.Join(ctx.AppDir, dc.name)
case !filepath.IsAbs(ctxDir):
ctxDir = filepath.Join(ctx.AppDir, ctxDir)
}
if err := archiveSrc(ctxDir, dc); err != nil {
return err
}

Expand Down Expand Up @@ -139,7 +159,7 @@ func (dc Component) Build(ctx context.Context, app *builder.AppContext) error {
}

func archiveSrc(contextPath string, component *Component) error {
contextDir, relDockerfile, err := build.GetContextFromLocalDir(contextPath, "")
contextDir, relDockerfile, err := build.GetContextFromLocalDir(contextPath, filepath.Join(contextPath, component.Dockerfile))
if err != nil {
return fmt.Errorf("unable to prepare docker context: %s", err)
}
Expand Down Expand Up @@ -189,7 +209,6 @@ func archiveSrc(contextPath string, component *Component) error {
return err
}

component.name = filepath.Base(contextDir)
component.BuildContext = dockerArchive
component.Dockerfile = relDockerfile

Expand Down