gadget_sdk/docker.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
//! Utilities for spinning up and managing Docker containers
//!
//! This module provides wrappers around [`bollard`] to simplify Docker interactions within blueprints.
pub use bollard;
use bollard::container::{
Config, CreateContainerOptions, InspectContainerOptions, ListContainersOptions,
StartContainerOptions, StopContainerOptions, WaitContainerOptions,
};
use bollard::models::{
ContainerConfig, ContainerCreateResponse, ContainerInspectResponse, HostConfig,
MountPointTypeEnum,
};
use bollard::{Docker, API_DEFAULT_VERSION};
use core::str::FromStr;
use std::collections::HashMap;
use std::sync::Arc;
use subxt::ext::futures::{Stream, StreamExt};
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Attempted to connect to a non-existent container")]
ContainerNotFound,
#[error("Found an invalid status for the container: `{0}`")]
BadContainerStatus(String),
#[error("{0}")]
Bollard(#[from] bollard::errors::Error),
}
/// The status of a Docker container
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ContainerStatus {
/// Created, but never started
Created,
/// Actively running
Running,
/// Paused via `docker pause`
Paused,
/// Restarting according to the restart policy
Restarting,
/// Container was started, and is no longer running
Exited,
/// In the process of being removed
Removing,
/// Defunct, partially removed
Dead,
}
impl FromStr for ContainerStatus {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"created" => Ok(ContainerStatus::Created),
"running" => Ok(ContainerStatus::Running),
"paused" => Ok(ContainerStatus::Paused),
"restarting" => Ok(ContainerStatus::Restarting),
"exited" => Ok(ContainerStatus::Exited),
"removing" => Ok(ContainerStatus::Removing),
"dead" => Ok(ContainerStatus::Dead),
_ => Err(Error::BadContainerStatus(s.to_string())),
}
}
}
impl ContainerStatus {
pub fn is_active(self) -> bool {
matches!(self, ContainerStatus::Running)
}
pub fn is_usable(self) -> bool {
!matches!(self, ContainerStatus::Removing | ContainerStatus::Dead)
}
}
/// A [Docker](https://en.wikipedia.org/wiki/Docker_(software)) container
#[derive(Debug)]
pub struct Container<'a> {
id: Option<String>,
image: String,
connection: &'a Docker,
options: ContainerOptions,
}
#[derive(Debug, Default, Clone)]
struct ContainerOptions {
env: Option<Vec<String>>,
cmd: Option<Vec<String>>,
binds: Option<Vec<String>>,
}
impl<'a> Container<'a> {
/// Create a new `Container`
///
/// # Examples
///
/// ```rust,no_run
/// use gadget_sdk::docker::{connect_to_docker, Container};
///
/// #[tokio::main]
/// async fn main() -> Result<(), gadget_sdk::Error> {
/// let connection = connect_to_docker(None).await?;
/// let mut container = Container::new(&connection, "rustlang/rust");
///
/// // We can now start our container
/// container.start(true).await?;
/// Ok(())
/// }
/// ```
pub fn new<T>(connection: &'a Docker, image: T) -> Self
where
T: Into<String>,
{
Self {
id: None,
image: image.into(),
connection,
options: ContainerOptions::default(),
}
}
/// Attempt to fetch an existing container by its ID
///
/// # Errors
///
/// * Docker inspect fails
/// * The container isn't found
///
/// # Examples
///
/// ```rust,no_run
/// use gadget_sdk::docker::{connect_to_docker, Container};
///
/// #[tokio::main]
/// async fn main() -> Result<(), gadget_sdk::Error> {
/// let connection = connect_to_docker(None).await?;
/// let mut container = Container::new(&connection, "rustlang/rust");
///
/// // We can now start our container and grab its id
/// container.start(false).await?;
///
/// let id = container.id().unwrap();
///
/// let container2 = Container::from_id(&connection, id).await?;
///
/// assert_eq!(container.id(), container2.id());
/// Ok(())
/// }
/// ```
pub async fn from_id<T>(connection: &'a Docker, id: T) -> Result<Self, Error>
where
T: AsRef<str>,
{
let inspection = connection
.inspect_container(id.as_ref(), None::<InspectContainerOptions>)
.await?;
let ContainerInspectResponse {
id: Some(id),
config:
Some(ContainerConfig {
env,
cmd,
image: Some(image),
..
}),
mounts,
..
} = inspection
else {
return Err(Error::ContainerNotFound);
};
let mut binds = None;
if let Some(mounts) = mounts {
let mut bind_mounds = Vec::new();
for mount in mounts {
if !matches!(mount.typ, Some(MountPointTypeEnum::BIND)) {
continue;
}
let mut bind = String::new();
if let Some(source) = mount.source {
bind.push_str(&source);
}
let Some(dest) = mount.destination else {
continue;
};
bind.push(':');
bind.push_str(&dest);
if let Some(mode) = mount.mode {
bind.push(':');
bind.push_str(&mode);
}
bind_mounds.push(bind);
}
binds = Some(bind_mounds);
}
let options = ContainerOptions { env, cmd, binds };
Ok(Self {
id: Some(id),
image,
connection,
options,
})
}
/// Set the environment variables for the container
///
/// NOTE: This will override any existing variables.
///
/// # Examples
///
/// ```rust,no_run
/// use gadget_sdk::docker::{connect_to_docker, Container};
///
/// #[tokio::main]
/// async fn main() -> Result<(), gadget_sdk::Error> {
/// let connection = connect_to_docker(None).await?;
/// let mut container = Container::new(&connection, "rustlang/rust");
///
/// container.env(["FOO=BAR", "BAZ=QUX"]);
///
/// // We can now start our container, and the "FOO" and "BAZ" env vars will be set
/// container.start(true).await?;
/// Ok(())
/// }
/// ```
pub fn env(&mut self, env: impl IntoIterator<Item = impl Into<String>>) -> &mut Self {
self.options.env = Some(env.into_iter().map(Into::into).collect());
self
}
/// Set the command to run
///
/// The command is provided as a list of strings.
///
/// NOTE: This will override any existing command
///
/// # Examples
///
/// ```rust,no_run
/// use gadget_sdk::docker::{connect_to_docker, Container};
///
/// #[tokio::main]
/// async fn main() -> Result<(), gadget_sdk::Error> {
/// let connection = connect_to_docker(None).await?;
/// let mut container = Container::new(&connection, "rustlang/rust");
///
/// container.cmd(["echo", "Hello!"]);
///
/// // We can now start our container, and the command "echo Hello!" will run
/// container.start(true).await?;
/// Ok(())
/// }
/// ```
pub fn cmd(&mut self, cmd: impl IntoIterator<Item = impl Into<String>>) -> &mut Self {
self.options.cmd = Some(cmd.into_iter().map(Into::into).collect());
self
}
/// Set a list of volume binds
///
/// These binds are in the standard `host:dest[:options]` format. For more information, see
/// the [Docker documentation](https://docs.docker.com/engine/storage/bind-mounts/).
///
/// # Examples
///
/// ```rust,no_run
/// use gadget_sdk::docker::{connect_to_docker, Container};
///
/// #[tokio::main]
/// async fn main() -> Result<(), gadget_sdk::Error> {
/// let connection = connect_to_docker(None).await?;
/// let mut container = Container::new(&connection, "rustlang/rust");
///
/// // Mount './my-host-dir' at '/some/container/dir' and make it read-only
/// container.binds(["./my-host-dir:/some/container/dir:ro"]);
///
/// // We can now start our container
/// container.start(true).await?;
/// Ok(())
/// }
/// ```
pub fn binds(&mut self, binds: impl IntoIterator<Item = impl Into<String>>) -> &mut Self {
self.options.binds = Some(binds.into_iter().map(Into::into).collect());
self
}
/// Get the container ID if it has been created
///
/// This will only have a value if [`Container::create`] or [`Container::start`] has been
/// called prior.
pub fn id(&self) -> Option<&str> {
self.id.as_deref()
}
/// Attempt to create the container
///
/// This will take the following into account:
///
/// * [`Container::env`]
/// * [`Container::cmd`]
/// * [`Container::binds`]
///
/// Be sure to set these before calling this!
///
/// # Examples
///
/// ```rust,no_run
/// use gadget_sdk::docker::{connect_to_docker, Container};
///
/// #[tokio::main]
/// async fn main() -> Result<(), gadget_sdk::Error> {
/// let connection = connect_to_docker(None).await?;
/// let mut container = Container::new(&connection, "rustlang/rust");
///
/// container.env(["FOO=BAR", "BAZ=QUX"]);
/// container.cmd(["echo", "Hello!"]);
/// container.binds(["./host-data:/container-data"]);
///
/// // The container is created using the above settings
/// container.create().await?;
///
/// // Now it can be started
/// container.start(true).await?;
/// Ok(())
/// }
/// ```
#[tracing::instrument]
pub async fn create(&mut self) -> Result<(), bollard::errors::Error> {
crate::debug!("Creating container");
let config = Config {
image: Some(self.image.clone()),
cmd: self.options.cmd.clone(),
env: self.options.env.clone(),
attach_stdout: Some(true),
host_config: Some(HostConfig {
binds: self.options.binds.clone(),
..Default::default()
}),
..Default::default()
};
let ContainerCreateResponse { id, warnings } = self
.connection
.create_container(None::<CreateContainerOptions<String>>, config)
.await?;
for warning in warnings {
crate::warn!("{}", warning);
}
self.id = Some(id);
Ok(())
}
/// Attempt to start the container
///
/// NOTE: If the container has not yet been created, this will attempt to call [`Container::create`] first.
///
/// `wait_for_exit` will wait for the container to exit before returning.
///
/// # Examples
///
/// ```rust,no_run
/// use gadget_sdk::docker::{connect_to_docker, Container};
///
/// #[tokio::main]
/// async fn main() -> Result<(), gadget_sdk::Error> {
/// let connection = connect_to_docker(None).await?;
/// let mut container = Container::new(&connection, "rustlang/rust");
///
/// container.cmd(["echo", "Hello!"]);
///
/// // We can now start our container, and the command "echo Hello!" will run.
/// let wait_for_exit = true;
/// container.start(wait_for_exit).await?;
///
/// // Since we waited for the container to exit, we don't have to stop it.
/// // It can now just be removed.
/// container.remove(None).await?;
/// Ok(())
/// }
/// ```
#[tracing::instrument]
pub async fn start(&mut self, wait_for_exit: bool) -> Result<(), bollard::errors::Error> {
if self.id.is_none() {
self.create().await?;
}
crate::debug!("Starting container");
let id = self.id.as_ref().unwrap();
self.connection
.start_container(id, None::<StartContainerOptions<String>>)
.await?;
if wait_for_exit {
self.wait().await?;
}
Ok(())
}
/// Checks if the container has not exited and is marked as `healthy`
///
/// NOTE: If the container has not yet been created, this will immediately return `None`.
///
/// # Examples
///
/// ```rust,no_run
/// use gadget_sdk::docker::{connect_to_docker, Container};
/// use std::time::Duration;
/// use tokio::time;
///
/// #[tokio::main]
/// async fn main() -> Result<(), gadget_sdk::Error> {
/// let connection = connect_to_docker(None).await?;
/// let mut container = Container::new(&connection, "rustlang/rust");
///
/// container.cmd(["echo", "Hello!"]);
///
/// let wait_for_exit = false;
/// container.start(wait_for_exit).await?;
///
/// loop {
/// let status = container.status().await?.unwrap();
/// if status.is_active() {
/// time::sleep(Duration::from_secs(5)).await;
/// continue;
/// }
///
/// println!("Container exited!");
/// break;
/// }
///
/// Ok(())
/// }
/// ```
pub async fn status(&self) -> Result<Option<ContainerStatus>, Error> {
if self.id.is_none() {
return Ok(None);
}
let mut filters = HashMap::new();
let _ = filters.insert("id", vec![self.id.as_deref().unwrap()]);
let options = Some(ListContainersOptions {
all: true,
filters,
..Default::default()
});
let containers = self.connection.list_containers(options).await?;
let Some(status) = &containers[0].state else {
return Ok(None);
};
ContainerStatus::from_str(status.as_str()).map(Some)
}
/// Stop a running container
///
/// NOTE: It is not an error to call this on a container that has not been started,
/// it will simply do nothing.
///
/// # Examples
///
/// ```rust,no_run
/// use gadget_sdk::docker::{connect_to_docker, Container};
///
/// #[tokio::main]
/// async fn main() -> Result<(), gadget_sdk::Error> {
/// let connection = connect_to_docker(None).await?;
///
/// let mut container = Container::new(&connection, "rustlang/rust");
///
/// // Does nothing, the container isn't started
/// container.stop().await?;
///
/// // Stops the running container
/// container.start(false).await?;
/// container.stop().await?;
/// Ok(())
/// }
/// ```
#[tracing::instrument]
pub async fn stop(&mut self) -> Result<(), bollard::errors::Error> {
let Some(id) = &self.id else {
crate::warn!("Container not started");
return Ok(());
};
self.connection
.stop_container(id, None::<StopContainerOptions>)
.await?;
Ok(())
}
/// Remove a container
///
/// NOTE: To remove a running container, a [`RemoveContainerOptions`] must be provided
/// with the `force` flag set.
///
/// See also: [`bollard::container::RemoveContainerOptions`]
///
/// # Examples
///
/// ```rust,no_run
/// use gadget_sdk::docker::{bollard, connect_to_docker, Container};
///
/// #[tokio::main]
/// async fn main() -> Result<(), gadget_sdk::Error> {
/// let connection = connect_to_docker(None).await?;
///
/// let mut container = Container::new(&connection, "rustlang/rust");
///
/// // Start our container
/// container.start(false).await?;
///
/// let remove_container_options = bollard::container::RemoveContainerOptions {
/// force: true,
/// ..Default::default()
/// };
///
/// // Kills the container and removes it
/// container.remove(Some(remove_container_options)).await?;
/// Ok(())
/// }
/// ```
///
/// [`RemoveContainerOptions::force`]: bollard::container::RemoveContainerOptions::force
#[tracing::instrument]
pub async fn remove(
mut self,
options: Option<bollard::container::RemoveContainerOptions>,
) -> Result<(), bollard::errors::Error> {
let Some(id) = self.id.take() else {
crate::warn!("Container not started");
return Ok(());
};
self.connection.remove_container(&id, options).await?;
Ok(())
}
/// Wait for a container to exit
///
/// NOTE: It is not an error to call this on a container that has not been started,
/// it will simply do nothing.
///
/// # Examples
///
/// ```rust,no_run
/// use gadget_sdk::docker::{bollard, connect_to_docker, Container};
///
/// #[tokio::main]
/// async fn main() -> Result<(), gadget_sdk::Error> {
/// let connection = connect_to_docker(None).await?;
///
/// let mut container = Container::new(&connection, "rustlang/rust");
///
/// // Start our container
/// container.start(false).await?;
///
/// // Once this returns, we know that the container has exited.
/// container.wait().await?;
/// Ok(())
/// }
/// ```
#[tracing::instrument]
pub async fn wait(&self) -> Result<(), bollard::errors::Error> {
let Some(id) = &self.id else {
crate::warn!("Container not created");
return Ok(());
};
wait_for_container(self.connection, id).await?;
Ok(())
}
/// Fetch the container log stream
///
/// NOTE: It is not an error to call this on a container that has not been started,
/// it will simply do nothing and return `None`.
///
/// See also:
///
/// * [`bollard::container::LogsOptions`]
/// * [`bollard::container::LogOutput`]
///
/// # Examples
///
/// ```rust,no_run
/// use futures::StreamExt;
/// use gadget_sdk::docker::{bollard, connect_to_docker, Container};
///
/// #[tokio::main]
/// async fn main() -> Result<(), gadget_sdk::Error> {
/// let connection = connect_to_docker(None).await?;
/// let mut container = Container::new(&connection, "rustlang/rust");
///
/// // Start our container and wait for it to exit
/// container.start(true).await?;
///
/// // We want to collect logs from stderr
/// let logs_options = bollard::container::LogsOptions {
/// stderr: true,
/// follow: true,
/// ..Default::default()
/// };
///
/// // Get our log stream
/// let mut logs = container
/// .logs(Some(logs_options))
/// .await
/// .expect("logs should be present");
///
/// // Now we want to print anything from stderr
/// while let Some(Ok(out)) = logs.next().await {
/// if let bollard::container::LogOutput::StdErr { message } = out {
/// eprintln!("Uh oh! Something was written to stderr: {:?}", message);
/// }
/// }
/// Ok(())
/// }
/// ```
#[tracing::instrument]
pub async fn logs(
&self,
logs_options: Option<bollard::container::LogsOptions<String>>,
) -> Option<impl Stream<Item = Result<bollard::container::LogOutput, bollard::errors::Error>>>
{
let Some(id) = &self.id else {
crate::warn!("Container not created");
return None;
};
Some(self.connection.logs(id, logs_options))
}
}
/// Connect to a local [Docker] socket
///
/// NOTE: If `socket` is `None`, this will attempt to connect to `/var/run/docker.sock`.
///
/// # Examples
///
/// ```rust,no_run
/// use gadget_sdk::docker::connect_to_docker;
///
/// #[tokio::main]
/// async fn main() -> Result<(), gadget_sdk::Error> {
/// let connection = connect_to_docker(None).await?;
///
/// // I now have a handle to my local Docker server!
/// Ok(())
/// }
/// ```
///
/// [Docker]: https://en.wikipedia.org/wiki/Docker_(software)
pub async fn connect_to_docker(
socket: Option<&str>,
) -> Result<Arc<Docker>, bollard::errors::Error> {
crate::info!("Connecting to local docker server...");
let docker = Docker::connect_with_socket(
socket.unwrap_or("/var/run/docker.sock"),
120,
API_DEFAULT_VERSION,
)?;
if let Err(e) = docker.ping().await {
crate::error!("Failed to ping docker server: {}", e);
return Err(e);
}
Ok(Arc::new(docker))
}
async fn wait_for_container(docker: &Docker, id: &str) -> Result<(), bollard::errors::Error> {
let options = WaitContainerOptions {
condition: "not-running",
};
let mut wait_stream = docker.wait_container(id, Some(options));
while let Some(msg) = wait_stream.next().await {
match msg {
Ok(msg) => {
if msg.status_code == 0 {
break;
}
if let Some(err) = msg.error {
crate::error!("Failed to wait for container: {:?}", err.message);
// TODO: These aren't the same error type, is this correct?
return Err(bollard::errors::Error::DockerContainerWaitError {
error: err.message.unwrap_or_default(),
code: msg.status_code,
});
}
}
Err(e) => {
match &e {
bollard::errors::Error::DockerContainerWaitError { error, code } => {
crate::error!("Container failed with status code `{}`: {error}", code);
}
_ => crate::error!("Container failed with error: {:?}", e),
}
return Err(e);
}
}
}
Ok(())
}