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

Support registries other than Docker Hub #311

Closed
Closed
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
25 changes: 23 additions & 2 deletions src/clients/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ impl Cli {

let output = command.output().expect("Failed to execute docker command");

assert!(output.status.success(), "failed to start container");
if !output.status.success() {
log::error!("{}", String::from_utf8_lossy(&output.stderr));
panic!("Failed to start container");
}

let container_id = String::from_utf8(output.stdout)
.expect("output is not valid utf8")
.trim()
Expand Down Expand Up @@ -170,9 +174,14 @@ impl Client {
command.arg("-P"); // publish all exposed ports
}

let image_path = match image.registry_prefix() {
Some(prefix) => format!("{}/{}", prefix, image.descriptor()),
None => image.descriptor(),
};

command
.arg("-d") // Always run detached
.arg(image.descriptor())
.arg(image_path)
.args(image.args().clone().into_iterator())
.stdout(Stdio::piped());

Expand Down Expand Up @@ -547,6 +556,18 @@ mod tests {
);
}

#[test]
fn cli_run_command_should_include_custom_registry_path() {
let image = GenericImage::new("hello", "0.0");
let image = RunnableImage::from(image).with_registry_prefix("gcr.io/test");
let command = Client::build_run_command(&image, Command::new("docker"));

assert_eq!(
format!("{:?}", command),
r#""docker" "run" "-P" "-d" "gcr.io/test/hello:0.0""#
);
}

#[test]
fn should_create_network_if_image_needs_it_and_drop_it_in_the_end() {
{
Expand Down
14 changes: 14 additions & 0 deletions src/core/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ pub struct RunnableImage<I: Image> {
image: I,
image_args: I::Args,
image_tag: Option<String>,
registry_prefix: Option<String>,
container_name: Option<String>,
network: Option<String>,
env_vars: BTreeMap<String, String>,
Expand All @@ -158,6 +159,10 @@ impl<I: Image> RunnableImage<I> {
&self.network
}

pub fn registry_prefix(&self) -> &Option<String> {
&self.registry_prefix
}

pub fn container_name(&self) -> &Option<String> {
&self.container_name
}
Expand Down Expand Up @@ -209,6 +214,14 @@ impl<I: Image> RunnableImage<I> {
}
}

/// Users can use this API to pull image from registries other than Docker Hub.
pub fn with_registry_prefix(self, prefix: impl Into<String>) -> Self {
Self {
registry_prefix: Some(prefix.into()),
..self
}
}

pub fn with_container_name(self, name: impl Into<String>) -> Self {
Self {
container_name: Some(name.into()),
Expand Down Expand Up @@ -262,6 +275,7 @@ impl<I: Image> From<(I, I::Args)> for RunnableImage<I> {
image,
image_args,
image_tag: None,
registry_prefix: None,
container_name: None,
network: None,
env_vars: BTreeMap::default(),
Expand Down