Skip to main content

libcontainer/container/
container_start.rs

1use super::{Container, ContainerStatus};
2use crate::config::YoukiConfig;
3use crate::error::LibcontainerError;
4use crate::hooks;
5use crate::notify_socket::{NOTIFY_FILE, NotifySocket};
6
7impl Container {
8    /// Starts a previously created container
9    ///
10    /// # Example
11    ///
12    /// ```no_run
13    /// use libcontainer::container::builder::ContainerBuilder;
14    /// use libcontainer::syscall::syscall::SyscallType;
15    ///
16    /// # fn main() -> anyhow::Result<()> {
17    /// let mut container = ContainerBuilder::new(
18    ///     "74f1a4cb3801".to_owned(),
19    ///     SyscallType::default(),
20    /// )
21    /// .as_init("/var/run/docker/bundle")
22    /// .build()?;
23    ///
24    /// container.start();
25    /// # Ok(())
26    /// # }
27    /// ```
28    pub fn start(&mut self) -> Result<(), LibcontainerError> {
29        self.refresh_status()?;
30
31        if !self.can_start() {
32            tracing::error!(status = ?self.status(), id = ?self.id(), "cannot start container due to incorrect state");
33            return Err(LibcontainerError::IncorrectStatus(self.status()));
34        }
35
36        let config = YoukiConfig::load(&self.root).map_err(|err| {
37            tracing::error!(
38                "failed to load runtime spec for container {}: {}",
39                self.id(),
40                err
41            );
42            err
43        })?;
44
45        let mut notify_socket = NotifySocket::new(self.root.join(NOTIFY_FILE));
46        notify_socket.notify_container_start()?;
47        self.set_status(ContainerStatus::Running)
48            .save()
49            .map_err(|err| {
50                tracing::error!(id = ?self.id(), ?err, "failed to save state for container");
51                err
52            })?;
53
54        // Run post start hooks. It runs after the container process is started.
55        // It is called in the runtime namespace.
56        if let Some(hooks) = config.hooks.as_ref() {
57            hooks::run_hooks(
58                hooks.poststart().as_ref(),
59                Some(&self.state),
60                Some(&self.root),
61                None,
62            )
63            .map_err(|err| {
64                tracing::error!("failed to run post start hooks: {}", err);
65                err
66            })?;
67        }
68
69        Ok(())
70    }
71}