19

Normally, people are all about making Docker persist data in their containers and there are about twenty million questions on how to do exactly that, but I'm a tester and I want to dump all that crap I just did to my data and revert to my known state (aka my image).

I'm aware I can do this by spinning up a new container based on my image but this forces me to disconnect and reconnect any network connections to my container and that's a huge pain.

Is it possible to revert a running container to its original image without restarting it?

j08691
  • 204,283
  • 31
  • 260
  • 272
user2859458
  • 2,285
  • 1
  • 17
  • 27

5 Answers5

6

Sadly while it's running you won't be able to revert or change the image. You'll need to stop your running containers and remove them. Once your volumes are no longer attached to any containers, running the command docker volume prune will destroy all volumes not currently attached to containers.

Then you can simply restart your docker containers from the images, and you'll have a fresh start again.

I also found this article to be a great reference when I was learning docker: https://web.archive.org/web/20190528002402/https://medium.com/the-code-review/top-10-docker-commands-you-cant-live-without-54fb6377f481

thelogix
  • 580
  • 2
  • 14
Nebri
  • 773
  • 2
  • 8
  • 21
  • That's okay that you are responding to an older question. I still think it has value. And who knows, maybe someday what I want will be made possible :) – user2859458 Jan 28 '19 at 23:48
  • This did not work for me for a use case where I start my container, start a test run, test run takes for ever and builds up a large backlog, stop container, do the prune, restart container, container picks up old data and continues the long running process. – John Dec 20 '21 at 11:41
1

To revert back to the original state, you have to restart the container - this is important because a container image is just a bunch of files, the actual running container must start some process and because of that, you cannot revert the container while running, since that process will most likely have issues.

So to answer your question - restart the container, a docker image only takes milliseconds to start up - the rest of the time is the process starting up.

Michael
  • 10,124
  • 1
  • 34
  • 49
  • 2
    This only works if a volume is not created by the image. If the image has a volume command the data will persist regardless of a restart. – GHETTO.CHiLD Aug 27 '15 at 00:01
  • 5
    Unfortunately, simply restarting a container (even one that doesn't write its data to a volume) doesn't remove changes. I have to call `docker run` on my image to get a container with a fresh image. `docker restart` or `docker stop/start` is insufficient. – user2859458 Aug 27 '15 at 15:50
  • I should have been more clear, I did not mean `docker restart` rather create a new container. – Michael Aug 27 '15 at 17:51
0

Do not mount a volume to the container. Volumes, whether a data or a fs mount are persistent. If you do not persist the data you can then go docker restart my container.

GHETTO.CHiLD
  • 3,267
  • 1
  • 22
  • 34
0

I'm in a Windows environment. This script shown below works for me. Basically you are deleting the container (which is ok because it is easily rebuilt from the image when docker up is called) and then deleting the now orphaned volumes.

This deletes ALL of the containers running in Docker which works for me as I'm only running one app. If you are running multiple apps you will probably want to modify your solution.

I'm not sure how to delete just the top level app by name.

(replace "myapp" with the name of your app)

@echo off
echo.
echo.
echo Deleting Containers...
FOR /f "tokens=*" %%i IN ('docker ps -aq') DO docker rm %%i
echo.
echo Pruning orphaned volumes
docker volume prune -f
echo.
echo Starting myapp...
docker-compose -p myapp -f ../tools/docker-compose.yml up --remove-orphans
echo.
echo.
echo Done.
echo.
echo.
John
  • 3,458
  • 4
  • 33
  • 54
0

In my experience, the quickest way to reset your environment does include taking the container down. But it's really not that painful.

Docker-compose can help you here:

docker-compose down
docker-compose up -d

That's it.

majorobot
  • 133
  • 4
  • 8