2016-06-07 12:15:44 -04:00
package image
2015-03-24 23:57:23 -04:00
import (
2015-07-21 17:47:44 -04:00
"archive/tar"
2015-03-24 23:57:23 -04:00
"bufio"
2015-10-27 20:29:21 -04:00
"bytes"
2015-03-24 23:57:23 -04:00
"fmt"
"io"
"os"
"path/filepath"
2015-07-21 17:47:44 -04:00
"regexp"
2015-03-24 23:57:23 -04:00
"runtime"
2016-02-03 18:41:26 -05:00
"golang.org/x/net/context"
2015-03-24 23:57:23 -04:00
"github.com/docker/docker/api"
2016-09-06 14:18:12 -04:00
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
2016-02-09 16:19:09 -05:00
"github.com/docker/docker/builder"
2015-12-14 05:15:00 -05:00
"github.com/docker/docker/builder/dockerignore"
2016-06-07 12:15:44 -04:00
"github.com/docker/docker/cli"
2016-09-08 13:11:39 -04:00
"github.com/docker/docker/cli/command"
2015-07-22 22:26:06 -04:00
"github.com/docker/docker/opts"
2015-03-24 23:57:23 -04:00
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/fileutils"
2015-03-17 22:18:41 -04:00
"github.com/docker/docker/pkg/jsonmessage"
2015-11-13 19:59:01 -05:00
"github.com/docker/docker/pkg/progress"
2015-03-17 22:18:41 -04:00
"github.com/docker/docker/pkg/streamformatter"
2015-03-24 23:57:23 -04:00
"github.com/docker/docker/pkg/urlutil"
2015-12-04 16:55:15 -05:00
"github.com/docker/docker/reference"
2015-12-21 15:06:46 -05:00
runconfigopts "github.com/docker/docker/runconfig/opts"
2015-12-16 12:26:49 -05:00
"github.com/docker/go-units"
2016-06-07 12:15:44 -04:00
"github.com/spf13/cobra"
2015-03-24 23:57:23 -04:00
)
2016-06-07 12:15:44 -04:00
type buildOptions struct {
context string
dockerfileName string
tags opts . ListOpts
2016-09-29 17:59:52 -04:00
labels opts . ListOpts
2016-06-07 12:15:44 -04:00
buildArgs opts . ListOpts
ulimits * runconfigopts . UlimitOpt
memory string
memorySwap string
shmSize string
cpuShares int64
cpuPeriod int64
cpuQuota int64
cpuSetCpus string
cpuSetMems string
cgroupParent string
isolation string
quiet bool
noCache bool
rm bool
forceRm bool
pull bool
2016-09-22 17:38:00 -04:00
cacheFrom [ ] string
2016-08-18 04:35:23 -04:00
compress bool
2016-06-07 15:15:50 -04:00
securityOpt [ ] string
2016-03-06 07:29:23 -05:00
networkMode string
2016-04-21 12:08:37 -04:00
squash bool
2016-06-07 12:15:44 -04:00
}
2016-03-16 17:52:34 -04:00
2016-06-07 12:15:44 -04:00
// NewBuildCommand creates a new `docker build` command
2016-09-08 13:11:39 -04:00
func NewBuildCommand ( dockerCli * command . DockerCli ) * cobra . Command {
2015-12-21 15:06:46 -05:00
ulimits := make ( map [ string ] * units . Ulimit )
2016-06-07 12:15:44 -04:00
options := buildOptions {
tags : opts . NewListOpts ( validateTag ) ,
2016-12-03 08:46:04 -05:00
buildArgs : opts . NewListOpts ( runconfigopts . ValidateEnv ) ,
2016-06-07 12:15:44 -04:00
ulimits : runconfigopts . NewUlimitOpt ( & ulimits ) ,
2016-09-29 17:59:52 -04:00
labels : opts . NewListOpts ( runconfigopts . ValidateEnv ) ,
2016-06-07 12:15:44 -04:00
}
2015-07-22 22:26:06 -04:00
2016-06-07 12:15:44 -04:00
cmd := & cobra . Command {
2016-06-19 04:26:23 -04:00
Use : "build [OPTIONS] PATH | URL | -" ,
2016-06-07 12:15:44 -04:00
Short : "Build an image from a Dockerfile" ,
Args : cli . ExactArgs ( 1 ) ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
options . context = args [ 0 ]
return runBuild ( dockerCli , options )
} ,
}
2015-07-03 05:26:09 -04:00
2016-06-07 12:15:44 -04:00
flags := cmd . Flags ( )
flags . VarP ( & options . tags , "tag" , "t" , "Name and optionally a tag in the 'name:tag' format" )
flags . Var ( & options . buildArgs , "build-arg" , "Set build-time variables" )
flags . Var ( options . ulimits , "ulimit" , "Ulimit options" )
flags . StringVarP ( & options . dockerfileName , "file" , "f" , "" , "Name of the Dockerfile (Default is 'PATH/Dockerfile')" )
flags . StringVarP ( & options . memory , "memory" , "m" , "" , "Memory limit" )
flags . StringVar ( & options . memorySwap , "memory-swap" , "" , "Swap limit equal to memory plus swap: '-1' to enable unlimited swap" )
flags . StringVar ( & options . shmSize , "shm-size" , "" , "Size of /dev/shm, default value is 64MB" )
flags . Int64VarP ( & options . cpuShares , "cpu-shares" , "c" , 0 , "CPU shares (relative weight)" )
flags . Int64Var ( & options . cpuPeriod , "cpu-period" , 0 , "Limit the CPU CFS (Completely Fair Scheduler) period" )
flags . Int64Var ( & options . cpuQuota , "cpu-quota" , 0 , "Limit the CPU CFS (Completely Fair Scheduler) quota" )
flags . StringVar ( & options . cpuSetCpus , "cpuset-cpus" , "" , "CPUs in which to allow execution (0-3, 0,1)" )
flags . StringVar ( & options . cpuSetMems , "cpuset-mems" , "" , "MEMs in which to allow execution (0-3, 0,1)" )
flags . StringVar ( & options . cgroupParent , "cgroup-parent" , "" , "Optional parent cgroup for the container" )
flags . StringVar ( & options . isolation , "isolation" , "" , "Container isolation technology" )
2016-09-29 17:59:52 -04:00
flags . Var ( & options . labels , "label" , "Set metadata for an image" )
2016-06-07 12:15:44 -04:00
flags . BoolVar ( & options . noCache , "no-cache" , false , "Do not use cache when building the image" )
flags . BoolVar ( & options . rm , "rm" , true , "Remove intermediate containers after a successful build" )
flags . BoolVar ( & options . forceRm , "force-rm" , false , "Always remove intermediate containers" )
flags . BoolVarP ( & options . quiet , "quiet" , "q" , false , "Suppress the build output and print image ID on success" )
flags . BoolVar ( & options . pull , "pull" , false , "Always attempt to pull a newer version of the image" )
2016-09-22 17:38:00 -04:00
flags . StringSliceVar ( & options . cacheFrom , "cache-from" , [ ] string { } , "Images to consider as cache sources" )
2016-08-18 04:35:23 -04:00
flags . BoolVar ( & options . compress , "compress" , false , "Compress the build context using gzip" )
2016-06-07 15:15:50 -04:00
flags . StringSliceVar ( & options . securityOpt , "security-opt" , [ ] string { } , "Security options" )
2016-12-09 10:15:26 -05:00
flags . StringVar ( & options . networkMode , "network" , "default" , "Set the networking mode for the RUN instructions during build" )
2016-06-07 12:15:44 -04:00
2016-09-08 13:11:39 -04:00
command . AddTrustedFlags ( flags , true )
2016-06-07 12:15:44 -04:00
2016-11-02 20:43:32 -04:00
flags . BoolVar ( & options . squash , "squash" , false , "Squash newly built layers into a single new layer" )
flags . SetAnnotation ( "squash" , "experimental" , nil )
2016-11-02 20:43:32 -04:00
flags . SetAnnotation ( "squash" , "version" , [ ] string { "1.25" } )
2016-04-21 12:08:37 -04:00
2016-06-07 12:15:44 -04:00
return cmd
}
2015-07-20 17:53:52 -04:00
2016-08-13 17:07:53 -04:00
// lastProgressOutput is the same as progress.Output except
// that it only output with the last update. It is used in
// non terminal scenarios to depresss verbose messages
type lastProgressOutput struct {
output progress . Output
}
// WriteProgress formats progress information from a ProgressReader.
func ( out * lastProgressOutput ) WriteProgress ( prog progress . Progress ) error {
if ! prog . LastUpdate {
return nil
}
return out . output . WriteProgress ( prog )
}
2016-09-08 13:11:39 -04:00
func runBuild ( dockerCli * command . DockerCli , options buildOptions ) error {
2015-03-24 23:57:23 -04:00
var (
2016-11-07 03:17:04 -05:00
buildCtx io . ReadCloser
err error
2015-07-20 17:53:52 -04:00
contextDir string
tempDir string
relDockerfile string
2015-10-27 20:29:21 -04:00
progBuff io . Writer
buildBuff io . Writer
2015-07-20 17:53:52 -04:00
)
2016-11-02 20:43:32 -04:00
2016-11-07 03:17:04 -05:00
specifiedContext := options . context
2016-06-07 12:15:44 -04:00
progBuff = dockerCli . Out ( )
buildBuff = dockerCli . Out ( )
if options . quiet {
2015-10-27 20:29:21 -04:00
progBuff = bytes . NewBuffer ( nil )
buildBuff = bytes . NewBuffer ( nil )
}
2015-07-20 17:53:52 -04:00
switch {
case specifiedContext == "-" :
2016-06-07 12:15:44 -04:00
buildCtx , relDockerfile , err = builder . GetContextFromReader ( dockerCli . In ( ) , options . dockerfileName )
2016-01-16 05:59:13 -05:00
case urlutil . IsGitURL ( specifiedContext ) :
2016-06-07 12:15:44 -04:00
tempDir , relDockerfile , err = builder . GetContextFromGitURL ( specifiedContext , options . dockerfileName )
2015-07-20 17:53:52 -04:00
case urlutil . IsURL ( specifiedContext ) :
2016-06-07 12:15:44 -04:00
buildCtx , relDockerfile , err = builder . GetContextFromURL ( progBuff , specifiedContext , options . dockerfileName )
2015-07-20 17:53:52 -04:00
default :
2016-06-07 12:15:44 -04:00
contextDir , relDockerfile , err = builder . GetContextFromLocalDir ( specifiedContext , options . dockerfileName )
2015-07-20 17:53:52 -04:00
}
2015-03-24 23:57:23 -04:00
2015-07-20 17:53:52 -04:00
if err != nil {
2016-06-07 12:15:44 -04:00
if options . quiet && urlutil . IsURL ( specifiedContext ) {
fmt . Fprintln ( dockerCli . Err ( ) , progBuff )
2015-10-27 20:29:21 -04:00
}
2015-07-20 17:53:52 -04:00
return fmt . Errorf ( "unable to prepare context: %s" , err )
}
2015-03-24 23:57:23 -04:00
2015-07-20 17:53:52 -04:00
if tempDir != "" {
defer os . RemoveAll ( tempDir )
contextDir = tempDir
}
2015-03-24 23:57:23 -04:00
2016-05-21 09:57:57 -04:00
if buildCtx == nil {
2016-01-12 16:18:24 -05:00
// And canonicalize dockerfile name to a platform-independent one
relDockerfile , err = archive . CanonicalTarNameForPath ( relDockerfile )
2015-09-06 13:26:40 -04:00
if err != nil {
2016-01-12 16:18:24 -05:00
return fmt . Errorf ( "cannot canonicalize dockerfile path %s: %v" , relDockerfile , err )
}
f , err := os . Open ( filepath . Join ( contextDir , ".dockerignore" ) )
if err != nil && ! os . IsNotExist ( err ) {
2015-09-06 13:26:40 -04:00
return err
}
2016-06-24 23:57:21 -04:00
defer f . Close ( )
2015-09-06 13:26:40 -04:00
2016-01-12 16:18:24 -05:00
var excludes [ ] string
if err == nil {
excludes , err = dockerignore . ReadAll ( f )
if err != nil {
return err
}
}
2015-03-24 23:57:23 -04:00
2016-02-09 16:19:09 -05:00
if err := builder . ValidateContextDirectory ( contextDir , excludes ) ; err != nil {
2016-01-12 16:18:24 -05:00
return fmt . Errorf ( "Error checking context: '%s'." , err )
}
2015-07-20 17:53:52 -04:00
2016-01-12 16:18:24 -05:00
// If .dockerignore mentions .dockerignore or the Dockerfile
// then make sure we send both files over to the daemon
// because Dockerfile is, obviously, needed no matter what, and
// .dockerignore is needed to know if either one needs to be
// removed. The daemon will remove them for us, if needed, after it
// parses the Dockerfile. Ignore errors here, as they will have been
// caught by validateContextDirectory above.
var includes = [ ] string { "." }
keepThem1 , _ := fileutils . Matches ( ".dockerignore" , excludes )
keepThem2 , _ := fileutils . Matches ( relDockerfile , excludes )
if keepThem1 || keepThem2 {
includes = append ( includes , ".dockerignore" , relDockerfile )
}
2015-03-24 23:57:23 -04:00
2016-08-18 04:35:23 -04:00
compression := archive . Uncompressed
if options . compress {
compression = archive . Gzip
}
2016-05-21 09:57:57 -04:00
buildCtx , err = archive . TarWithOptions ( contextDir , & archive . TarOptions {
2016-08-18 04:35:23 -04:00
Compression : compression ,
2016-01-12 16:18:24 -05:00
ExcludePatterns : excludes ,
IncludeFiles : includes ,
} )
2016-01-05 13:32:43 -05:00
if err != nil {
2016-01-12 16:18:24 -05:00
return err
2016-01-05 13:32:43 -05:00
}
2016-01-12 16:18:24 -05:00
}
2016-01-05 13:32:43 -05:00
2016-05-21 09:57:57 -04:00
ctx := context . Background ( )
2016-01-12 16:18:24 -05:00
var resolvedTags [ ] * resolvedTag
2016-09-08 13:11:39 -04:00
if command . IsTrusted ( ) {
2016-08-29 14:45:29 -04:00
translator := func ( ctx context . Context , ref reference . NamedTagged ) ( reference . Canonical , error ) {
return TrustedReference ( ctx , dockerCli , ref )
}
2016-01-05 13:32:43 -05:00
// Wrap the tar archive to replace the Dockerfile entry with the rewritten
// Dockerfile which uses trusted pulls.
2016-08-29 14:45:29 -04:00
buildCtx = replaceDockerfileTarWrapper ( ctx , buildCtx , relDockerfile , translator , & resolvedTags )
2016-01-05 13:32:43 -05:00
}
2015-07-21 17:47:44 -04:00
2015-03-24 23:57:23 -04:00
// Setup an upload progress bar
2015-10-27 20:29:21 -04:00
progressOutput := streamformatter . NewStreamFormatter ( ) . NewProgressOutput ( progBuff , true )
2016-08-29 11:11:29 -04:00
if ! dockerCli . Out ( ) . IsTerminal ( ) {
2016-08-13 17:07:53 -04:00
progressOutput = & lastProgressOutput { output : progressOutput }
}
2015-11-13 19:59:01 -05:00
2016-05-21 09:57:57 -04:00
var body io . Reader = progress . NewProgressReader ( buildCtx , progressOutput , 0 , "" , "Sending build context to Docker daemon" )
2015-03-24 23:57:23 -04:00
var memory int64
2016-06-07 12:15:44 -04:00
if options . memory != "" {
parsedMemory , err := units . RAMInBytes ( options . memory )
2015-03-24 23:57:23 -04:00
if err != nil {
return err
}
memory = parsedMemory
}
var memorySwap int64
2016-06-07 12:15:44 -04:00
if options . memorySwap != "" {
if options . memorySwap == "-1" {
2015-03-24 23:57:23 -04:00
memorySwap = - 1
} else {
2016-06-07 12:15:44 -04:00
parsedMemorySwap , err := units . RAMInBytes ( options . memorySwap )
2015-03-24 23:57:23 -04:00
if err != nil {
return err
}
memorySwap = parsedMemorySwap
}
}
2015-12-29 15:49:17 -05:00
var shmSize int64
2016-06-07 12:15:44 -04:00
if options . shmSize != "" {
shmSize , err = units . RAMInBytes ( options . shmSize )
2015-12-29 15:49:17 -05:00
if err != nil {
return err
}
}
2016-08-18 17:23:10 -04:00
authConfigs , _ := dockerCli . GetAllCredentials ( )
2016-06-07 12:15:44 -04:00
buildOptions := types . ImageBuildOptions {
2015-12-03 18:27:01 -05:00
Memory : memory ,
MemorySwap : memorySwap ,
2016-06-07 12:15:44 -04:00
Tags : options . tags . GetAll ( ) ,
SuppressOutput : options . quiet ,
NoCache : options . noCache ,
Remove : options . rm ,
ForceRemove : options . forceRm ,
PullParent : options . pull ,
Isolation : container . Isolation ( options . isolation ) ,
CPUSetCPUs : options . cpuSetCpus ,
CPUSetMems : options . cpuSetMems ,
CPUShares : options . cpuShares ,
CPUQuota : options . cpuQuota ,
CPUPeriod : options . cpuPeriod ,
CgroupParent : options . cgroupParent ,
2015-12-03 18:27:01 -05:00
Dockerfile : relDockerfile ,
2015-12-29 15:49:17 -05:00
ShmSize : shmSize ,
2016-06-07 12:15:44 -04:00
Ulimits : options . ulimits . GetList ( ) ,
2016-12-03 08:46:04 -05:00
BuildArgs : runconfigopts . ConvertKVStringsToMapWithNil ( options . buildArgs . GetAll ( ) ) ,
2016-08-18 17:23:10 -04:00
AuthConfigs : authConfigs ,
2016-09-29 17:59:52 -04:00
Labels : runconfigopts . ConvertKVStringsToMap ( options . labels . GetAll ( ) ) ,
2016-09-22 17:38:00 -04:00
CacheFrom : options . cacheFrom ,
2016-06-07 15:15:50 -04:00
SecurityOpt : options . securityOpt ,
2016-03-06 07:29:23 -05:00
NetworkMode : options . networkMode ,
2016-04-21 12:08:37 -04:00
Squash : options . squash ,
2015-07-22 22:26:06 -04:00
}
2016-06-07 12:15:44 -04:00
response , err := dockerCli . Client ( ) . ImageBuild ( ctx , body , buildOptions )
2014-11-14 13:59:14 -05:00
if err != nil {
2016-09-13 00:06:04 -04:00
if options . quiet {
fmt . Fprintf ( dockerCli . Err ( ) , "%s" , progBuff )
}
2014-11-14 13:59:14 -05:00
return err
}
2016-03-16 11:38:13 -04:00
defer response . Body . Close ( )
2014-11-14 13:59:14 -05:00
2016-08-29 11:11:29 -04:00
err = jsonmessage . DisplayJSONMessagesStream ( response . Body , buildBuff , dockerCli . Out ( ) . FD ( ) , dockerCli . Out ( ) . IsTerminal ( ) , nil )
2015-03-24 23:57:23 -04:00
if err != nil {
2015-12-03 18:27:01 -05:00
if jerr , ok := err . ( * jsonmessage . JSONError ) ; ok {
// If no error code is set, default to 1
if jerr . Code == 0 {
jerr . Code = 1
2015-06-04 09:30:14 -04:00
}
2016-06-07 12:15:44 -04:00
if options . quiet {
fmt . Fprintf ( dockerCli . Err ( ) , "%s%s" , progBuff , buildBuff )
2015-10-27 20:29:21 -04:00
}
2016-06-07 12:15:44 -04:00
return cli . StatusError { Status : jerr . Message , StatusCode : jerr . Code }
2015-06-04 09:30:14 -04:00
}
}
2016-01-06 13:26:37 -05:00
// Windows: show error message about modified file permissions if the
// daemon isn't running Windows.
2016-07-06 17:34:14 -04:00
if response . OSType != "windows" && runtime . GOOS == "windows" && ! options . quiet {
2016-06-07 12:15:44 -04:00
fmt . Fprintln ( dockerCli . Err ( ) , ` SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories. ` )
2015-07-24 19:35:11 -04:00
}
2015-10-27 20:29:21 -04:00
// Everything worked so if -q was provided the output from the daemon
// should be just the image ID and we'll print that to stdout.
2016-06-07 12:15:44 -04:00
if options . quiet {
fmt . Fprintf ( dockerCli . Out ( ) , "%s" , buildBuff )
2015-10-27 20:29:21 -04:00
}
2016-01-05 13:32:43 -05:00
2016-09-08 13:11:39 -04:00
if command . IsTrusted ( ) {
2016-01-05 13:32:43 -05:00
// Since the build was successful, now we must tag any of the resolved
// images from the above Dockerfile rewrite.
for _ , resolved := range resolvedTags {
2016-08-29 14:45:29 -04:00
if err := TagTrusted ( ctx , dockerCli , resolved . digestRef , resolved . tagRef ) ; err != nil {
2016-01-05 13:32:43 -05:00
return err
}
2015-07-24 19:35:11 -04:00
}
}
return nil
2015-03-24 23:57:23 -04:00
}
2015-07-20 17:53:52 -04:00
2016-06-07 12:15:44 -04:00
type translatorFunc func ( context . Context , reference . NamedTagged ) ( reference . Canonical , error )
2015-08-30 09:48:03 -04:00
// validateTag checks if the given image name can be resolved.
func validateTag ( rawRepo string ) ( string , error ) {
2015-12-11 14:00:13 -05:00
_ , err := reference . ParseNamed ( rawRepo )
2015-11-18 17:20:54 -05:00
if err != nil {
2015-08-30 09:48:03 -04:00
return "" , err
}
return rawRepo , nil
}
2015-07-21 17:47:44 -04:00
var dockerfileFromLinePattern = regexp . MustCompile ( ` (?i)^[\s]*FROM[ \f\r\t\v]+(?P<image>[^ \f\r\t\v\n#]+) ` )
2015-07-24 19:35:11 -04:00
// resolvedTag records the repository, tag, and resolved digest reference
// from a Dockerfile rewrite.
type resolvedTag struct {
2015-11-18 17:20:54 -05:00
digestRef reference . Canonical
tagRef reference . NamedTagged
2015-07-24 19:35:11 -04:00
}
2015-07-21 17:47:44 -04:00
// rewriteDockerfileFrom rewrites the given Dockerfile by resolving images in
// "FROM <image>" instructions to a digest reference. `translator` is a
// function that takes a repository name and tag reference and returns a
// trusted digest reference.
2016-05-21 09:57:57 -04:00
func rewriteDockerfileFrom ( ctx context . Context , dockerfile io . Reader , translator translatorFunc ) ( newDockerfile [ ] byte , resolvedTags [ ] * resolvedTag , err error ) {
2015-07-21 17:47:44 -04:00
scanner := bufio . NewScanner ( dockerfile )
2016-01-12 16:18:24 -05:00
buf := bytes . NewBuffer ( nil )
2015-07-21 17:47:44 -04:00
// Scan the lines of the Dockerfile, looking for a "FROM" line.
for scanner . Scan ( ) {
line := scanner . Text ( )
matches := dockerfileFromLinePattern . FindStringSubmatch ( line )
2015-12-31 08:57:58 -05:00
if matches != nil && matches [ 1 ] != api . NoBaseImageSpecifier {
2015-07-21 17:47:44 -04:00
// Replace the line with a resolved "FROM repo@digest"
2015-11-18 17:20:54 -05:00
ref , err := reference . ParseNamed ( matches [ 1 ] )
if err != nil {
return nil , nil , err
2015-07-21 17:47:44 -04:00
}
2015-12-10 14:01:34 -05:00
ref = reference . WithDefaultTag ( ref )
2016-09-08 13:11:39 -04:00
if ref , ok := ref . ( reference . NamedTagged ) ; ok && command . IsTrusted ( ) {
2016-05-21 09:57:57 -04:00
trustedRef , err := translator ( ctx , ref )
2015-07-21 17:47:44 -04:00
if err != nil {
2015-07-24 19:35:11 -04:00
return nil , nil , err
2015-07-21 17:47:44 -04:00
}
2015-11-18 17:20:54 -05:00
line = dockerfileFromLinePattern . ReplaceAllLiteralString ( line , fmt . Sprintf ( "FROM %s" , trustedRef . String ( ) ) )
2015-07-24 19:35:11 -04:00
resolvedTags = append ( resolvedTags , & resolvedTag {
digestRef : trustedRef ,
2015-12-10 14:01:34 -05:00
tagRef : ref ,
2015-07-24 19:35:11 -04:00
} )
2015-07-21 17:47:44 -04:00
}
}
2016-01-12 16:18:24 -05:00
_ , err := fmt . Fprintln ( buf , line )
2015-07-21 17:47:44 -04:00
if err != nil {
2015-07-24 19:35:11 -04:00
return nil , nil , err
2015-07-21 17:47:44 -04:00
}
}
2016-01-12 16:18:24 -05:00
return buf . Bytes ( ) , resolvedTags , scanner . Err ( )
2015-07-21 17:47:44 -04:00
}
// replaceDockerfileTarWrapper wraps the given input tar archive stream and
// replaces the entry with the given Dockerfile name with the contents of the
// new Dockerfile. Returns a new tar archive stream with the replaced
// Dockerfile.
2016-05-21 09:57:57 -04:00
func replaceDockerfileTarWrapper ( ctx context . Context , inputTarStream io . ReadCloser , dockerfileName string , translator translatorFunc , resolvedTags * [ ] * resolvedTag ) io . ReadCloser {
2015-07-21 17:47:44 -04:00
pipeReader , pipeWriter := io . Pipe ( )
go func ( ) {
tarReader := tar . NewReader ( inputTarStream )
tarWriter := tar . NewWriter ( pipeWriter )
defer inputTarStream . Close ( )
for {
hdr , err := tarReader . Next ( )
if err == io . EOF {
// Signals end of archive.
tarWriter . Close ( )
pipeWriter . Close ( )
return
}
if err != nil {
pipeWriter . CloseWithError ( err )
return
}
2016-08-29 16:36:16 -04:00
content := io . Reader ( tarReader )
2015-07-21 17:47:44 -04:00
if hdr . Name == dockerfileName {
// This entry is the Dockerfile. Since the tar archive was
// generated from a directory on the local filesystem, the
// Dockerfile will only appear once in the archive.
2016-01-12 16:18:24 -05:00
var newDockerfile [ ] byte
2016-05-21 09:57:57 -04:00
newDockerfile , * resolvedTags , err = rewriteDockerfileFrom ( ctx , content , translator )
2016-01-12 16:18:24 -05:00
if err != nil {
pipeWriter . CloseWithError ( err )
return
}
hdr . Size = int64 ( len ( newDockerfile ) )
content = bytes . NewBuffer ( newDockerfile )
2015-07-21 17:47:44 -04:00
}
if err := tarWriter . WriteHeader ( hdr ) ; err != nil {
pipeWriter . CloseWithError ( err )
return
}
if _ , err := io . Copy ( tarWriter , content ) ; err != nil {
pipeWriter . CloseWithError ( err )
return
}
}
} ( )
return pipeReader
}