justpull

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 22 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

justpull 🔁

docker pull, but it doesn't start over.

I got tired of watching docker pull fail halfway, dozens and dozens of times, because of one small network blip. A blip like that should be completely recoverable. Instead, Docker threw the progress away and downloaded the whole huge layer again. 😩

justpull keeps every byte it already got. A broken download picks up right where it stopped: inside one pull, after a failed attempt, even after a restart.

justpull alpine:3.20
justpull --platform linux/arm64 ghcr.io/example/app@sha256:...
justpull -a example/app

It's a drop-in: same flags, same output, same exit codes as docker pull. Your docker login just works.

📦 Get it

  • Grab a binary from the releases. That's all you need.
  • Or use Go: go install github.com/cryguy/justpull/cmd/justpull@latest. The first run pulls the proxy image ghcr.io/cryguy/justpull.

You need Docker with Linux containers. It's been tested on Docker Desktop for Windows and on Colima on macOS.

⚙️ How it works

No magic, just a tiny proxy. justpull starts it in a container and has Docker pull through it. The proxy saves each layer into a cache volume, justpull-cache, and streams it on to Docker as it arrives. When the connection drops, the proxy reconnects and carries on from the last byte. Docker never even notices.

A few things worth knowing:

  • 🐢 Slow is fine, stuck isn't. If a layer gets no new byte for 10 minutes, the pull gives up.
  • ✅ No half-baked layers. Docker only gets a layer's last byte once its sha256 checks out.
  • 🧹 It cleans up. After a good pull the cached files are gone. After a failed one they stick around for 24 hours, so the next try can use them.
  • 💾 Disk space: while a pull runs, the layers sit on disk twice, once in the cache and once in Docker.
  • 🔒 Locked down. The proxy runs as nobody, read-only, with no capabilities. Your login reaches it over stdin, never in its arguments or environment.
  • 🌐 HTTPS_PROXY and NO_PROXY work.

Done with it? This removes everything:

docker volume rm justpull-cache
docker image rm $(docker image ls -q ghcr.io/cryguy/justpull)

🐹 Use it from Go

Want the same trick inside your own program? The library does the resuming; you hand it three Docker calls.

cache, err := justpull.New("/var/lib/example/pullcache", justpull.Options{})
if err != nil {
	return err
}
err = cache.Pull(ctx, dockerAdapter{cli}, "alpine:3.20", nil)

With github.com/docker/docker/client, the adapter is:

type dockerAdapter struct{ cli *client.Client }

func (d dockerAdapter) Pull(ctx context.Context, ref string) (io.ReadCloser, error) {
	return d.cli.ImagePull(ctx, ref, image.PullOptions{})
}

func (d dockerAdapter) Tag(ctx context.Context, source, target string) error {
	return d.cli.ImageTag(ctx, source, target)
}

func (d dockerAdapter) Untag(ctx context.Context, ref string) error {
	_, err := d.cli.ImageRemove(ctx, ref, image.RemoveOptions{})
	return err
}

Logins, progress, sharing one cache between processes, and making the Docker calls yourself are all in the package docs. It has one dependency, github.com/distribution/reference.

⚠️ The library needs a Docker that can reach the machine's own 127.0.0.1, like stock Docker on Linux. Docker Desktop and Colima run Docker inside a VM, which can't, so on those use the justpull command instead.

🛠️ Hacking on it

PRs are welcome! 🙌 Found a bug or have an idea? Open an issue or send a PR. Before you send one, run the tests:

go test -race ./...   # Linux or macOS; no Docker or network needed
scripts/e2e.sh        # the real thing, against your Docker

scripts/e2e.sh needs the image alpine:3.20, and it deletes the justpull-cache volume when it's done.

📄 License

Apache-2.0. See LICENSE.

Documentation

Overview

Package justpull pulls container images through a local pull-through proxy that keeps every downloaded byte on disk, so a pull broken by the network, a failed attempt or a restart continues where it stopped instead of starting over.

