user2859458
user2859458

Reputation: 2505

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?

Upvotes: 20

Views: 32936

Answers (5)

majorobot
majorobot

Reputation: 142

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.

Upvotes: 0

John
John

Reputation: 3996

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.

Upvotes: 0

Nebri
Nebri

Reputation: 843

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

Upvotes: 6

GHETTO.CHiLD
GHETTO.CHiLD

Reputation: 3416

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.

Upvotes: 0

Michael
Michael

Reputation: 10474

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.

Upvotes: 1

Related Questions