dockerfile_builder

Crates.iodockerfile_builder
lib.rsdockerfile_builder
version0.1.4
sourcesrc
created_at2023-07-15 20:32:17.35876
updated_at2023-07-18 17:15:43.985367
descriptionFlexible Dockerfile builder with type-safe features
homepage
repositoryhttps://github.com/ptmphuong/dockerfile-builder/
max_upload_size
id917406
size55,441
Phuong Pham (ptmphuong)

documentation

README

Documentation Crates.io Crates.io

This library provides a convenient way to programmatically generate Dockerfiles using Rust.

Dockerfiles instructions can be generated using structured and type-safe interfaces, or they can be added flexibly in raw form.

Quickstart

[dependencies]
dockerfile_builder = "0.1.4"
use dockerfile_builder::Dockerfile;
use dockerfile_builder::instruction::{RUN, EXPOSE};

fn main() {
    let dockerfile = Dockerfile::default()
        .push(RUN::from("echo $HOME"))
        .push(EXPOSE::from("80/tcp"))
        .push_any("# Just adding a comment");
    
    let expected = r#"RUN echo $HOME
EXPOSE 80/tcp
# Just adding a comment"#;

    assert_eq!(
        dockerfile.to_string(),
        expected
    );
}

Type-safe support

Dockerfile instructions can be created from a string or with instruction builders. Instruction builders provide structured and type-safe interfaces to build instructions.

use dockerfile_builder::Dockerfile;
use dockerfile_builder::instruction::EXPOSE;
use dockerfile_builder::instruction_builder::ExposeBuilder;

fn main() -> eyre::Result<()) {
    let expose = EXPOSE::from("80/tcp");
    
    let expose_from_builder = ExposeBuilder::builder()
        .port(80)
        .protocol("tcp")
        .build()?;
    
    assert_eq!(expose, expose_from_builder);
    
    let dockerfile = Dockerfile::default()
        .push(expose_from_builder);
      
    assert_eq!(
        dockerfile.to_string(), 
        "EXPOSE 80/tcp"
    );

    Ok(())
}
Commit count: 63

cargo fmt