1

I am trying to load image to docker from tar file and verify if the tar file is valid or not. Now if tar file is valid I need to get the imageName and tag.

But I checked and the return type for exec method is void. Does anyone know how can I get the imageName and tag.

@Autowired
private DockerClient dockerClient;

public void loadImage(InputStream inputStream) {
        dockerClient.loadImageCmd(inputStream).exec();
}

I am using below library

com.github.docker-java:docker-java:3.2.5
com.github.docker-java:docker-java-transport-httpclient5:3.2

1 Answer 1

1

This question is already asked here and they return void for loadImageCmd. I found a workaround for this.

If we have the tar file stored in some location we can then read the manifest file and get the repo name from that.

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;
            }
        }

I used below dependecy to extract inputStream of manifest.json

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-vfs2</artifactId>
    <version>2.7.0</version>
</dependency>

Once you get the input stream convert this inputstream to JSONArray object and get the 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;
            }
        }

Used below dependency to convert inputStream to JsonArray object.

<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.