Snapshot a docker container
I was talking with a friend earlier this week. Docker has many uses.
Historically, I have loved to virtualize all my work, my tool of choice, VMWare Fusion/Workstation that allows me to move between Mac and Windows at ease.
But VMs are heavy! So while docker has many purposes, and it’s true value shines for deployment/manageability, I find it quite useful for development too.
For instance, in the version hell we live in, I found it very convenient to install tensorflow as a docker image, instead of installing it on my main machine.
Another example is some Node 6 code I need to support, and I really dislike nvm. Docker to the rescue.
The thing is, how do you go about “snapshotting” a docker container, like you do with VMs? Easy! Your magic bullet is,
docker commit
Here is how to use it.
Start a docker container, for example, I have a docker container I have that gives me a clean linux dev environment instantly, I call it “ubuntu:sahil” (here are the details on how I built that)
docker run --rm -it ubuntu:sahil
The thing about this plain vanilla ubuntu environment is that it has the very basic stuff I want, git, node, nano, and zsh as my shell. It contains two users, root and devuser. When I start it up, I see the following prompt
While it’s pretty cool that I get a clean linux environment on-demand, I want to do some “work” and “save it as a snapshot”.
For instance, the work I wish to do here is, install zsh themes and change the theme to agnoster. Here is how I do it,

and then I edit my .zshrc and launch zsh again to test it.
Great my prompt looks like this now,

I quickly test it by creating a simple git repo,
And then lets assume I do some valuable work that I don’t want to loose! So, I snapshot it .. or .. in docker terms, commit it.
Here is how, I first check to see what images I have,
Okay wonderful! I capture this via a commit,
Now, whenever I wish to go right back to that snapshot, I run
docker run --rm -it ubuntu:sahil-zsh zsh
Check it out, I’m back on my snapshot —
Important note: Always treat your docker images/containers as something you can blow off easily and recreate from scratch. Don’t go around storing valuable work on the image itself, add “MOUNT” for instance to your docker image to force keep the actual work on your host machine!!
留言
張貼留言