Skip to main content

Container

Struct Container 

Source
pub struct Container {
Show 22 fields pub name: String, pub container_id: Option<String>, pub image: Option<String>, pub config: HashMap<String, String>, pub ports: Vec<String>, pub volumes: Vec<String>, pub env_vars: HashMap<String, String>, pub network: Option<String>, pub network_aliases: Vec<String>, pub cpu_limit: Option<String>, pub memory_limit: Option<String>, pub memory_swap_limit: Option<String>, pub cpu_shares: Option<String>, pub restart_policy: Option<String>, pub health_check: Option<HealthCheck>, pub detach: bool, pub snapshotter: Option<String>, pub runtime: Option<String>, pub disk_limit: Option<String>, pub annotations: HashMap<String, String>, pub privileged: bool, pub devices: Vec<String>,
}
Expand description

Container struct for nerdctl operations

Fields§

§name: String

Name of the container

§container_id: Option<String>

Container ID

§image: Option<String>

Base image (if created from an image)

§config: HashMap<String, String>

Configuration options

§ports: Vec<String>

Port mappings

§volumes: Vec<String>

Volume mounts

§env_vars: HashMap<String, String>

Environment variables

§network: Option<String>

Network to connect to

§network_aliases: Vec<String>

Network aliases

§cpu_limit: Option<String>

CPU limit

§memory_limit: Option<String>

Memory limit

§memory_swap_limit: Option<String>

Memory swap limit

§cpu_shares: Option<String>

CPU shares

§restart_policy: Option<String>

Restart policy

§health_check: Option<HealthCheck>

Health check

§detach: bool

Whether to run in detached mode

§snapshotter: Option<String>

Snapshotter to use

§runtime: Option<String>

Runtime to use (e.g., “kata-runtime”, “runc”)

§disk_limit: Option<String>

Disk size limit (for Kata containers, sets VM block device size)

§annotations: HashMap<String, String>

OCI annotations (key-value pairs passed to runtime)

§privileged: bool

Whether to run the container in privileged mode

§devices: Vec<String>

Device mounts (e.g., “/dev/net/tun”)

Implementations§

Source§

impl Container

Source

pub fn new(name: &str) -> Result<Self, NerdctlError>

Create a new container reference with the given name

§Arguments
  • name - Name for the container
§Returns
  • Result<Self, NerdctlError> - Container instance or error
Source

pub fn from_image(name: &str, image: &str) -> Result<Self, NerdctlError>

Create a container from an image

§Arguments
  • name - Name for the container
  • image - Image to create the container from
§Returns
  • Result<Self, NerdctlError> - Container instance or error
Source§

impl Container

Source

pub fn reset(self) -> Self

Reset the container configuration to defaults while keeping the name and image If the container exists, it will be stopped and removed.

§Returns
  • Self - The container instance for method chaining
Source

pub fn with_port(self, port: &str) -> Self

Add a port mapping

§Arguments
  • port - Port mapping (e.g., “8080:80”)
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_ports(self, ports: &[&str]) -> Self

Add multiple port mappings

§Arguments
  • ports - Array of port mappings (e.g., [“8080:80”, “8443:443”])
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_volume(self, volume: &str) -> Self

Add a volume mount

§Arguments
  • volume - Volume mount (e.g., “/host/path:/container/path”)
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_volumes(self, volumes: &[&str]) -> Self

Add multiple volume mounts

§Arguments
  • volumes - Array of volume mounts (e.g., [“/host/path1:/container/path1”, “/host/path2:/container/path2”])
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_env(self, key: &str, value: &str) -> Self

Add an environment variable

§Arguments
  • key - Environment variable name
  • value - Environment variable value
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_envs(self, env_map: &HashMap<&str, &str>) -> Self

Add multiple environment variables

§Arguments
  • env_map - Map of environment variable names to values
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_network(self, network: &str) -> Self

Set the network for the container

§Arguments
  • network - Network name
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_network_alias(self, alias: &str) -> Self

Add a network alias for the container

§Arguments
  • alias - Network alias
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_network_aliases(self, aliases: &[&str]) -> Self

Add multiple network aliases for the container

§Arguments
  • aliases - Array of network aliases
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_cpu_limit(self, cpus: &str) -> Self

Set CPU limit for the container

§Arguments
  • cpus - CPU limit (e.g., “0.5” for half a CPU, “2” for 2 CPUs)
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_memory_limit(self, memory: &str) -> Self

Set memory limit for the container

§Arguments
  • memory - Memory limit (e.g., “512m” for 512MB, “1g” for 1GB)
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_memory_swap_limit(self, memory_swap: &str) -> Self

Set memory swap limit for the container

§Arguments
  • memory_swap - Memory swap limit (e.g., “1g” for 1GB)
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_cpu_shares(self, shares: &str) -> Self

Set CPU shares for the container (relative weight)

§Arguments
  • shares - CPU shares (e.g., “1024” for default, “512” for half)
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_restart_policy(self, policy: &str) -> Self

Set restart policy for the container

§Arguments
  • policy - Restart policy (e.g., “no”, “always”, “on-failure”, “unless-stopped”)
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_health_check(self, cmd: &str) -> Self

