Class Dockerfile

java.lang.Object
org.gradle.api.internal.AbstractTask
org.gradle.api.DefaultTask
com.bmuschko.gradle.docker.tasks.image.Dockerfile
All Implemented Interfaces:
Comparable<Task>, org.gradle.api.internal.DynamicObjectAware, org.gradle.api.internal.TaskInternal, ExtensionAware, Task, Configurable<Task>

@CacheableTask public class Dockerfile extends DefaultTask
Creates a Dockerfile based on the provided instructions.
  • Constructor Details

    • Dockerfile

      public Dockerfile()
  • Method Details

    • getDestFile

      @OutputFile public final RegularFileProperty getDestFile()
      The destination file representing the Dockerfile. The destination file encourages the conventional file name Dockerfile but allows any arbitrary file name.

      Defaults to $buildDir/docker/Dockerfile.

      The method getDestDir() returns the parent directory of the Dockerfile.

    • getInstructions

      public ListProperty<Dockerfile.Instruction> getInstructions()
      Returns all instructions used to generate the Dockerfile.
      Returns:
      All instructions
    • getDestDir

      @Internal public Provider<Directory> getDestDir()
      Returns a provider representing the destination directory containing the Dockerfile.
      Returns:
      The destination directory containing the Dockerfile
      Since:
      4.4.0
    • create

      public void create() throws IOException
      Throws:
      IOException
    • instructionsFromTemplate

      public void instructionsFromTemplate(File template) throws IOException
      Adds instructions to the Dockerfile from a template file. The template file can have any name.
      Parameters:
      template - The template file
      Throws:
      IOException
      See Also:
    • instructionsFromTemplate

      public void instructionsFromTemplate(String templatePath) throws IOException
      Adds instructions to the Dockerfile from a template file. The path can be relative to the project root directory or absolute.
      Parameters:
      templatePath - The path to the template file
      Throws:
      IOException
      See Also:
    • instructionsFromTemplate

      public void instructionsFromTemplate(Provider<RegularFile> provider) throws IOException
      Adds instructions to the Dockerfile from a template file. Currently, the provider is evaluated as soon as the method is called which means that the provider is not evaluated lazily. This behavior might change in the future.
      Parameters:
      provider - The provider of the template file
      Throws:
      IOException
      Since:
      4.0.0
      See Also:
    • instruction

      public void instruction(String instruction)
      Adds a full instruction as String.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           instruction('FROM ubuntu:14.04')
           instruction('LABEL maintainer=benjamin.muschko@gmail.com')
       }
       
      The produced instructions look as follows:

       FROM ubuntu:14.04
       LABEL maintainer=benjamin.muschko@gmail.com
       
      Parameters:
      instruction - Instruction as String
      See Also:
    • instruction

      public void instruction(Provider<String> provider)
      Adds a full instruction as Provider.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           instruction(project.provider(new Callable<String>() {
               @Override
               String call() throws Exception {
                   'FROM ubuntu:14.04'
               }
           }))
       }
       
      The produced instruction looks as follows:

       FROM ubuntu:14.04
       
      Parameters:
      provider - Instruction as Provider
      Since:
      4.0.0
      See Also:
    • from

      public void from(String image)
      The FROM instruction sets the Base Image for subsequent instructions.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           from('ubuntu:14.04')
       }
       
      The produced instruction looks as follows:

       FROM ubuntu:14.04
       
      Parameters:
      image - From definition
      See Also:
    • from

      public void from(Dockerfile.From from)
      The FROM instruction sets the Base Image for subsequent instructions.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           from(new From('ubuntu:14.04'))
       }
       
      The produced instruction looks as follows:

       FROM ubuntu:14.04
       
      Parameters:
      from - From definition
      See Also:
    • from

      public void from(Provider<Dockerfile.From> provider)
      A FROM instruction as Provider.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           from(project.provider(new Callable<Dockerfile.From>() {
               @Override
               Dockerfile.From call() throws Exception {
                   new Dockerfile.From('ubuntu:14.04')
               }
           }))
       }
       
      The produced instruction looks as follows:

       FROM ubuntu:14.04
       
      Parameters:
      provider - From information as Provider
      Since:
      4.0.0
      See Also:
    • arg

      public void arg(String arg)
      The ARG instruction defines a variable that users can pass at build-time to the builder.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           arg('user1=someuser')
       }
       
      The produced instruction looks as follows:

       ARG user1=someuser
       
      Parameters:
      arg - Argument to pass, possibly with default value.
      See Also:
    • arg

      public void arg(Provider<String> provider)
      An ARG instruction as Provider.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           arg(project.provider(new Callable<String>() {
               @Override
               String call() throws Exception {
                   'user1=someuser'
               }
           }))
       }
       
      The produced instruction looks as follows:

       ARG user1=someuser
       
      Parameters:
      provider - Argument to pass as Provider
      Since:
      4.0.0
      See Also:
    • runCommand

      public void runCommand(String command)
      The RUN instruction will execute any commands in a new layer on top of the current image and commit the results.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           runCommand('/bin/bash -c echo hello')
       }
       
      The produced instruction looks as follows:

       RUN /bin/bash -c echo hello
       
      Parameters:
      command - Command
      See Also:
    • runCommand

      public void runCommand(Provider<String> provider)
      A RUN instruction as Provider.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           runCommand(project.provider(new Callable<String>() {
               @Override
               String call() throws Exception {
                   '/bin/bash -c echo hello'
               }
           }))
       }
       
      The produced instruction looks as follows:

       RUN /bin/bash -c echo hello
       
      Parameters:
      provider - Command as Provider
      Since:
      4.0.0
      See Also:
    • defaultCommand

      public void defaultCommand(String... command)
      The main purpose of a CMD instruction is to provide defaults for an executing container.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           defaultCommand('/usr/bin/wc', '--help')
       }
       
      The produced instruction looks as follows:

       CMD ["/usr/bin/wc", "--help"]
       
      Parameters:
      command - Command
      See Also:
    • defaultCommand

      public void defaultCommand(Provider<List<String>> provider)
      A CMD instruction as Provider.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           defaultCommand(project.provider(new Callable<List<String>>() {
               @Override
               List<String> call() throws Exception {
                   ['/usr/bin/wc', '--help']
               }
           }))
       }
       
      The produced instruction looks as follows:

       CMD ["/usr/bin/wc", "--help"]
       
      Parameters:
      provider - Command as Provider
      Since:
      4.0.0
      See Also:
    • exposePort

      public void exposePort(Integer... ports)
      The EXPOSE instruction informs Docker that the container will listen on the specified network ports at runtime.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           exposePort(8080, 9090)
       }
       
      The produced instruction looks as follows:

       EXPOSE 8080 9090
       
      Parameters:
      ports - Ports
      See Also:
    • exposePort

      public void exposePort(Provider<List<Integer>> provider)
      A EXPOSE instruction as Provider.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           exposePort(project.provider(new Callable<List<Integer>>() {
               @Override
               List<Integer> call() throws Exception {
                   [8080, 9090]
               }
           }))
       }
       
      The produced instruction looks as follows:

       EXPOSE 8080 9090
       
      Parameters:
      provider - Ports as Provider
      Since:
      4.0.0
      See Also:
    • environmentVariable

      public void environmentVariable(String key, String value)
      The ENV instruction sets the environment variable <key> to the value <value>. This value will be passed to all future RUN instructions.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           environmentVariable('MY_NAME', 'John Doe')
       }
       
      The produced instruction looks as follows:

       ENV MY_NAME=John Doe
       
      Parameters:
      key - Key
      value - Value
      See Also:
    • environmentVariable

      public void environmentVariable(Map<String,String> envVars)
      A ENV instruction as Map.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           environmentVariable(['MY_NAME': 'John Doe'])
       }
       
      The produced instruction looks as follows:

       ENV MY_NAME=John Doe
       
      Parameters:
      envVars - Environment variables
      See Also:
    • environmentVariable

      public void environmentVariable(Provider<Map<String,String>> provider)
      A ENV instruction as Provider.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           environmentVariable(project.provider(new Callable<Map<String, String>>() {
               @Override
               Map<String, String> call() throws Exception {
                   ['MY_NAME': 'John Doe']
               }
           }))
       }
       
      The produced instruction looks as follows:

       ENV MY_NAME=John Doe
       
      Parameters:
      provider - Environment variables as Provider
      Since:
      4.0.0
      See Also:
    • addFile

      public void addFile(String src, String dest)
      The ADD instruction copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the container at the path <dest>.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           addFile('test', '/absoluteDir/')
       }
       
      The produced instruction looks as follows:

       ADD test /absoluteDir/
       
      Parameters:
      src - The source path
      dest - The destination path
      See Also:
    • addFile

      public void addFile(Dockerfile.File file)
      The ADD instruction copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the container at the path <dest>.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           addFile(new Dockerfile.File('test', '/absoluteDir/'))
       }
       
      The produced instruction looks as follows:

       ADD test /absoluteDir/
       
      Parameters:
      file - Dockerfile.File definition
      See Also:
    • addFile

      public void addFile(Provider<Dockerfile.File> provider)
      An ADD instruction as Provider.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           addFile(project.provider(new Callable<Dockerfile.File>() {
               @Override
               Dockerfile.File call() throws Exception {
                   new Dockerfile.File('test', '/absoluteDir/')
               }
           }))
       }
       
      The produced instruction looks as follows:

       ADD test /absoluteDir/
       
      Parameters:
      provider - Add instruction as Provider
      Since:
      4.0.0
      See Also:
    • copyFile

      public void copyFile(String src, String dest)
      The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           copyFile('test', '/absoluteDir/')
       }
       
      The produced instruction looks as follows:

       COPY test /absoluteDir/
       
      Parameters:
      src - The source path
      dest - The destination path
      See Also:
    • copyFile

      public void copyFile(Dockerfile.CopyFile file)
      The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           copyFile(new Dockerfile.CopyFile('test', '/absoluteDir/'))
       }
       
      The produced instruction looks as follows:

       COPY test /absoluteDir/
       
      Parameters:
      file - CopyFile definition
      See Also:
    • copyFile

      public void copyFile(Provider<Dockerfile.CopyFile> provider)
      A COPY instruction as Provider.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           copyFile(project.provider(new Callable<Dockerfile.CopyFile>() {
               @Override
               Dockerfile.CopyFile call() throws Exception {
                   new Dockerfile.CopyFile('test', '/absoluteDir/')
               }
           }))
       }
       
      The produced instruction looks as follows:

       COPY test /absoluteDir/
       
      Parameters:
      provider - Copy instruction as Provider
      Since:
      4.0.0
      See Also:
    • entryPoint

      public void entryPoint(String... entryPoint)
      An ENTRYPOINT allows you to configure a container that will run as an executable.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           entryPoint('top', '-b')
       }
       
      The produced instruction looks as follows:

       ENTRYPOINT ["top", "-b"]
       
      Parameters:
      entryPoint - Entry point
      See Also:
    • entryPoint

      public void entryPoint(Provider<List<String>> provider)
      A ENTRYPOINT as Provider.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           entryPoint(project.provider(new Callable<List<String>>() {
               @Override
               List<String> call() throws Exception {
                   ['top', '-b']
               }
           }))
       }
       
      The produced instruction looks as follows:

       ENTRYPOINT ["top", "-b"]
       
      Parameters:
      provider - Entry point
      Since:
      4.0.0
      See Also:
    • volume

      public void volume(String... volume)
      The VOLUME instruction will create a mount point with the specified name and mark it as holding externally mounted volumes from native host or other containers.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           volume('/myvol')
       }
       
      The produced instruction looks as follows:

       VOLUME ["/myvol"]
       
      Parameters:
      volume - Volume
      See Also:
    • volume

      public void volume(Provider<List<String>> provider)
      A VOLUME instruction as Provider.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           volume(project.provider(new Callable<List<String>>() {
               @Override
               List<String> call() throws Exception {
                   ['/myvol']
               }
           }))
       }
       
      The produced instruction looks as follows:

       VOLUME ["/myvol"]
       
      Parameters:
      provider - Volume
      Since:
      4.0.0
      See Also:
    • user

      public void user(String user)
      The USER instruction sets the user name or UID to use when running the image and for any RUN, CMD and ENTRYPOINT instructions that follow it in the Dockerfile.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           user('patrick')
       }
       
      The produced instruction looks as follows:

       USER patrick
       
      Parameters:
      user - User
      See Also:
    • user

      public void user(Provider<String> provider)
      A USER instruction as Provider.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           user(project.provider(new Callable<String>() {
               @Override
               String call() throws Exception {
                   'patrick'
               }
           }))
       }
       
      The produced instruction looks as follows:

       USER patrick
       
      Parameters:
      provider - User as Provider
      Since:
      4.0.0
      See Also:
    • workingDir

      public void workingDir(String dir)
      The WORKDIR instruction sets the working directory for any RUN, CMD and ENTRYPOINT instructions that follow it in the Dockerfile.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           workingDir('/path/to/workdir')
       }
       
      The produced instruction looks as follows:

       WORKDIR /path/to/workdir
       
      Parameters:
      dir - Directory
      See Also:
    • workingDir

      public void workingDir(Provider<String> provider)
      A WORKDIR instruction as Provider.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           workingDir(project.provider(new Callable<String>() {
               @Override
               String call() throws Exception {
                   '/path/to/workdir'
               }
           }))
       }
       
      The produced instruction looks as follows:

       WORKDIR /path/to/workdir
       
      Parameters:
      provider - Directory
      Since:
      4.0.0
      See Also:
    • onBuild

      public void onBuild(String instruction)
      The ONBUILD instruction adds to the image a trigger instruction to be executed at a later time, when the image is used as the base for another build.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           onBuild('ADD . /app/src')
       }
       
      The produced instruction looks as follows:

       ONBUILD ADD . /app/src
       
      Parameters:
      instruction - Instruction
      See Also:
    • onBuild

      public void onBuild(Provider<String> provider)
      A ONBUILD instruction as Provider.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           onBuild(project.provider(new Callable<String>() {
               @Override
               String call() throws Exception {
                   'ADD . /app/src'
               }
           }))
       }
       
      The produced instruction looks as follows:

       ONBUILD ADD . /app/src
       
      Parameters:
      provider - Instruction
      Since:
      4.0.0
      See Also:
    • label

      public void label(Map<String,String> labels)
      The LABEL instruction adds metadata to an image.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           label(['version': '1.0'])
       }
       
      The produced instruction looks as follows:

       LABEL version=1.0
       
      Parameters:
      labels - Labels
      See Also:
    • label

      public void label(Provider<Map<String,String>> provider)
      A LABEL instruction as Provider.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           label(project.provider(new Callable<Map<String, String>>() {
               @Override
               Map<String, String> call() throws Exception {
                   ['version': '1.0']
               }
           }))
       }
       
      The produced instruction looks as follows:

       LABEL version=1.0
       
      Parameters:
      provider - Labels as Provider
      Since:
      4.0.0
      See Also:
    • healthcheck

      public void healthcheck(Dockerfile.Healthcheck healthcheck)
      The HEALTHCHECK instruction tells Docker how to test a container to check that it is still working.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           healthcheck(new Healthcheck("curl -f http://localhost/ || exit 1").withRetries(5))
       }
       
      The produced instruction looks as follows:

       HEALTHCHECK --retries=5 CMD curl -f http://localhost/ || exit 1
       
      Parameters:
      healthcheck - the healthcheck configuration
      See Also:
    • healthcheck

      public void healthcheck(Provider<Dockerfile.Healthcheck> provider)
      The HEALTHCHECK instruction tells Docker how to test a container to check that it is still working.

      Example in Groovy DSL:

       task createDockerfile(type: Dockerfile) {
           from(project.provider(new Callable<Dockerfile.Healthcheck>() {
               @Override
               Dockerfile.Healthcheck call() throws Exception {
                   new Dockerfile.Healthcheck("curl -f http://localhost/ || exit 1")
               }
           }))
       }
       
      The produced instruction looks as follows:

       HEALTHCHECK CMD curl -f http://localhost/ || exit 1
       
      Parameters:
      provider - Healthcheck information as Provider
      See Also: