Home

Awesome

Docker-Lock-Banner

About

ci cd-master cd-tag Go Report Card PkgGoDev

docker-lock is a cli tool that automates managing image digests by tracking them in a separate Lockfile (think package-lock.json or Pipfile.lock). With docker-lock, you can refer to images in Dockerfiles, docker-compose V3 files, and Kubernetes manifests by mutable tags (as in python:3.6) yet receive the same benefits as if you had specified immutable digests (as in python:3.6@sha256:25a189a536ae4d7c77dd5d0929da73057b85555d6b6f8a66bfbcc1a7a7de094b).

Note: If you are unsure about the differences between tags and digests, refer to this quick summary.

docker-lock ships with 3 commands that take you from development to production:

docker-lock is most commonly used as a cli-plugin for docker so lock can be used as subcommand of docker as in docker lock. However, docker-lock does not require docker at all. Instead, it can be called manually as a standalone executable as in docker-lock lock. This is especially convenient if the proper version of docker is unavailable or you would prefer to use another container technology such as podman.

Demo

Consider a project with a multi-stage build Dockerfile at its root:

FROM ubuntu AS base
# ...
FROM mperel/log:v1
# ...
FROM python:3.6
# ...

Running docker lock generate from the root queries each images' registry to produce a Lockfile, docker-lock.json.

Generate GIF

Note that the Lockfile records image digests so you do not have to manually specify them.

Running docker lock verify ensures that the image digests are the same as those on the registry for the same tags.

Verify Success GIF

Now, assume that a change to mperel/log:v1 has been pushed to the registry.

Running docker lock verify shows that the image digest in the Lockfile is out-of-date because it differs from the newer image's digest on the registry.

Verify Fail GIF

While developing, it can be useful to generate a Lockfile, commit it to source control, and verify it periodically (for instance on PR merges). In this way, developers can be notified when images change, and if a bug related to a change in an image crops up, it will be easy to identify.

Finally, lets assume the Dockerfile is ready to be built and shared.

Running docker lock rewrite will add digests from the Lockfile to all of the images.

Rewrite GIF

At this point, the Dockerfile will contain all of the digest information from the Lockfile, so it will always maintain the same, known behavior in the future.

Install

docker-lock can be run as a

Cli-plugin

Ensure docker cli version >= 19.03 is installed by running docker --version.

Linux / Mac

$ mkdir -p "${HOME}/.docker/cli-plugins"
$ curl -fsSL "https://github.com/safe-waters/docker-lock/releases/download/v${VERSION}/docker-lock_${VERSION}_${OS}_${ARCH}.tar.gz" | tar -xz -C "${HOME}/.docker/cli-plugins" "docker-lock"
$ chmod +x "${HOME}/.docker/cli-plugins/docker-lock"

Windows

Standalone tool

$ docker-lock lock --help

Docker image

docker-lock can be run in a docker container, as below. If you leave off the ${VERSION} tag, you will use the latest, nightly build from the master branch.

Note: If your host machine uses a credential helper such as osxkeychain, wincred, or pass, the credentials will not be available to the container even if you pass in your docker config.

Linux / Mac

$ docker run -v "${PWD}":/run safewaters/docker-lock:${VERSION} [commands]
$ docker run -v "${HOME}/.docker/config.json":/.docker/config.json:ro -v "${PWD}":/run safewaters/docker-lock:${VERSION} [commands]

Windows

$ docker run -v "%cd%":/run safewaters/docker-lock:${VERSION} [commands]
$ docker run -v "%USERPROFILE%\.docker\config.json":/.docker/config.json:ro -v "%cd%":/run safewaters/docker-lock:${VERSION} [commands]

Available tags

Use

Registries

docker-lock supports public and private registries. If necessary, login to docker before using docker-lock.

How to specify configuration options

docker-lock supports options via cli flags or a configuration file, .docker-lock.yml. The root of this repo has an example, .docker-lock.yml.example.

To see available options, run commands with --help. For instance:

$ docker lock --help
$ docker lock generate --help
$ docker lock verify --help
$ docker lock rewrite --help
$ docker lock version --help

Note: You can mix and match cli flags to get the output that you want.

Generate

Commands for Dockerfiles, docker-compose files, and Kubernetes manifests

Commands for Dockerfiles

Commands for docker-compose files

Commands for Kubernetes manifests

Verify

Rewrite

Suggested workflow

Contributing

Development environment

A development container based on ubuntu:bionic has been provided, so ensure docker is installed and the docker daemon is running.

Build from source

To build and install docker-lock in docker's cli-plugins directory, from the root of the project, run:

$ make install

Code quality and correctness

To clean, format, lint, install, generate a new Lockfile, and run unit tests:

make

The CI pipeline will additionally run integration tests on pull requests.

You can run any step individually.

To view the coverage report after running unit tests, open coverage.html in your browser.

Note: While there exists a target in the Makefile for integration tests, these cannot run locally because they require credentials that are only available in the CI pipeline.

Tutorials