Docker is pointed at 127.0.0.1:<port>/<repository>, which dockerd treats as an insecure registry by default. It asks the proxy only for the blobs it does not have, on either image store; the proxy fetches them from the real registry, resuming after any break, and serves them while they download, so Docker's own connection never breaks. After a successful pull the image gets its real name, the localhost name is removed and the cached files are deleted.

Index

Constants

View Source
const (
	// DefaultStallLimit is how long a layer may go without a new byte from
	// the registry, reconnects included, before the pull fails. A slow link
	// that keeps moving is never cut off.
	DefaultStallLimit = 10 * time.Minute
	// DefaultMaxAge is how long Sweep keeps a cached file after its last
	// write.
	DefaultMaxAge = 24 * time.Hour
)

Variables

View Source
var ErrRefused = errors.New("access refused")

ErrRefused is wrapped by the error of a registry that refused the login, or refused access to the image: a login, or another one, may help.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache stores blobs on disk by digest and pulls images through them, so a download broken by the network, a failed pull or a restart continues where it stopped. Layout: <dir>/blobs/sha256/<hex> once verified, <hex>.partial while downloading; <dir>/manifests/sha256/<hex> for the manifests relayed, which name an image's blobs for cleanup; <dir>/locks/sha256/<hex>.dl and <hex>.use, the flock(2) files that coordinate the processes sharing the directory.

Several processes, or several Caches, may share a directory. A blob is downloaded by one of them at a time; the others stream it from the cache file as it arrives, and one of them takes the download over from what is on disk if its downloader dies or gives up. Cleanup and Sweep skip blobs another process uses. Unix only (Linux, macOS, the BSDs): New fails elsewhere. A Cache's methods are safe for concurrent use.

func New

func New(dir string, opts Options) (*Cache, error)

New opens (creating if needed) a cache rooted at dir.

func (*Cache) Pull

func (c *Cache) Pull(ctx context.Context, d Docker, ref string, progress func(Progress)) error

Pull pulls ref into Docker through the cache. On success the image is known by ref, as after a plain docker pull, and this pull's cached files are gone. On failure or cancel they stay, so the next Pull of an image sharing those layers continues where this one stopped. progress may be nil; it is called from Pull's goroutine.

func (*Cache) Serve

func (c *Cache) Serve(ctx context.Context, ref string, opts SessionOptions) (*Session, error)

Serve sweeps the cache, then serves ref on 127.0.0.1 until Stop or Finish.

func (*Cache) Sweep

func (c *Cache) Sweep() error

Sweep deletes cached files that have not been written for MaxAge and are not in use by this or another process, and the lock files of blobs whose files are gone. Pull runs it first; a long-running caller should also run it periodically so a cancelled pull's files go even when no new pull comes.

type Credential

type Credential struct {
	Username string
	Password string
	// IdentityToken is an OAuth2 refresh token. When set it is used
	// instead of Username and Password.
	IdentityToken string
}

Credential is a registry login, as `docker login` stores it.

type Docker

type Docker interface {
	Pull(ctx context.Context, ref string) (io.ReadCloser, error)
	Tag(ctx context.Context, source, target string) error
	Untag(ctx context.Context, ref string) error
}

Docker is the part of the Docker API a pull needs. References are plain image references. An implementation over the Docker client:

Pull:  ImagePull(ref) with no registry auth, returning the progress stream
Tag:   ImageTag(source, target)
Untag: ImageRemove(ref) with Force and PruneChildren off

type Options

