List running containers

Hi!
Could you please finally add -r to list running only containers?
Or better -s(tate) r/s/f/… (running/stopped/freezed/…)?

I second that request.

Currently, lxc list supports filters on the configuration of a container.
If the container is called mycontainer, run lxc config show mycontainer to see the available keys in the config: section.
Therefore, the following will show which containers are based on Ubuntu 18.04.

lxc list image.version=18.04

However, is it possible to filter based on the output of lxc info mycontainer?
AFAIK, there is no built-in feature for this yet.
Having said that, you can get this result by parsing the JSON output.
Here is what is available to work with.

lxc list --format=json | jq .

To get the list of running container, use the following. It will show only the container name.

lxc list --format=json | jq -r '.[] | select(.state.status == "Running") | .name + ","'

For more info in the output, adapt like the following

$ lxc list --format=json | jq -r '.[] | select(.state.status == "Running") | .name + " | " + .architecture + " | " + .type + " | " + .state.status'
android | x86_64 | container | Running

I do lxc list | grep RUNNING | grep MOE

for example to do running containers on node MOE… But of course there should be a simpler way.

why cant it be lxc list -r -n MOE

I could also do it with aliases, but I should not have too.

SO I THIRD THAT REQUEST! — I mean it can’t be so hard…

For the record:
I opened an issue on github:

stgraber already anwered and approved it :slight_smile:.

Juste add these function to your .bashrc

function lxcr() { lxc list volatile.last_state.power=“RUNNING” -c n --format csv;}
function lxcs() { lxc list volatile.last_state.power=“STOPPED” -c n --format csv;}
function lxca() { lxc list -c n --format csv;}

I use something simple extensively in my scripts:
lxc ls -c ns --format=csv|grep RUNNING|cut -f1 -d,

Fast and works across different versions of lxd/lxc. It give you just the name of the container.

I agree it is ridiculous that I have grep all kinds of things to get the information that should be available via simple commands to lxc list on a command line.

I do it via a series of alias and scripts… all stuff that I should not have to do. It make LXC look like a half asssed programming.