Comments (2)
So, you can download the contents of a container that was created from an image ... an example might be in the test https://github.com/fussybeaver/bollard/blob/master/tests/container_test.rs#L603-L612
Otherwise, there is also the buildkit functionality that will generate a file structure on build of an image.. https://github.com/moby/buildkit/blob/master/README.md#local-directory - that might be possible with some code changes, I'll need to take a closer look in the coming days.
from bollard.
This is the code I ended up using , I had to assume a default path of /entrypoint.wasm
because I couldn't figure out a way to create an image with a ENTRYPOINT
programmatically. It might be nice if there was a higher level equivalent to this in the library, so that you could have the equivalent of docker cp
as a function.
async fn pull_from_image(name: &str) -> Result<Vec<u8>, eyre::Error> {
let docker = Docker::connect_with_local_defaults()?;
// Pull the Docker image
let options = CreateImageOptions {
from_image: name.to_string(),
..<_>::default()
};
let mut stream = docker.create_image(Some(options), None, None);
while let Some(_) = stream.next().await {}
// Create a new container from the image
let options = CreateContainerOptions {
name: "my-container",
..<_>::default()
};
let container = docker
.create_container(
Some(options),
bollard::container::Config {
image: Some(name),
cmd: Some(vec!["/usr/bin/true"]),
..<_>::default()
},
)
.await?;
let _ = docker.start_container::<String>(&container.id, None).await;
// Inspect the container to retrieve the ENTRYPOINT value
let options = InspectContainerOptions { size: false };
let container_info = docker
.inspect_container(&container.id, Some(options))
.await?;
let entrypoint = match container_info.config {
Some(config) => match config.entrypoint {
Some(entrypoint) => entrypoint.join("/"),
None => "/entrypoint.wasm".into(),
},
None => return Err(eyre::eyre!("Failed to retrieve container configuration.")),
};
// Retrieve the WASM module from the container using the Docker API
let stream = docker.download_from_container(
&container.id,
Some(DownloadFromContainerOptions {
path: entrypoint.clone(),
}),
);
let bytes = stream
.collect::<Vec<_>>()
.await
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
let bytes = bytes
.into_iter()
.fold(bytes::BytesMut::new(), |mut buf, item| {
buf.extend(item);
buf
});
// Remove the container
let options = RemoveContainerOptions {
v: true,
force: true,
link: false,
};
docker
.remove_container(&container.id, Some(options))
.await?;
// Decompress the tar archive in memory
let mut archive = Archive::new(Self::decompress_layer(&bytes)?);
let mut wasm_module = Vec::new();
for file in archive.entries()? {
let mut file = file?;
if std::path::Path::new("/").join(file.path()?) == std::path::Path::new(&entrypoint) {
file.read_to_end(&mut wasm_module)?;
break;
}
}
Ok(wasm_module.into())
}
fn decompress_layer(layer: &[u8]) -> Result<Box<dyn Read + '_>, eyre::Error> {
const GZIP: &[u8] = b"\x1F\x8B";
const BZIP2: &[u8] = b"\x42\x5A";
const XZ: &[u8] = b"\xFD\x37";
match &layer[0..2] {
GZIP => Ok(Box::from(GzDecoder::new(layer))),
BZIP2 => Ok(Box::from(BzDecoder::new(layer))),
XZ => Ok(Box::from(XzDecoder::new(layer))),
_ => Ok(Box::from(Cursor::new(layer))),
}
}
from bollard.
Related Issues (20)
- Youki as runtime HOT 7
- 'docker diff <running container id>' HOT 2
- Docker exec example with output
- "Universal" connection method? HOT 2
- serde_with used in different versions
- Bind Volume Mounts HOT 6
- Create container with a specified ip, skip assigned ip HOT 1
- Exposed ports not bound HOT 1
- Correct options for accessing Docker host IP from inside container HOT 3
- Migrate `webpki` dependency to `rustls-webpki` to mitigate RUSTSEC-2023-0052 HOT 4
- Events without options HOT 2
- How to publish ports? HOT 3
- Equivalent of `--add-host=host.docker.internal:host-gateway` HOT 1
- Handle when socket is in the home directory HOT 5
- one-shot stats fails with Docker 24.0.7
- `DockerResponseServerError { status_code: 400, message: "file with no instructions" }` HOT 4
- how to use github registry? HOT 1
- type_length_limit HOT 1
- Hyper 1.x upgrade
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bollard.