How to use the command lxc (with examples)

How to use the command lxc (with examples)

The lxc command is used to manage Linux containers using the lxd REST API. It allows you to perform various operations on containers, such as listing containers, creating new containers, starting and stopping containers, taking snapshots, and executing commands inside a container.

Use case 1: List local containers matching a string

Code:

lxc list match_string

Motivation: This use case is helpful when you want to list only the local containers that match a specific string. It allows you to filter out containers based on a given pattern.

Explanation:

  • lxc list: This command is used to list containers.
  • match_string: It is an optional argument, where you can specify a string to match against container names. If provided, only the containers whose names contain the specified string will be listed. If not provided, all local containers will be listed.

Example output:

+-------------+---------+----------------------+---------------------------------------+
|    NAME     |  STATE  |         IPV4         |                  IPV6                 |
+-------------+---------+----------------------+---------------------------------------+
| my-container| RUNNING | 10.0.3.5 (eth0)     | 2001:db8:42:abcd::1 (eth0)            |
+-------------+---------+----------------------+---------------------------------------+

Use case 2: List images matching a string

Code:

lxc image list [remote:]match_string

Motivation: This use case allows you to list only the images that match a specific string. It can be useful when you want to filter images based on a certain criteria.

Explanation:

  • lxc image list: This command is used to list images.
  • [remote:]match_string: It is an optional argument, where you can specify a string to match against image names. If provided, only the images whose names contain the specified string will be listed. If [remote:] is specified, the command will list the matching images on the remote server. If match_string is not provided, all images will be listed.

Example output:

+------------+--------------+--------+---------------------------------+
|   ALIAS    | FINGERPRINT  | PUBLIC |           DESCRIPTION           |
+------------+--------------+--------+---------------------------------+
| my-image   | 5e2e8e9546fd | no     | Ubuntu 20.04 LTS amd64 (release)|
+------------+--------------+--------+---------------------------------+

Use case 3: Create a new container from an image

Code:

lxc init [remote:]image container

Motivation: This use case is used to create a new container from an existing image. It allows you to instantiate containers based on pre-built images, reducing the setup time for new containers.

Explanation:

  • lxc init: This command is used to initialize a new container from an image.
  • [remote:]image: It specifies the image from which the container will be created. If [remote:] is specified, the image will be fetched from the remote server.
  • container: It is the name of the new container that will be created.

Example output:

Creating my-container

Use case 4: Start a container

Code:

lxc start [remote:]container

Motivation: This use case allows you to start a stopped container. Starting a container enables all the processes within the container to run and be accessible.

Explanation:

  • lxc start: This command is used to start a container.
  • [remote:]container: It specifies the container that will be started. If [remote:] is specified, the container will be started on the remote server.

Example output:

my-container started

Use case 5: Stop a container

Code:

lxc stop [remote:]container

Motivation: This use case allows you to stop a running container. Stopping a container halts all the processes running within the container.

Explanation:

  • lxc stop: This command is used to stop a running container.
  • [remote:]container: It specifies the container that will be stopped. If [remote:] is specified, the container will be stopped on the remote server.

Example output:

my-container stopped

Use case 6: Show detailed info about a container

Code:

lxc info [remote:]container

Motivation: This use case provides detailed information about a specific container. It can be useful to check the state, network configuration, and resource usage of a container.

Explanation:

  • lxc info: This command is used to display detailed information about a container.
  • [remote:]container: It specifies the container for which the information will be displayed. If [remote:] is specified, the container will be queried on the remote server.

Example output:

Name: my-container
State: RUNNING
PID: 12345
IPs:
  eth0: 10.0.3.5
  eth1: 192.168.1.2

Use case 7: Take a snapshot of a container

Code:

lxc snapshot [remote:]container snapshot

Motivation: This use case allows you to take a snapshot of a container’s current state. Snapshots can be useful for backup purposes or for creating checkpoints before making changes to the container.

Explanation:

  • lxc snapshot: This command is used to take a snapshot of a container.
  • [remote:]container: It specifies the container for which the snapshot will be taken. If [remote:] is specified, the container will be accessed on the remote server.
  • snapshot: It is the name of the snapshot that will be created.

Example output:

Snapshot "my-snapshot" created

Use case 8: Execute a specific command inside a container

Code:

lxc exec [remote:]container command

Motivation: This use case allows you to execute a specific command inside a running container. It can be useful to perform tasks or run scripts within the container.

Explanation:

  • lxc exec: This command is used to execute a command inside a container.
  • [remote:]container: It specifies the container in which the command will be executed. If [remote:] is specified, the container will be accessed on the remote server.
  • command: It is the command that will be executed inside the container.

Example output:

Command output

Conclusion:

The lxc command is a versatile tool for managing Linux containers using the lxd REST API. With its various use cases, you can easily perform operations on containers, such as listing, creating, starting, stopping, and executing commands within them. By understanding each use case and its corresponding code, motivation, explanation, and example output, you can leverage the lxc command to manage your Linux containers efficiently.

Tags :

Related Posts

How to use the command 'aiac' (with examples)

How to use the command 'aiac' (with examples)

The ‘aiac’ command is a tool that leverages OpenAI to generate Infrastructure as Code (IaC) configurations, utilities, queries, and more.

Read More
How to use the command 'calligraflow' (with examples)

How to use the command 'calligraflow' (with examples)

Calligraflow is an application created by Calligra, which specializes in flowcharts and diagrams.

Read More
How to use the command 'repo-add' (with examples)

How to use the command 'repo-add' (with examples)

The ‘repo-add’ command is a package database maintenance utility that enables the installation of packages via Pacman in Arch Linux.

Read More