2

let's supposse I have a Dockerfile like this:

FROM debian:stretch

RUN apt update
RUN apt install -y wget 

RUN wget https://stackoverflow.com/
# I know the wget is useless. Is just an example :)
CMD ["echo", "hello-world"]

I want to put over the wget statement, a new RUN statement. After this change, when I rebuild, It will re-run all the commands from my modification to down, so the wget will be executed again. The problem is that the wget command takes so much time to finish because on my real file, the file is a very big file.

The question is, can be docker "tweaked" somewhere in order to avoid on building the execution again of the wget layer? If I already built it, can that layer be used again even changing a statement over it?

Thank you.

OscarAkaElvis
  • 5,384
  • 4
  • 27
  • 51

2 Answers2

2

AFAIK this is not possible, as docker only reuses the layers up until your change and starts to build again from there on out.

This is because the new layers get tested on the previously built layers (so your RUN wget layer is tested and built on the layers from FROM to RUN apt install -y wget). So if you'd enter another RUN instruction above the RUN wget instruction, you'd get a changed environment for your RUN wget instruction, so it needs to be executed again.

I don't think there's a way to fidget with it manually so it would reuse the layer built on a "different" environment and neither would I recommend it.

samprog
  • 2,454
  • 1
  • 13
  • 18
1

Using docker-compose, or the -v flag when running docker run you can mount a volume that will persist between runs. Change your wget to a script that conditionally runs in absence of the file.

That won’t cache the later but will make that step faster.

You may need to modify the folder where you store that file depending on the rest of your script and how your environment is set up.

I’m using compose for volume mounting here: https://github.com/jaydorsey/ghgvcR/blob/master/docker-compose.yml

Look at the bin/download-files.sh file in that repo for a bash example.

Jay Dorsey
  • 3,563
  • 2
  • 18
  • 24