Docker: How should I cache docker image to save build time?

Hi there,

I am trying to dockerize our Rails app. The main problem for now is that it takes too long to build the image.

I followed this document: https://circleci.com/docs/docker/, and have the following configuration in my circle.yml:

dependencies:
  cache_directories:
    - "~/docker"
  pre:
    - docker info
  override:
    - if [[ -e ~/docker/myapp-web.tar ]]; then docker load -i ~/docker/myapp-web.tar; fi
    - docker images
    - docker build --rm=false -t myapp-web:latest .
    - docker images
    - mkdir -p ~/docker; docker save -o ~/docker/myapp-web.tar myapp-web:latest

The step of docker load restores the image as I wished:

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
myapp-web        latest              84d2c9bc48cd        19 minutes ago      2.411 GB

However the docker build step ignores that cache, and builds the image from ruby:2.3.3 (which I specified on FROM field in my Dockerfile):

$ docker build --rm=false -t myapp-web:latest .
Sending build context to Docker daemon 1.117 GB


Step 1 : FROM ruby:2.3.3
2.3.3: Pulling from library/ruby

# long process taking 5 ~ 8 minutes

And finally I got two images:

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
myapp-web        latest              b6590ab623dc        20 seconds ago      2.409 GB
<none>              <none>              84d2c9bc48cd        25 minutes ago      2.411 GB
ruby                2.3.3               990fb4b2a76c        47 hours ago        732 MB

I reproduced the same steps on my local macOS machine, and got the same problem.

I think I am doing something wrong, but not really sure what I should do to save build time.

I understand this is not a CircleCI-specific issue, but CircleCI’s document does not solve the problem I got.

Please help me, thanks!

Yucheng

2 Likes

I also have the same issue, willing to look for any workaround.