Can Travis CI cache docker images?

Docker images are not recommended to be cached regard to Travis document here https://docs.travis-ci.com/user/caching/#things-not-to-cache


Simplest solution today (October 2019) is to add the following to .travis.yml:

cache:
  directories:
  - docker_images

before_install:
- docker load -i docker_images/images.tar || true

before_cache:
- docker save -o docker_images/images.tar $(docker images -a -q)

See Caching Docker Images on Build #5358 for the answer(s). For Docker 1.12 available now on Travis, it is recommended to manually cache the images. For the Docker 1.13, you could use its --cache-from when it is on Travis.

Save:

before_cache:
  # Save tagged docker images
  - >
    mkdir -p $HOME/docker && docker images -a --filter='dangling=false' --format '{{.Repository}}:{{.Tag}} {{.ID}}'
    | xargs -n 2 -t sh -c 'test -e $HOME/docker/$1.tar.gz || docker save $0 | gzip -2 > $HOME/docker/$1.tar.gz'

Load:

before_install:
  # Load cached docker images
  - if [[ -d $HOME/docker ]]; then ls $HOME/docker/*.tar.gz | xargs -I {file} sh -c "zcat {file} | docker load"; fi

Also need to declare a cache folder:

cache:
  bundler: true
  directories:
    - $HOME/docker