Transmode / gradle-docker

A Gradle plugin to build Docker images from the build script.
Apache License 2.0
647 stars 142 forks source link

Using addFile (Closure) #4

Closed sfitts closed 10 years ago

sfitts commented 10 years ago

This is totally user error, but I cannot figure out how to use the variant of addFile that takes a closure. My assumption is that it wants a copySpec closure, something like

{
    from 'myFile'
    into stageDir
}

But this fails on the line that uses the closure as a substitution parameter for the ADD line. There is something I'm missing here (I'm a groovy newbie).

dwellner commented 10 years ago

@mattgruter @sfitts Did you find a solution for your problem?

I also find the addFile API a bit confusing. There seems to be 2 addFile overloads:

addFile(File file), which puts the given file in the output folder and adds it to the dockerfile as ADD <file> /

This seems to be pretty useless, since usually files tend to go somewhere else than /.

Then there is addFile(Closure copySpec), which gives full control over copying the file, but that seems a bit unnecessary because typically the file just needs to go into the docker build folder. (stageDir)

Instead, if anyhow possible I would like to see something as simple as this

void addFile(File file, String destination) {
        stageDir.mkdir()
        project.copy {
            from file
            into stageDir
        }
        instructions.add("ADD ${file.name} " + destination)
    }

Or did I completely misunderstood something here?

mattgruter commented 10 years ago

@sfitts The addFile(Closure copySpec) API seems to be broken. Honestly I'm not sure how it ever worked. I'm working on a fix. So, no user error on your part.

@dwellner The recommended way to use the addFile(File file) API is with tar archive files:

addFile file('archive.tar')

Adding tar archives in a Dockerfile implictly unpacks the archive (see http://docs.docker.io/reference/builder/#add). The idea here is to add the docker payload by first creating a tar archive with the correct directory structure (e.g. an archive created by a distTar application task) and then passing the archive to addFile.

Until the addFile(Closure copySpec) API call is fixed, creating a tar archive is the most flexible way to add files. We'll probably also add a possibility to addFile(File file, String destination) in the next release (coming soon).

Cheers, Matthias

sfitts commented 10 years ago

Thanks for the update and for the explanation of how the other variant is designed to be used.

mattgruter commented 10 years ago

Fixed on branch 'dev'. Included in upcoming version 1.2