domingo, 19 de fevereiro de 2017

Docker: only commit to image if container running


Docker: only commit to image if container running

Safe backup image overwriting #1



With docker, there are several ways to backup your data or entire containers; one of those ways is to commit to an image, which can later be used to fire a container with the exact same contents the source container had when that image was created.

But if for some reason the container gone bad, you don't want to overwrite that image with its contents. One way is to check if the container is running: if running then odds are that everything is ok, and we can go ahead and replace last image with current contents one:

#!/bin/bash
if $(docker inspect -f {{.State.Running}} $1);  
then
     docker commit $1 $1_fb
fi

In  this case, I'm creating an image with the name of the running container plus a "_fb" suffix to identify that it's the one with my contents.

Call it via
./<script name>.sh <container name or id>
If you want to make some basic clean-up, delete the untagged images by adding to the script
docker rmi $(docker images -a | grep "^<none>" | awk '{print $3}')
Enjoy! 

Sem comentários:

Enviar um comentário