gak
gak

Reputation: 32773

Is there any way to "apply a patch" to a docker image?

Is there any way to "apply a patch" to a docker image, particularly one or more RUN, ADD, etc. commands?

For example a RUN command might take 20 minutes to run, that downloads, compiles, and installs a binary. Is it possible to take a diff of that and apply it to another image?

The only way I can think of (which I haven't attempted yet) is to run docker diff, parse the output, create a tgz, then use the tgz as an ADD in another Dockerfile.

I understand that there are issues with this, e.g. if an apt-get update is called beforehand which might break an expected dynamic library linkage for the binary, etc. I'm OK with this as my tests will fail, and will show that I have to rebuild the "diff" again.

I also realise there could be conflicts. I'm happy to replace the file completely.

The reason for this functionality is to save time. For example sometimes early Dockerfile commands need to be changed, and will break the cache. Also, as much as I try to make them identical to take advantage of cache, it is not always possible for the preceding commands in two different Dockerfiles to be the same.

Upvotes: 7

Views: 21571

Answers (2)

Dinesh Reddy
Dinesh Reddy

Reputation: 1752

I see we can use different approaches to apply patches or updates.

1.Patching using the Dockerfile
2.Patching an instance of the container and converting it to an image

I follow the second procedure to apply any patches. This helps me to test and update my image before building or pushing to repo.

 1. Create a container from the image you would like to update.
 2. Inside the container, run the native package manager and update the  desired packages or patches.
 3. Exit the container instance.
 4. Commit the changes done to get the updated image.
 5. Stop the containers that were instantiated from the pre-updated image.
 6. Instantiate new containers from the update image.

Upvotes: 0

larsks
larsks

Reputation: 312038

The short answer is that no, you can't do that.

The long answer is that with sufficient motivation you might be able to write code that would do what you want. I've written some documentation on the docker image format (and a tool for manipulating those images) here:

That might give you some ideas about where to start.

Personally, I suspect the effort is not worth it.

Upvotes: 2

Related Questions