DockerfileParser { public static Dockerfile parse(File file) throws DockerFileException { try { return parse(CharStreams.readLines(Files.newReader(file, Charset.defaultCharset()))); } catch (IOException e) { throw new DockerFileException("Error happened parsing the Docker file." + e.getMessage(), e); } } private  DockerfileParser(); static Dockerfile parse(File file); static Dockerfile parse(final URL file); static Dockerfile parse(Reader reader); static Dockerfile parse(String contentOfDockerFile);  }
@Test public void testParse() throws Exception { String dockerfileContent = "FROM base_image\n" + "# Comment 1\n" + "MAINTAINER Codenvy Corp\n" + "# Comment 2\n" + "RUN echo 1 > /dev/null\n" + "RUN echo 2 > /dev/null\n" + "RUN echo 3 > /dev/null\n" + "ADD file1 /tmp/file1\n" + "ADD http: "EXPOSE 6000 7000\n" + "EXPOSE 8000 9000\n" + "# Comment 3\n" + "ENV ENV_VAR1 hello world\n" + "ENV ENV_VAR2\t to be or not to be\n" + "VOLUME [\"/data1\", \t\"/data2\"]\n" + "USER andrew\n" + "WORKDIR /tmp\n" + "ENTRYPOINT echo hello > /dev/null\n" + "CMD echo hello > /tmp/test"; File targetDir = new File(Thread.currentThread().getContextClassLoader().getResource(".").toURI()).getParentFile(); File file = new File(targetDir, "testParse"); FileWriter w = new FileWriter(file); w.write(dockerfileContent); w.flush(); w.close(); List<DockerImage> dockerImages = DockerfileParser.parse(file).getImages(); assertEquals( dockerImages.size(), 1); DockerImage dockerImage = dockerImages.get(0); assertEquals(dockerImage.getFrom(), "base_image"); assertEquals(dockerImage.getMaintainer(), Arrays.asList("Codenvy Corp")); assertEquals(dockerImage.getRun(), Arrays.asList("echo 1 > /dev/null", "echo 2 > /dev/null", "echo 3 > /dev/null")); assertEquals(dockerImage.getCmd(), "echo hello > /tmp/test"); assertEquals(dockerImage.getExpose(), Arrays.asList("6000", "7000", "8000", "9000")); Map<String, String> env = new LinkedHashMap<>(); env.put("ENV_VAR1", "hello world"); env.put("ENV_VAR2", "to be or not to be"); assertEquals(env, dockerImage.getEnv()); assertEquals( dockerImage.getAdd(), Arrays.asList(Pair.of("file1", "/tmp/file1"), Pair.of("http: assertEquals(dockerImage.getEntrypoint(), "echo hello > /dev/null"); assertEquals(dockerImage.getVolume(), Arrays.asList("/data1", "/data2")); assertEquals(dockerImage.getUser(), "andrew"); assertEquals(dockerImage.getWorkdir(), "/tmp"); assertEquals(dockerImage.getComments(), Arrays.asList("Comment 1", "Comment 2", "Comment 3")); }
@Test public void testParseMultipleImages() throws Exception { String dockerfileContent = "FROM base_image_1\n" + "# Image 1\n" + "MAINTAINER Codenvy Corp\n" + "RUN echo 1 > /dev/null\n" + "ADD http: "EXPOSE 6000 7000\n" + "ENV ENV_VAR\t to be or not to be\n" + "VOLUME [\"/data1\"]\n" + "USER andrew\n" + "WORKDIR /tmp\n" + "ENTRYPOINT echo hello > /dev/null\n" + "CMD echo hello > /tmp/test1" + "\n" + "\n" + "FROM base_image_2\n" + "# Image 2\n" + "MAINTAINER Codenvy Corp\n" + "RUN echo 2 > /dev/null\n" + "ADD file1 /tmp/file1\n" + "EXPOSE 8000 9000\n" + "ENV ENV_VAR\t to be or not to be\n" + "VOLUME [\"/data2\"]\n" + "USER andrew\n" + "WORKDIR /home/andrew\n" + "ENTRYPOINT echo test > /dev/null\n" + "CMD echo hello > /tmp/test2"; File targetDir = new File(Thread.currentThread().getContextClassLoader().getResource(".").toURI()).getParentFile(); File file = new File(targetDir, "testParse"); FileWriter w = new FileWriter(file); w.write(dockerfileContent); w.flush(); w.close(); List<DockerImage> dockerImages = DockerfileParser.parse(file).getImages(); assertEquals(2, dockerImages.size()); DockerImage dockerImage1 = dockerImages.get(0); assertEquals(dockerImage1.getFrom(), "base_image_1"); assertEquals(dockerImage1.getMaintainer(), Arrays.asList("Codenvy Corp")); assertEquals(dockerImage1.getRun(), Arrays.asList("echo 1 > /dev/null")); assertEquals(dockerImage1.getCmd(), "echo hello > /tmp/test1"); assertEquals(dockerImage1.getExpose(), Arrays.asList("6000", "7000")); Map<String, String> env1 = new LinkedHashMap<>(); env1.put("ENV_VAR", "to be or not to be"); assertEquals(dockerImage1.getEnv(), env1); assertEquals(dockerImage1.getAdd(), Arrays.asList(Pair.of("http: assertEquals(dockerImage1.getEntrypoint(), "echo hello > /dev/null"); assertEquals(dockerImage1.getVolume(), Arrays.asList("/data1")); assertEquals(dockerImage1.getUser(), "andrew"); assertEquals(dockerImage1.getWorkdir(), "/tmp"); assertEquals(dockerImage1.getComments(), Arrays.asList("Image 1")); DockerImage dockerImage2 = dockerImages.get(1); assertEquals(dockerImage2.getFrom(), "base_image_2"); assertEquals(dockerImage2.getMaintainer(), Arrays.asList("Codenvy Corp")); assertEquals(dockerImage2.getRun(), Arrays.asList("echo 2 > /dev/null")); assertEquals(dockerImage2.getCmd(), "echo hello > /tmp/test2"); assertEquals(dockerImage2.getExpose(), Arrays.asList("8000", "9000")); Map<String, String> env2 = new LinkedHashMap<>(); env2.put("ENV_VAR", "to be or not to be"); assertEquals(dockerImage2.getEnv(), env2); assertEquals(dockerImage2.getAdd(), Arrays.asList(Pair.of("file1", "/tmp/file1"))); assertEquals(dockerImage2.getEntrypoint(), "echo test > /dev/null"); assertEquals(dockerImage2.getVolume(), Arrays.asList("/data2")); assertEquals(dockerImage2.getUser(), "andrew"); assertEquals(dockerImage2.getWorkdir(), "/home/andrew"); assertEquals(dockerImage2.getComments(), Arrays.asList("Image 2")); }