'Docker commit : Saving as new image by editing docker container

I have one docker image which spins up a container for executing some task in a small time period. Container is exited as soon as the task is completed.

Below is the output from docker ps -a command

CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS                      PORTS                                         NAMES
40be32cb4299   88841cd3d4a7        "/home/test/testing-…"   40 seconds ago   Exited (0) 22 seconds ago                                                 beautiful_agnesi

Since the container is automatically exited in small time period, I can't perform docker exec -it -u root 40be32cb4299 bash Output of exec command gives below error since container is exited.

Error response from daemon: Container 40be32cb4299 is not running 

Is there way for me to perform exec on this container for editing some files inside the same container in order to perform docker commit and save as new image ?



Solution 1:[1]

There is no way to exec into a stop container. But, you really have workaround to achieve you aim, something like next:

  1. docker commit 40be32cb4299 old_image, this commit the old stopped container as an old docker image.
  2. docker run -it --entrypoint=/bin/bash --name=new_container old_image, this use the old image to start a new container. As the entrypoint has been override, so the new_container will not run your default entrypoint or command, then new_container won't exit, and with -it, you are now in the new container.
  3. In new container, make file changes.
  4. Exit that new container, now you could use docker commit new_container new_image to commit your new changes to a new docker image.

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 atline