Skip to content

Instantly share code, notes, and snippets.

@kaxing
Last active September 19, 2018 20:56
Show Gist options
  • Save kaxing/1f6f272138a6458686e49e2c39fcaa8b to your computer and use it in GitHub Desktop.
Save kaxing/1f6f272138a6458686e49e2c39fcaa8b to your computer and use it in GitHub Desktop.
Automatically launch and exec new lxc container when opening new Terminal tab

Automatically launch and exec new lxc container when opening new Terminal tab

If you happen to throw most things into containers, I think this a handy trick for lazy people like us.

* It is somehow an idea inspiring from Firefox TabContainer

Here is the trick, so Gnome Terminal supports different profile and most terminal apps maybe, too. Inside profile you can execute a different program, this is what I did:

sh -c 'NEW_NAME=AG-$(shuf -n1 /usr/share/dict/words|tr -d [:punct:] ) && lxc launch images:ubuntu/18.04 $NEW_NAME && lxc exec $NEW_NAME bash'
image
Under menu Edit > Profile tab > edit the profile > Command tab > “Run a custom command instead of my shell”

Open new container through profile, mine is named “New Container”, then in the same Gnome Terminal Window, you could simply open another container by opening new tabs: image

To keep the environment and stuff after whatever stuff your tried and built with containers, simply copy that container, using command outside containers:

$ lxc copy AG-random-name a-new-name

At the end of the day, to clean up all auto-generated containers, just do following:

$ lxc list AG --format=csv|cut -d, -f1|xargs -I% sh -c 'lxc stop % && lxc delete %'

Put this into shell alias or a script under ~/bin as you wished.

To config lxd/lxc please check this: https://tutorials.ubuntu.com/tutorial/tutorial-setting-up-lxd-1604

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment