How to use the command 'incus' (with examples)
Incus is a modern, secure, and powerful system container and virtual machine manager. It facilitates the creation, management, and removal of containers and virtual machines, providing a streamlined and efficient workflow for developers and systems administrators. The command offers a suite of capabilities, making it an essential tool for those looking to manage containerized and virtual machine environments effectively. Here’s a closer look at how you can use the ‘incus’ command with various practical examples.
Use case 1: Listing All Containers and Virtual Machines
Code:
incus list
Motivation: Listing all containers and virtual machines is a fundamental task that allows you to understand the current state of your system’s environment. This information is crucial for monitoring, maintenance, and planning purposes, as it provides a comprehensive overview of both running and stopped instances.
Explanation:
The command incus list
does not require any additional arguments and is straightforward in its function. It queries the Incus environment to retrieve and display a list of all containers and virtual machines, regardless of their current state. By default, this command will show various details such as their names, states, and resource usage.
Example Output:
+----------------+---------+-------------------+------+-----------+-----------+----------+
| NAME | STATE | IPV4 | TYPE | SNAPSHOTS | PROFILES | SIZE |
+----------------+---------+-------------------+------+-----------+-----------+----------+
| my-container-1 | STOPPED | | CONTAINER | 0 | default | |
| vm-instance-1 | RUNNING | 192.168.1.5 (eth0)| VIRTUAL MACHINE | 1 | vm-profile| 2GB |
+----------------+---------+-------------------+------+-----------+-----------+----------+
Use case 2: Creating a Container from an Image
Code:
incus create image container_name
Motivation: Creating a container from an image is an essential process in leveraging container technology. Containers provide isolated environments that can run software consistently across various systems. This command is particularly useful when you need to deploy applications quickly in a reproducible manner.
Explanation:
The command incus create
requires two arguments, image
and container_name
. Image
specifies the identifier of the base image from which the container is to be created, while container_name
is the custom name you assign to the new container to easily reference it within your Incus environment.
Example Output:
Container container_name has been created
Use case 3: Starting or Stopping an Existing Container
Code:
incus start|stop container_name
Motivation: Being able to start or stop a container is a crucial aspect of managing container lifecycles. Starting a container means running its application workloads, whereas stopping it safely halts its processes, freeing up system resources. This flexibility allows for optimized resource allocation tailored to specific needs at any given time.
Explanation:
This command accepts two main actions, start
or stop
, followed by the container_name
which specifies the exact container you wish to manage. The action is based on your operational needs: whether to initiate or terminate the container’s operations.
Example Output (for starting):
Container container_name is now running
Use case 4: Opening a Shell Inside an Already Running Container
Code:
incus shell container_name
Motivation: Opening a shell inside a container extends your ability to interact directly with the container’s environment. This operation is valuable for debugging, configuration, or even routine maintenance tasks, enabling administrators to perform operations just as they would on any Unix-like system.
Explanation:
The command incus shell
is followed by container_name
, indicating the running container you wish to access. It effectively launches an interactive shell session that connects directly to the container’s filesystem and processes.
Example Output:
root@container_name:/#
Use case 5: Removing a Stopped Container
Code:
incus delete container_name
Motivation: Cleaning up unused or unnecessary containers is important to maintain an efficient and resource-friendly system. Removing stopped containers helps free up disk space and minimizes clutter, promoting a cleaner management environment.
Explanation:
incus delete
requires the container_name
of the stopped container you plan on removing. Note that the container must be stopped before removal. This command effectively erases the container and all its associated data.
Example Output:
Container container_name successfully removed
Use case 6: Pulling an Image from an Image Repository
Code:
incus copy remote:image local:custom_image_name
Motivation: Pulling an image from a remote repository is essential when creating new containers or ensuring you have the latest updates from a maintained image. This practice keeps your environment current and leverages community or vendor-provided optimizations and configurations.
Explanation:
The command incus copy
is followed by the source image location remote:image
and the desired local image name local:custom_image_name
. The remote:image
specifies where to retrieve the image from, and the local:custom_image_name
allows you to give it an identifiable name for future use.
Example Output:
Image copied successfully to local:custom_image_name
Use case 7: Listing All Available Images in the Official Images Repository
Code:
incus image list images:
Motivation: Having visibility into available images is useful when planning new deployments. Knowing what base images you can pull from aids in selecting the appropriate environment for your software applications or development needs.
Explanation:
The command incus image list
with the argument images:
queries the official remote repository for a list of available images. This command effectively enables browsing of possible image templates you can employ.
Example Output:
+--------------+--------------+--------+-----------------+
| ALIAS | FINGERPRINT | PUBLIC | SIZE |
+--------------+--------------+--------+-----------------+
| ubuntu/20.04 | 123abc456def | YES | 196.23MB |
| alpine/3.13 | 789ghi012jkl | YES | 5.04MB |
+--------------+--------------+--------+-----------------+
Use case 8: Listing All Images Already Downloaded Locally
Code:
incus image list local:
Motivation: Listing locally downloaded images helps track your environment footprint and prepare for new container deployments. It ensures that you are aware of what you currently have available and assists in efficient resource management.
Explanation:
The command incus image list
followed by the argument local:
requests a list of images that have been downloaded to your local Incus environment. This allows you to inventory your existing resources quickly.
Example Output:
+--------------+--------------+--------+-----------------+
| ALIAS | FINGERPRINT | PUBLIC | SIZE |
+--------------+--------------+--------+-----------------+
| custom_ubuntu| 123abc456def | NO | 196.23MB |
| my_alpine | 789ghi012jkl | NO | 5.04MB |
+--------------+--------------+--------+-----------------+
Conclusion:
The ‘incus’ command is a versatile and robust tool for managing both containers and virtual machines. By mastering the examples discussed, you can streamline your workflows, optimize resource utilization, and maintain a more organized and effective virtual environment. Whether you are listing, creating, starting, stopping, or removing entities, Incus equips you with the necessary functionality to perform each task with ease.