'Docker Commit on a existing image

docker commit creates a new image every time the commit command is issued. Is it possible to issue commit on currently running container and the changes get saved to the existing image? (existing image here is the image from which the container was spawned). This way no new image would be created everytime I execute commit.



Solution 1:[1]

Using names and tags already updates the image you want. Make sure you put the name of the same image after container id during commit. From the doc:

$ docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS              NAMES
c3f279d17e0a        ubuntu:12.04        /bin/bash           7 days ago          Up 25 hours                            desperate_dubinsky
197387f1b436        ubuntu:12.04        /bin/bash           7 days ago          Up 25 hours                            focused_hamilton

$ docker commit c3f279d17e0a  svendowideit/testimage:version3

Edit: I highly recommend you to stop the container before proceeding, so you would reduce your chance getting into stale state for the next run. Also, it might be good idea to use a different tag, test it, then re-tag to your real target.

Edit2: As pointed by @MilindDeore, this doesn't physically overwrite the previous image, but re-address the name. So, you might have to do a manual deletion or wait it to be garbage collected (if in place).

Solution 2:[2]

Docker images are immutable, below text from the documentation:

The Docker image is read-only. When Docker runs a container from an image, it adds a read-write layer on top of the image (using a UnionFS) in which your application runs.

Hence suggestion given by @hurturk is going to create a new image and not what is asked in the question.

Solution 3:[3]

To create a new image from changes to a container, it’s a simple as running just one command. Before we do so, however, let’s change the container!

Within this container we are free to do anything we would typically do on Fedora. In this case, we’re going to install Git and then commit the container. The linked instructions are for installing Git on CentOS 6, but they’ll work for this Fedora container too.

Once you’ve completed those instructions you can disconnect, or detach, from the shell without exiting use the escape sequence Ctrl-p + Ctrl-q.

Finally it’s time to commit our changes to a named image. This command converts the container 9c09acd48a25 to an image with the name fedora-base-with-git:

docker commit 9c09acd48a25 fedora-base-with-git

Docker uses the “commits are cheap” concept in its design. You’re encouraged to commit frequently and at any point in an image’s history, much like version control.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Milind Deore
Solution 3 AJ-