Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 916
vijaygopal Avatar asked May 27 '17 23:05

vijaygopal


People also ask

Can we make changes in docker image?

Docker images can now be edited simply and reliably. This is an example of a Dockerfile for a Zabbix monitoring container. To change the image used by an existing container, we must first delete it, then edit the Docker file to make the necessary changes, and then recreate the container using the new file.

Is the command to commit changes done in a docker image?

Docker's commit command allows users to take a running container and save its current state as an image. This means to add our new user, we will need a running container. To get started, let's go ahead and launch a Redis container with the docker run command.

How do I save changes within a docker container?

If you want to make changes inside the container and want those changes to persist, you can use the Docker commit command. This will create a new image with all the changes made to the previous container committed to it.


2 Answers

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).

like image 127
hurturk Avatar answered Sep 17 '22 12:09

hurturk


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.

like image 40
Milind Deore Avatar answered Sep 20 '22 12:09

Milind Deore