Docker Tricks to Remember

Chances are if you have been working with docker for a while you may have noticed that removing containers does not free up the disk space associated with that container.  When removing the container the -v flag can be used to remove the volumes associated with that container.

[code language=”bash”]docker rm -v webapp[/code]

If it’s too late and you have a lot of orphaned containers you can use the docker-volume script to clean up your machine. https://github.com/cpuguy83/docker-volumes

Backing up Data in a Container

Backing up a docker volume may seem to be tricky since the data is buried on your filesystem.  But it’s actually easy to do by mapping a temporary container to your postgres volume and a local backup volume.

[code language=”bash”]

#Backup data in a volume
#remove it no need to keep it around.

$ docker run –rm –volumes-from dbdata -v $(pwd):/backup busybox tar cvf /backup/backup.tar /var/lib/postgresql/data

[/code]

Upgrading a container

Upgrading a container to a newer version can use a similar technique.  Stopping a container does not remove a volume.

[code language=”bash”]

$docker stop dbdata

$docker run –name newdbdata –volumes-from dbdata postgres:latest

$docker rm dbdata

[/code]

Accessing a shell in your container.

[code language=”bash”]

$docker excec -rm -ti web bash

[/code]

You may also like...