我正在尝试从tar文件加载图像到docker,并验证tar文件是否有效。现在,如果tar文件有效,我需要获取imageName和标记。
但是我检查了一下,exec方法的返回类型是void。谁知道怎样才能得到imageName和标签。
@Autowired
private DockerClient dockerClient;
public void loadImage(InputStream inputStream) {
dockerClient.loadImageCmd(inputStream).exec();
}我正在使用下面的库
com.github.docker-java:docker-java:3.2.5
com.github.docker-java:docker-java-transport-httpclient5:3.2发布于 2021-03-02 13:11:51
这个问题已经被问过了
这里
它们返回loadImageCmd的空值。我找到了解决此问题的方法。
如果我们将tar文件存储在某个位置,那么我们就可以读取清单文件并从中获得repo名称。
import org.apache.commons.vfs2.FileContent;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.FileType;
import org.apache.commons.vfs2.VFS;
public InputStream getManifestFileInputStream(String fileName){
InputStream manifestInputStream = null;
FileObject archive;
try {
FileSystemManager fsManager = VFS.getManager();
archive = fsManager.resolveFile("tar:file://" + fileName);
FileObject[] children = archive.getChildren();
for (int i = 0; i < children.length; i++) {
FileObject fo = children[i];
if (fo.isReadable() && fo.getType() == FileType.FILE
&& fo.getName().toString().contains("manifest.json")) {
FileContent fc = fo.getContent();
manifestInputStream = fc.getInputStream();
}
}
return manifestInputStream;
} catch (Exception e)
return null;
}
}我使用下面的依赖来提取manifest.json的inputStream
org.apache.commons
commons-vfs2
2.7.0获得输入流后,将此输入流转换为JSONArray对象并获得repoTag。
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public String getRepoName(InputStream inputStream) {
String repoName = null;
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new InputStreamReader(inputStream, "UTF-8"));
JSONArray jsonArray = (JSONArray)obj;
if (!jsonArray.isEmpty()) {
JSONObject jsonObject = (JSONObject)jsonArray.get(0);
JSONArray repoTags = (JSONArray)jsonObject.get("RepoTags");
if (!repoTags.isEmpty()) {
repoName = (String)repoTags.get(0);
}
}
return repoName;
} catch (Exception e) {
return repoName;
}
}下面使用依赖项将inputStream转换为JsonArray对象。
com.googlecode.json-simple
json-simple
1.1.1https://stackoverflow.com/questions/66349275
复制相似问题