How to use the command 'lxc' (with examples)
- Linux
- December 17, 2024
The lxc
command is a powerful tool used to manage Linux containers via the LXD REST API. It is designed to simplify the process of creating, starting, stopping, and managing containers and images. With lxc
, administrators can efficiently handle containers either locally or remotely by specifying the remote server name. This flexibility makes it a crucial utility for anyone working in containerized environments. More details can be found at manned.org/lxc
.
Use case 1: List local containers matching a string
Code:
lxc list match_string
Motivation:
Listing containers is a fundamental operation when managing containerized environments as it allows administrators to track and manage active containers efficiently. Using a match string enables users to narrow down results, helping in scenarios where there are numerous containers, and one needs to locate specific ones based on partial names or identifiers.
Explanation:
lxc list
: This base command lists all containers.match_string
: This optional argument filters the list of containers to those whose names contain the specified string. If omitted, all local containers will be listed.
Example Output:
+--------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+--------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| my-container | RUNNING | 192.168.1.2 (eth0) | fd42:ac82:7df:4867:216:3eff:feaa:29d6 (eth0) | CONTAINER | 0 |
+--------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
Use case 2: List images matching a string
Code:
lxc image list [remote:]match_string
Motivation:
Listing images with a specific match string is essential when managing multiple images across different projects or deployments. It helps in quickly identifying the exact version or type of image needed for creating new containers, especially in environments with extensive image repositories.
Explanation:
lxc image list
: This command lists available images.[remote:]
: An optional prefix that, if specified, lists images from the remote server rather than locally.match_string
: This filters images by name or description, allowing users to locate specific images among many. It can be omitted to list all images.
Example Output:
+-------------------+-------------+--------+-----------------------------------------------+--------+--------+-------------------------------+
| ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE |
+-------------------+-------------+--------+-----------------------------------------------+--------+--------+-------------------------------+
| ubuntu/20.04 | 5c8f31b989a3| no | Ubuntu 20.04 LTS amd64 (20231006_07:42) | x86_64 | 215MB | Oct 6, 2023, 07:42 |
+-------------------+-------------+--------+-----------------------------------------------+--------+--------+-------------------------------+
Use case 3: Create a new container from an image
Code:
lxc init [remote:]image container
Motivation:
Creating a new container from an image is a frequent task while deploying applications in environments such as development, testing, or production. It allows for consistent and rapid setup of environments to match specific configurations or deployments.
Explanation:
lxc init
: This command initializes a new container but does not start it.[remote:]
: This optional parameter is used to specify the remote server from which the image should be sourced.image
: This parameter specifies the image to be used for creating the container. It can be a local or a remote image if prefixed.container
: This is the name to be assigned to the newly created container.
Example Output:
Creating ubuntu20-container
Use case 4: Start a container
Code:
lxc start [remote:]container
Motivation:
Starting a container is crucial for setting up an operational environment from which applications or services can be run. This command is particularly useful after initializing a container or when resuming operation after conducting maintenance tasks.
Explanation:
lxc start
: This command starts an existing container.[remote:]
: This optional prefix allows starting a container on a specified remote server.container
: The name of the container to be started.
Example Output:
The container 'my-container' has been started.
Use case 5: Stop a container
Code:
lxc stop [remote:]container
Motivation:
Stopping a container is necessary during maintenance, updates, or when it’s no longer needed. This practice helps to reduce resource consumption and ensure that unnecessary services are not running.
Explanation:
lxc stop
: This stops a running container.[remote:]
: Optional prefix to indicate stopping a container on a different server.container
: The specific container to stop.
Example Output:
The container 'my-container' has been stopped.
Use case 6: Show detailed info about a container
Code:
lxc info [remote:]container
Motivation:
Obtaining detailed information about a container is essential for troubleshooting, auditing, or gaining insights into the container’s configuration and status. It provides crucial details about the container’s network, storage, and operational state.
Explanation:
lxc info
: This command retrieves detailed information of the specified container.[remote:]
: Optional suffix to specify the server where the container resides.container
: The container from which information is to be gathered.
Example Output:
Name: my-container
Status: RUNNING
IPV4: 192.168.1.2
Network usage: ...
Memory usage: ...
Use case 7: Take a snapshot of a container
Code:
lxc snapshot [remote:]container snapshot
Motivation:
Taking snapshots of containers is a critical part of versioning, backup, and rollback strategies. It ensures that administrators can restore containers to a previous state in case of errors or failures.
Explanation:
lxc snapshot
: This command creates a snapshot of a container.[remote:]
: Optional prefix that specifies if the container is on a remote server.container
: The name of the container to snapshot.snapshot
: The name to be given to the snapshot.
Example Output:
Snapshot 'snapshot1' of container 'my-container' created successfully.
Use case 8: Execute a specific command inside a container
Code:
lxc exec [remote:]container command
Motivation:
Executing commands inside a container is crucial for maintenance, configuration, or direct management. It allows administrators and developers to interact with and manipulate the container environment as if they were operating directly within it.
Explanation:
lxc exec
: This commands a process to execute inside a container.[remote:]
: Optional prefix for specifying if the action is to be performed on a remote container.container
: The target container for the command.command
: The command to run inside the selected container.
Example Output:
root@my-container:/# echo "Hello, Container!"
Hello, Container!
Conclusion:
The lxc
command provides a comprehensive suite of tools for container management, offering flexibility and control over your containerized environments. From listing and creating containers to capturing snapshots and executing commands within containers, lxc
supports a wide array of functions essential for effective container orchestration and administration.