Revert a Docker container back to its original image without restarting it?

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?

5 Answers

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:

2

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.

3

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.

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. 

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like