Set a simple health check for the container

§Arguments
  • cmd - Command to run for health check (e.g., “curl -f http://localhost/ || exit 1”)
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_health_check_options( self, cmd: &str, interval: Option<&str>, timeout: Option<&str>, retries: Option<u32>, start_period: Option<&str>, ) -> Self

Set a health check with custom options for the container

§Arguments
  • cmd - Command to run for health check
  • interval - Optional time between running the check (e.g., “30s”, “1m”)
  • timeout - Optional maximum time to wait for a check to complete (e.g., “30s”, “1m”)
  • retries - Optional number of consecutive failures needed to consider unhealthy
  • start_period - Optional start period for the container to initialize before counting retries (e.g., “30s”, “1m”)
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_snapshotter(self, snapshotter: &str) -> Self

Set the snapshotter

§Arguments
  • snapshotter - Snapshotter to use
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_detach(self, detach: bool) -> Self

Set whether to run in detached mode

§Arguments
  • detach - Whether to run in detached mode
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_runtime(self, runtime: &str) -> Self

Set the runtime to use for the container

§Arguments
  • runtime - Runtime to use (e.g., “kata-runtime”, “runc”, “kata”)
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_disk_limit(self, size: &str) -> Self

Set disk size limit for the container

For Kata containers, this sets the VM block device size via OCI annotation. The size should be specified in a format like “10G”, “512M”, etc. The annotation will be automatically added when building if runtime is kata-related.

§Arguments
  • size - Disk size limit (e.g., “10G” for 10GB, “512M” for 512MB)
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_annotation(self, key: &str, value: &str) -> Self

Add an OCI annotation to the container

§Arguments
  • key - Annotation key
  • value - Annotation value
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_annotations(self, annotations_map: &HashMap<&str, &str>) -> Self

Add multiple OCI annotations to the container

§Arguments
  • annotations_map - Map of annotation keys to values
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_privileged(self, privileged: bool) -> Self

Set privileged mode for the container

§Arguments
  • privileged - Whether to run the container in privileged mode
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_device(self, device: &str) -> Self

Add a device to the container

§Arguments
  • device - Device path (e.g., “/dev/net/tun”)
§Returns
  • Self - The container instance for method chaining
Source

pub fn with_devices(self, devices: &[&str]) -> Self

Add multiple devices to the container

§Arguments
  • devices - Array of device paths (e.g., [“/dev/net/tun”, “/dev/kvm”])
§Returns
  • Self - The container instance for method chaining
Source

pub fn build(self) -> Result<Self, NerdctlError>

Build the container

§Returns
  • Result<Self, NerdctlError> - Container instance or error
Source§

impl Container

Source

pub fn start(&self) -> Result<CommandResult, NerdctlError>

Start the container and verify it’s running If the container hasn’t been created yet, it will be created automatically.

§Returns
  • Result<CommandResult, NerdctlError> - Command result or error with detailed information
Source

pub fn stop(&self) -> Result<CommandResult, NerdctlError>

Stop the container

§Returns
  • Result<CommandResult, NerdctlError> - Command result or error
Source

pub fn remove(&self) -> Result<CommandResult, NerdctlError>

Remove the container

§Returns
  • Result<CommandResult, NerdctlError> - Command result or error
Source

pub fn exec(&self, command: &str) -> Result<CommandResult, NerdctlError>

Execute a command in the container

§Arguments
  • command - The command to run
§Returns
  • Result<CommandResult, NerdctlError> - Command result or error
Source

pub fn copy( &self, source: &str, dest: &str, ) -> Result<CommandResult, NerdctlError>

Copy files between container and local filesystem

§Arguments
  • source - Source path (can be container:path or local path)
  • dest - Destination path (can be container:path or local path)
§Returns
  • Result<CommandResult, NerdctlError> - Command result or error
Source

pub fn export(&self, path: &str) -> Result<CommandResult, NerdctlError>

Export the container to a tarball

§Arguments
  • path - Path to save the tarball
§Returns
  • Result<CommandResult, NerdctlError> - Command result or error
Source

pub fn commit(&self, image_name: &str) -> Result<CommandResult, NerdctlError>

Commit the container to an image

§Arguments
  • image_name - Name for the new image
§Returns
  • Result<CommandResult, NerdctlError> - Command result or error
Source

pub fn status(&self) -> Result<ContainerStatus, NerdctlError>

Get container status

§Returns
  • Result<ContainerStatus, NerdctlError> - Container status or error
Source

pub fn health_status(&self) -> Result<String, NerdctlError>

Get the health status of the container

§Returns
  • Result<String, NerdctlError> - Health status or error
Source

pub fn logs(&self) -> Result<CommandResult, NerdctlError>

Get container logs

§Returns
  • Result<CommandResult, NerdctlError> - Command result or error
Source

pub fn resources(&self) -> Result<ResourceUsage, NerdctlError>

Get container resource usage

§Returns
  • Result<ResourceUsage, NerdctlError> - Resource usage or error

Trait Implementations§

Source§

impl Clone for Container

Source§

fn clone(&self) -> Container

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more