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/…)?

1 Like

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
1 Like

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.

1 Like

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.

Add the follow shell function to your profile, to decorate the lxc command.

lxc() {
    command lxc $(echo "$@" | sed -E '/\blist\b|\bls\b/{
        s/--running/volatile.last_state.power=RUNNING/
        s/--stopped/volatile.last_state.power=STOPPED/
    }')
}

Then, use

lxc [list|ls] [--running|--stopped]

It won’t interfere with other arguments or options.

lxc list status=running
lxc list status=stopped

Aren’t that much longer to type :slight_smile:

I didn’t know that was an option. lol

I thought you had to type

lxc ls volatile.last_state.power=RUNNING

When I type

I don’t see my running containers.?

What LXD version are you using?

4.0.7

I thought I was using the snap but just noticed which lxc is /usr/bin/lxc!

I have about fifty Ubuntu 20.04 LTS VMs with lxd but they all seem to have different versions; most are the snap but some are the native packages. New VMs seem to have the snap installed by default but it seems older 20.04 LTS VMs had the native package by default. I think I used snap install on a few but forgot on others. What is the best practice to make these all uniform?

Thanks.

Ubuntu 20.04 only has LXD as the snap. The machines reporting /usr/bin/lxc must just have the transitional lxd deb installed. That packages migrates systems over to the snap upon upgrading to 20.04 and provides a small wrapper for /usr/bin/lxc and /usr/bin/lxd.

You can confirm that with cat /usr/bin/lxc. If it’s just a 10 lines long shell script, then that’s the wrapper.

One such systems, the lxd, lxd-client and lxd-tools packages if installed should all have a description saying Transitional package - ....

Having those packages installed is harmless but you can get rid of them with apt remove --purge lxd lxd-client lxd-tools which will then have which lxc show /snap/bin/lxc as it does on newly installed systems.

As for the new filtering feature (status=running), this is a feature which was introduced after the 4.0 LTS release and so is missing in 4.0.x.

If this is a production environment, you’re probably best to stay on 4.0.x as that’s our LTS release. It comes with the downside of not getting monthly new features, but for production environments, that tends to be a good thing :slight_smile:

Though that means you’ll need to keep using something like that volatile.last_state trick until LXD 5.0 LTS gets released in March/April next year.

Now if you don’t mind getting a new LXD release every month, you can do snap refresh lxd --channel=latest which will move you over to the LXD 4.16 release that comes with this particular new features (was introduce around 4.13/4.14 I believe).

Thank-you, I really appreciate your help.

Just to clarify for everyone reading this, the Pull request was merged a while ago, so you can now list running containers and much more, more easily.

Type lxc list --help for details.

To shorten the commands even more, take a look at Advanced Guide - Command aliases.