Crate dockerfile_builder

Source
Expand description

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.

§Usage

§Build Dockerfile

Dockerfile contains a list of Instruction.

use dockerfile_builder::Dockerfile;
use dockerfile_builder::instruction::{RUN, EXPOSE};

let mut dockerfile = Dockerfile::new();
    dockerfile.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
);

§Dockerfile Instructions

Instruction can be created from String or from Instruction Builder. 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, PortProtocol};

let expose = EXPOSE::from("80/tcp");

let expose_from_builder = ExposeBuilder::builder()
   .port(80)
   .protocol(PortProtocol::Tcp)
   .build()
   .unwrap();

assert_eq!(expose, expose_from_builder);

let mut dockerfile = Dockerfile::new();
   dockerfile.push(expose_from_builder);
 
assert_eq!(
   dockerfile.to_string(),
   "EXPOSE 80/tcp"
);

Modules§

instruction
Dockerfile Instruction definitions
instruction_builder
Type-safe interfaces for building Instructions

Structs§

Dockerfile
Dockerfile builder