type Options struct {
	// StallLimit fails a pull when a layer has received no new bytes for
	// this long. Zero means DefaultStallLimit.
	StallLimit time.Duration
	// MaxAge is how long Sweep keeps a cached file after its last write.
	// Zero means DefaultMaxAge.
	MaxAge time.Duration
	// Logger receives warnings that do not fail a pull. Nil means
	// slog.Default().
	Logger *slog.Logger
	// Credentials returns the login for a registry, named as in image
	// references: docker.io for Docker Hub, ghcr.io, localhost:5000,
	// 127.0.0.1:5000. It is called only when a registry answers 401 with a
	// challenge, and may be called concurrently. A zero Credential means
	// anonymous; nil means every pull is anonymous.
	Credentials func(ctx context.Context, registry string) (Credential, error)
}

Options tunes a Cache. The zero value uses the defaults.

type Progress

type Progress struct {
	Done  int64
	Total int64
}

Progress is how far a pull's downloads are, in compressed bytes. Layers Docker already has are not counted.

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session is the proxy of one pull without the Docker calls, for a caller that drives Docker itself: Docker pulls LocalRef, the caller tags the image, and Finish ends the session. Pull is built on it.

func (*Session) Context

func (s *Session) Context() context.Context

Context ends when a layer fails for good, with that failure as its cause, or when Stop runs. The caller then cancels Docker's pull.

func (*Session) Finish

func (s *Session) Finish(success bool) error

Finish stops the session. After a successful pull it deletes the image's cached blobs, except those another process is using, and its saved manifests; after a failed one they stay, so the next pull of an image sharing those layers continues from them. The error is about deleting.

func (*Session) LayerSize

func (s *Session) LayerSize(shortID string) int64

LayerSize returns the size the image's manifests give for the layer that Docker's progress stream names by its 12-character ID, or 0 if unknown.

func (*Session) LocalRef

func (s *Session) LocalRef() string

LocalRef is the reference Docker pulls: 127.0.0.1:<port>/<repository>, with the tag or digest of the reference served unless AllTags is set.

func (*Session) Stop

func (s *Session) Stop()

Stop stops serving, waits for running requests and downloads, and lets other processes clean up the image's blobs again. The cached files stay. Calling it again does nothing.

type SessionOptions

type SessionOptions struct {
	// AllTags serves every tag of the repository and its tag list, for
	// docker pull --all-tags. The reference must then name no tag or digest.
	AllTags bool
}

SessionOptions tunes a Session. The zero value serves the image the reference names.

Directories

Path Synopsis
cmd
justpull command
Command justpull is docker pull through a cache that keeps every downloaded byte, so a broken pull continues where it stopped.
Command justpull is docker pull through a cache that keeps every downloaded byte, so a broken pull continues where it stopped.
internal
dockerconfig
Package dockerconfig finds the registry login the docker CLI would use, from DOCKER_AUTH_CONFIG, its config file and its credential helpers.
Package dockerconfig finds the registry login the docker CLI would use, from DOCKER_AUTH_CONFIG, its config file and its credential helpers.
engine
Package engine talks to the Docker daemon that the user's docker command reaches, whatever its context, DOCKER_HOST or transport, without the Docker SDK.
Package engine talks to the Docker daemon that the user's docker command reaches, whatever its context, DOCKER_HOST or transport, without the Docker SDK.
fakedocker
Package fakedocker is a Docker for tests.
Package fakedocker is a Docker for tests.
fakereg
Package fakereg is an in-process OCI registry for tests: images with random layers, a paged tag list, optional bearer auth, an optional login (basic, or for the token), optional redirects to a separate storage host whose links expire, scripted faults on blob GETs, and an optional rate limit on blob bodies.
Package fakereg is an in-process OCI registry for tests: images with random layers, a paged tag list, optional bearer auth, an optional login (basic, or for the token), optional redirects to a separate storage host whose links expire, scripted faults on blob GETs, and an optional rate limit on blob bodies.
proxyimage
Package proxyimage makes the image that runs the justpull proxy present in Docker: imported from the Linux binary a release build embeds, or pulled.
Package proxyimage makes the image that runs the justpull proxy present in Docker: imported from the Linux binary a release build embeds, or pulled.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址