Docker cache question

My Dockerfile does basically 4 things:

#1 install mysql
#2 load a dump
#3 install other software
#4 applies incremental changes to the schema and data based on an added changelog.sql file (product is liquibase).

I build the image the first time, all good.

Second time, I may have changed the changelog.sql file. And I’d like the build to start from 4, to apply just the new changes.
It works, docker starts from 4. But the problem is that the image state is the one after point 3. And this means it is going to apply ALL the changes on every build. If I could instruct Docker to start from 4, using the cache from 4 instead of 3, it would work.

Is there any way, or can I change the dockerfile to achieve this result?

Basically I would like to touch this changelog.sql and be able to build an image quickly without having to apply all the changes again and again, but just the new ones.
When I run 4, Liquibase applies the statements in changelog.sql but skip the ones that have already been executed. Statements that have been executed are saved into a “log” (db table).
When I run a build after changing changelog.sql file, it start from 3 but database state in layer 3 doesn’t contain the “log” table from Liquibase (because it exists after layer 4).

It’s kinda recursive problem! :slight_smile: