github.com/squaremo/docker@v1.3.2-0.20150516120342-42cfc9554972/api/client/build.go (about) 1 package client 2 3 import ( 4 "bufio" 5 "encoding/base64" 6 "encoding/json" 7 "fmt" 8 "io" 9 "io/ioutil" 10 "net/http" 11 "net/url" 12 "os" 13 "os/exec" 14 "path" 15 "path/filepath" 16 "runtime" 17 "strconv" 18 "strings" 19 20 "github.com/docker/docker/api" 21 "github.com/docker/docker/graph/tags" 22 "github.com/docker/docker/pkg/archive" 23 "github.com/docker/docker/pkg/fileutils" 24 "github.com/docker/docker/pkg/jsonmessage" 25 flag "github.com/docker/docker/pkg/mflag" 26 "github.com/docker/docker/pkg/parsers" 27 "github.com/docker/docker/pkg/progressreader" 28 "github.com/docker/docker/pkg/streamformatter" 29 "github.com/docker/docker/pkg/symlink" 30 "github.com/docker/docker/pkg/units" 31 "github.com/docker/docker/pkg/urlutil" 32 "github.com/docker/docker/registry" 33 "github.com/docker/docker/utils" 34 ) 35 36 const ( 37 tarHeaderSize = 512 38 ) 39 40 // CmdBuild builds a new image from the source code at a given path. 41 // 42 // If '-' is provided instead of a path or URL, Docker will build an image from either a Dockerfile or tar archive read from STDIN. 43 // 44 // Usage: docker build [OPTIONS] PATH | URL | - 45 func (cli *DockerCli) CmdBuild(args ...string) error { 46 cmd := cli.Subcmd("build", "PATH | URL | -", "Build a new image from the source code at PATH", true) 47 tag := cmd.String([]string{"t", "-tag"}, "", "Repository name (and optionally a tag) for the image") 48 suppressOutput := cmd.Bool([]string{"q", "-quiet"}, false, "Suppress the verbose output generated by the containers") 49 noCache := cmd.Bool([]string{"#no-cache", "-no-cache"}, false, "Do not use cache when building the image") 50 rm := cmd.Bool([]string{"#rm", "-rm"}, true, "Remove intermediate containers after a successful build") 51 forceRm := cmd.Bool([]string{"-force-rm"}, false, "Always remove intermediate containers") 52 pull := cmd.Bool([]string{"-pull"}, false, "Always attempt to pull a newer version of the image") 53 dockerfileName := cmd.String([]string{"f", "-file"}, "", "Name of the Dockerfile (Default is 'PATH/Dockerfile')") 54 flMemoryString := cmd.String([]string{"m", "-memory"}, "", "Memory limit") 55 flMemorySwap := cmd.String([]string{"-memory-swap"}, "", "Total memory (memory + swap), '-1' to disable swap") 56 flCPUShares := cmd.Int64([]string{"c", "-cpu-shares"}, 0, "CPU shares (relative weight)") 57 flCpuPeriod := cmd.Int64([]string{"-cpu-period"}, 0, "Limit the CPU CFS (Completely Fair Scheduler) period") 58 flCpuQuota := cmd.Int64([]string{"-cpu-quota"}, 0, "Limit the CPU CFS (Completely Fair Scheduler) quota") 59 flCPUSetCpus := cmd.String([]string{"-cpuset-cpus"}, "", "CPUs in which to allow execution (0-3, 0,1)") 60 flCPUSetMems := cmd.String([]string{"-cpuset-mems"}, "", "MEMs in which to allow execution (0-3, 0,1)") 61 flCgroupParent := cmd.String([]string{"-cgroup-parent"}, "", "Optional parent cgroup for the container") 62 63 cmd.Require(flag.Exact, 1) 64 cmd.ParseFlags(args, true) 65 66 var ( 67 context archive.Archive 68 isRemote bool 69 err error 70 ) 71 72 _, err = exec.LookPath("git") 73 hasGit := err == nil 74 if cmd.Arg(0) == "-" { 75 // As a special case, 'docker build -' will build from either an empty context with the 76 // contents of stdin as a Dockerfile, or a tar-ed context from stdin. 77 buf := bufio.NewReader(cli.in) 78 magic, err := buf.Peek(tarHeaderSize) 79 if err != nil && err != io.EOF { 80 return fmt.Errorf("failed to peek context header from STDIN: %v", err) 81 } 82 if !archive.IsArchive(magic) { 83 dockerfile, err := ioutil.ReadAll(buf) 84 if err != nil { 85 return fmt.Errorf("failed to read Dockerfile from STDIN: %v", err) 86 } 87 88 // -f option has no meaning when we're reading it from stdin, 89 // so just use our default Dockerfile name 90 *dockerfileName = api.DefaultDockerfileName 91 context, err = archive.Generate(*dockerfileName, string(dockerfile)) 92 } else { 93 context = ioutil.NopCloser(buf) 94 } 95 } else if urlutil.IsURL(cmd.Arg(0)) && (!urlutil.IsGitURL(cmd.Arg(0)) || !hasGit) { 96 isRemote = true 97 } else { 98 root := cmd.Arg(0) 99 if urlutil.IsGitURL(root) { 100 root, err = utils.GitClone(root) 101 if err != nil { 102 return err 103 } 104 defer os.RemoveAll(root) 105 } 106 if _, err := os.Stat(root); err != nil { 107 return err 108 } 109 110 absRoot, err := filepath.Abs(root) 111 if err != nil { 112 return err 113 } 114 115 filename := *dockerfileName // path to Dockerfile 116 117 if *dockerfileName == "" { 118 // No -f/--file was specified so use the default 119 *dockerfileName = api.DefaultDockerfileName 120 filename = filepath.Join(absRoot, *dockerfileName) 121 122 // Just to be nice ;-) look for 'dockerfile' too but only 123 // use it if we found it, otherwise ignore this check 124 if _, err = os.Lstat(filename); os.IsNotExist(err) { 125 tmpFN := path.Join(absRoot, strings.ToLower(*dockerfileName)) 126 if _, err = os.Lstat(tmpFN); err == nil { 127 *dockerfileName = strings.ToLower(*dockerfileName) 128 filename = tmpFN 129 } 130 } 131 } 132 133 origDockerfile := *dockerfileName // used for error msg 134 if filename, err = filepath.Abs(filename); err != nil { 135 return err 136 } 137 138 // Verify that 'filename' is within the build context 139 filename, err = symlink.FollowSymlinkInScope(filename, absRoot) 140 if err != nil { 141 return fmt.Errorf("The Dockerfile (%s) must be within the build context (%s)", origDockerfile, root) 142 } 143 144 // Now reset the dockerfileName to be relative to the build context 145 *dockerfileName, err = filepath.Rel(absRoot, filename) 146 if err != nil { 147 return err 148 } 149 // And canonicalize dockerfile name to a platform-independent one 150 *dockerfileName, err = archive.CanonicalTarNameForPath(*dockerfileName) 151 if err != nil { 152 return fmt.Errorf("Cannot canonicalize dockerfile path %s: %v", *dockerfileName, err) 153 } 154 155 if _, err = os.Lstat(filename); os.IsNotExist(err) { 156 return fmt.Errorf("Cannot locate Dockerfile: %s", origDockerfile) 157 } 158 var includes = []string{"."} 159 160 excludes, err := utils.ReadDockerIgnore(path.Join(root, ".dockerignore")) 161 if err != nil { 162 return err 163 } 164 165 // If .dockerignore mentions .dockerignore or the Dockerfile 166 // then make sure we send both files over to the daemon 167 // because Dockerfile is, obviously, needed no matter what, and 168 // .dockerignore is needed to know if either one needs to be 169 // removed. The deamon will remove them for us, if needed, after it 170 // parses the Dockerfile. 171 keepThem1, _ := fileutils.Matches(".dockerignore", excludes) 172 keepThem2, _ := fileutils.Matches(*dockerfileName, excludes) 173 if keepThem1 || keepThem2 { 174 includes = append(includes, ".dockerignore", *dockerfileName) 175 } 176 177 if err := utils.ValidateContextDirectory(root, excludes); err != nil { 178 return fmt.Errorf("Error checking context is accessible: '%s'. Please check permissions and try again.", err) 179 } 180 options := &archive.TarOptions{ 181 Compression: archive.Uncompressed, 182 ExcludePatterns: excludes, 183 IncludeFiles: includes, 184 } 185 context, err = archive.TarWithOptions(root, options) 186 if err != nil { 187 return err 188 } 189 } 190 191 // windows: show error message about modified file permissions 192 // FIXME: this is not a valid warning when the daemon is running windows. should be removed once docker engine for windows can build. 193 if runtime.GOOS == "windows" { 194 fmt.Fprintln(cli.err, `SECURITY WARNING: You are building a Docker image from Windows against a Linux 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.`) 195 } 196 197 var body io.Reader 198 // Setup an upload progress bar 199 // FIXME: ProgressReader shouldn't be this annoying to use 200 if context != nil { 201 sf := streamformatter.NewStreamFormatter() 202 body = progressreader.New(progressreader.Config{ 203 In: context, 204 Out: cli.out, 205 Formatter: sf, 206 NewLines: true, 207 ID: "", 208 Action: "Sending build context to Docker daemon", 209 }) 210 } 211 212 var memory int64 213 if *flMemoryString != "" { 214 parsedMemory, err := units.RAMInBytes(*flMemoryString) 215 if err != nil { 216 return err 217 } 218 memory = parsedMemory 219 } 220 221 var memorySwap int64 222 if *flMemorySwap != "" { 223 if *flMemorySwap == "-1" { 224 memorySwap = -1 225 } else { 226 parsedMemorySwap, err := units.RAMInBytes(*flMemorySwap) 227 if err != nil { 228 return err 229 } 230 memorySwap = parsedMemorySwap 231 } 232 } 233 // Send the build context 234 v := &url.Values{} 235 236 //Check if the given image name can be resolved 237 if *tag != "" { 238 repository, tag := parsers.ParseRepositoryTag(*tag) 239 if err := registry.ValidateRepositoryName(repository); err != nil { 240 return err 241 } 242 if len(tag) > 0 { 243 if err := tags.ValidateTagName(tag); err != nil { 244 return err 245 } 246 } 247 } 248 249 v.Set("t", *tag) 250 251 if *suppressOutput { 252 v.Set("q", "1") 253 } 254 if isRemote { 255 v.Set("remote", cmd.Arg(0)) 256 } 257 if *noCache { 258 v.Set("nocache", "1") 259 } 260 if *rm { 261 v.Set("rm", "1") 262 } else { 263 v.Set("rm", "0") 264 } 265 266 if *forceRm { 267 v.Set("forcerm", "1") 268 } 269 270 if *pull { 271 v.Set("pull", "1") 272 } 273 274 v.Set("cpusetcpus", *flCPUSetCpus) 275 v.Set("cpusetmems", *flCPUSetMems) 276 v.Set("cpushares", strconv.FormatInt(*flCPUShares, 10)) 277 v.Set("cpuquota", strconv.FormatInt(*flCpuQuota, 10)) 278 v.Set("cpuperiod", strconv.FormatInt(*flCpuPeriod, 10)) 279 v.Set("memory", strconv.FormatInt(memory, 10)) 280 v.Set("memswap", strconv.FormatInt(memorySwap, 10)) 281 v.Set("cgroupparent", *flCgroupParent) 282 283 v.Set("dockerfile", *dockerfileName) 284 285 headers := http.Header(make(map[string][]string)) 286 buf, err := json.Marshal(cli.configFile.AuthConfigs) 287 if err != nil { 288 return err 289 } 290 headers.Add("X-Registry-Config", base64.URLEncoding.EncodeToString(buf)) 291 292 if context != nil { 293 headers.Set("Content-Type", "application/tar") 294 } 295 sopts := &streamOpts{ 296 rawTerminal: true, 297 in: body, 298 out: cli.out, 299 headers: headers, 300 } 301 err = cli.stream("POST", fmt.Sprintf("/build?%s", v.Encode()), sopts) 302 if jerr, ok := err.(*jsonmessage.JSONError); ok { 303 // If no error code is set, default to 1 304 if jerr.Code == 0 { 305 jerr.Code = 1 306 } 307 return StatusError{Status: jerr.Message, StatusCode: jerr.Code} 308 } 309 return err 310 }