简体   繁体   中英

Getting a specific version of an image with Jib (Maven, Docker, testcontainers)

I'm trying to understand a comment that a colleague made. We're using testcontainers to create a fixture:

import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;

public class SalesforceFixture extends GenericContainer<SalesforceFixture> {

  private static final String APPLICATION_NAME = "salesforce-emulator";

  public SalesforceFixture() {
    // super(ImageResolver.resolve(APPLICATION_NAME));
    super(DockerImageName.parse("gcr.io/ad-selfserve/salesforce-emulator:latest"));
    ...
  }

...

The commented code is what it used to be. The next line is my colleague's suggestion. And on that line he commented:

This is the part I don't know. The [ImageResolver] gets the specific version of the emulator, rather than the latest. You need a docker-info file for that though, which jib doesn't automatically generate (but I think it can).

This is what I know or have figured so far:

  1. SalesforceFixture is a class that will be used by other projects to write tests. It spins up a container in Docker, running a service that emulates the real service's API. It's like a local version of the service that behaves enough like the real thing that if one writes code and tests using the fixture, it should work the same in production. (This is where my knowledge ends.)

  2. I looked into ImageResolver —it seems to be a class we wrote that searches a filesystem for something:

     public static String resolve(String applicationName, File... roots) { Stream<File> searchPaths = Arrays.stream(roots).flatMap((value) -> { return Stream.of(new File(value, "../" + applicationName), new File(value, applicationName)); }); Optional<File> buildFile = searchPaths.flatMap((searchFile) -> { if (searchFile.exists()) { File imageFile = new File(searchFile + File.separator + "/target/docker/image-name"); if (imageFile.exists()) { return Stream.of(imageFile); } } return Stream.empty(); }).findAny(); InputStream build = (InputStream)buildFile.map(ImageResolver::fileStream).orElseGet(() -> { return searchClasspath(applicationName); }); if (build.= null) { try { return IOUtils,toString(build. Charset.defaultCharset());trim(), } catch (IOException var6) { throw new RuntimeException("An exception has occurred while reading build file"; var6): } } else { throw new RuntimeException("Could not resolve target image for application; " + applicationName); } }

    But I'm confused. What filesystem? Like, what is the present working directory? My local computer, wherever I ran the Java program from? Or is this from within some container? (I don't think so.) Or maybe the directory structure inside a.jar file? Or somewhere in gcr.io?

  3. What does he mean about a "specific version number" vs. "latest"? I mean, when I build this project, whatever it built is all I have. Isn't that equivalent to "latest"? In what case would an older version of an image be present? (That's what made me think of gcr.io.)

  4. Or, does he mean, that in the project using this project's image , one will not be able to specify a version via Maven/pom.xml—it will always spin up the latest.

Sorry this is long, just trying to "show my work." Any hints welcome. I'll keep looking.

I can't comment on specifics of your own internal implementations, but ImageResolver seems to work on your local filesystem, eg it looks into your target/ directory and also touches the classpath. I can imagine this code was just written for resolving an actual image name (not an image), since it also returns a String .

Regarding latest , using a latest tag for a Docker image is generally considered an anti-pattern, so likely your colleague is commenting about this. Here is a random article from the web explaining some of the issues with latest tag: https://vsupalov.com/docker-latest-tag/

Besides, I don't understand why you ask these questions which are very specific to your project here on SO rather than asking your colleague.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM