Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker graph caching #36

Merged
merged 5 commits into from
Feb 18, 2016
Merged
Show file tree
Hide file tree
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
32 changes: 29 additions & 3 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,36 @@ publish:
build_args:
- HTTP_PROXY=http://yourproxy.com
```

## Layer Caching

The Drone build environment is, by default, ephemeral meaning that you layers are not saved between builds. The below example combines Drone's caching feature and Docker's `save` and `load` capabilities to cache and restore image layers between builds:
## Caching

The Drone build environment is, by default, ephemeral meaning that you layers are not saved between builds. There are two methods for caching your layers.

### Graph directory caching

This is the preferred method when using the `overlay` or `aufs` storage drivers. Just use Drone's caching feature to backup and restore the directory `/drone/docker`, as shown in the following example:

```yaml
publish:
docker:
username: kevinbacon
password: pa55word
email: [email protected]
repo: foo/bar
tag:
- latest
- "1.0.1"

cache:
mount:
- /drone/docker
```

NOTE: This probably won't work correctly with the `btrfs` driver, and it will be very inefficient with the `devicemapper` driver. Please make sure to use the `overlay` or `aufs` storage driver with this method.

### Layer Caching

The below example combines Drone's caching feature and Docker's `save` and `load` capabilities to cache and restore image layers between builds:

```yaml
publish:
Expand Down
26 changes: 25 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -93,7 +94,7 @@ func main() {
}

go func() {
args := []string{"daemon"}
args := []string{"daemon", "-g", "/drone/docker"}

if len(vargs.Storage) != 0 {
args = append(args, "-s", vargs.Storage)
Expand Down Expand Up @@ -228,6 +229,29 @@ func main() {
}
}

// Remove untagged images, if any
var outbuf bytes.Buffer
cmd = exec.Command("docker", "images", "-q", "-f", "dangling=true")
cmd.Stdout = &outbuf
cmd.Stderr = os.Stderr
trace(cmd)
err = cmd.Run()
if err != nil {
os.Exit(1)
}

if outbuf.Len() > 0 {
images := strings.Split(strings.TrimSpace(outbuf.String()), "\n")
cmd = exec.Command("docker", append([]string{"rmi"}, images...)...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
trace(cmd)
err := cmd.Run()
if err != nil {
os.Exit(1)
}
}

// Save to tarred image repository
if len(vargs.Save.File) != 0 {
// if the destination directory does not exist, create it
Expand Down