Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: image build args #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Image {
let headers = opts
.auth_header()
.map(|auth| Headers::single(AUTH_HEADER, auth))
.unwrap_or_else(Headers::default);
.unwrap_or_default();

self.docker
.post_string(&ep, Payload::empty(), Some(headers))
Expand Down
2 changes: 1 addition & 1 deletion src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub mod swarm;
#[cfg_attr(docsrs, doc(cfg(feature = "swarm")))]
pub mod task;

pub use {container::*, exec::*, image::*, network::*, system::*, volume::*};
pub use {container::*, exec::*, image::*, network::*, volume::*};

#[cfg(feature = "swarm")]
#[cfg_attr(docsrs, doc(cfg(feature = "swarm")))]
Expand Down
5 changes: 4 additions & 1 deletion src/opts/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ impl ImageBuildOptsBuilder {
cpu_quota: usize => "cpuquota"
);

// TODO: buildargs
impl_map_field!(url
/// Set build-time variables.
build_args => "buildargs"
);

impl_url_field!(
/// Size of /dev/shm in bytes. The size must be greater than 0. If omitted the system uses 64MB.
Expand Down
20 changes: 16 additions & 4 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
use std::env;
use std::path::PathBuf;

pub use docker_api::{api, conn, models, models::ImageBuildChunk, opts, Docker};
pub use futures_util::{StreamExt, TryStreamExt};
#[allow(unused_imports)]
pub use docker_api::conn;
pub use docker_api::{api, models, models::ImageBuildChunk, opts, Docker};
pub use futures_util::StreamExt;
#[allow(unused_imports)]
pub use futures_util::TryStreamExt;
pub use tempfile::TempDir;

pub const DEFAULT_IMAGE: &str = "ubuntu:latest";
Expand All @@ -22,9 +26,12 @@ pub fn init_runtime() -> Docker {
#[cfg(unix)]
{
let uid = nix::unistd::Uid::effective();
let docker_user_sock = PathBuf::from(format!("/run/user/{uid}/docker.sock"));
let docker_dir = PathBuf::from(format!("/run/user/{uid}/docker"));
let docker_root_dir = PathBuf::from("/var/run");
if docker_dir.exists() {
if docker_user_sock.exists() {
Docker::unix(docker_user_sock)
} else if docker_dir.exists() {
Docker::unix(docker_dir.join("docker.sock"))
} else if docker_root_dir.exists() {
Docker::unix(docker_root_dir.join("docker.sock"))
Expand Down Expand Up @@ -87,7 +94,12 @@ pub async fn get_container_full_id(docker: &Docker, name: &str) -> String {
pub fn tempdir_with_dockerfile(content: Option<&str>) -> TempDir {
let tmp = TempDir::new().expect("temp dir for image");
let default_dockerfile = format!(
"FROM {DEFAULT_IMAGE}\nRUN echo 1234 > {TEST_IMAGE_PATH}\nRUN echo 321\nCMD sleep inf",
"FROM {DEFAULT_IMAGE}
ARG TEST_ARG=\"\"
RUN echo 1234 > {TEST_IMAGE_PATH}
RUN echo 321
ENV TEST_ENV=\"${{TEST_ARG}}\"
CMD sleep inf",
);

std::fs::write(
Expand Down
24 changes: 24 additions & 0 deletions tests/image_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ async fn image_create_inspect_delete() {
assert!(image.inspect().await.is_err());
}

#[tokio::test]
async fn image_create_with_build_args() {
let docker = init_runtime();

let image_name = "test-build-args-image";
let tmp = tempdir_with_dockerfile(None);
let opts = opts::ImageBuildOpts::builder(tmp.path())
.tag(image_name)
.build_args([("TEST_ARG", "test_value")])
.build();

let image = create_base_image(&docker, image_name, Some(opts)).await;

assert!(image.inspect().await.is_ok());
let inspect_data = image.inspect().await.expect("image inspect data");
assert!(inspect_data
.config
.expect("config element")
.env
.expect("environment")
.contains(&"TEST_ENV=test_value".to_string()));
assert!(image.delete().await.is_ok());
}

#[tokio::test]
async fn image_inspect() {
let docker = init_runtime();
Expand Down
Loading