22
33import com .docker .BaseTestCase ;
44import com .github .dockerjava .api .DockerClient ;
5+ import com .github .dockerjava .api .command .BuildImageCmd ;
56import com .github .dockerjava .api .command .CreateContainerResponse ;
67import com .github .dockerjava .api .command .ExecCreateCmdResponse ;
78import com .github .dockerjava .api .exception .DockerClientException ;
89import com .github .dockerjava .api .exception .DockerException ;
910import com .github .dockerjava .api .exception .NotFoundException ;
1011import com .github .dockerjava .api .model .*;
1112import com .github .dockerjava .core .RemoteApiVersion ;
12- import com .github .dockerjava .core .command .BuildImageResultCallback ;
13- import com .github .dockerjava .core .command .EventsResultCallback ;
14- import com .github .dockerjava .core .command .PullImageResultCallback ;
13+ import com .github .dockerjava .core .command .*;
14+ import com .github .dockerjava .core .util .CompressArchiveUtil ;
15+ import org .apache .commons .io .FileUtils ;
16+ import org .apache .commons .io .filefilter .TrueFileFilter ;
1517import org .junit .Test ;
1618import org .springframework .beans .factory .annotation .Autowired ;
1719
18- import java .io .File ;
19- import java .io .IOException ;
20+ import java .io .*;
2021import java .security .SecureRandom ;
22+ import java .util .Arrays ;
23+ import java .util .Collection ;
24+ import java .util .HashSet ;
25+ import java .util .UUID ;
2126import java .util .concurrent .TimeUnit ;
2227
2328import static org .hamcrest .MatcherAssert .assertThat ;
29+ import static org .hamcrest .Matchers .containsString ;
2430import static org .hamcrest .Matchers .isEmptyString ;
2531import static org .hamcrest .Matchers .not ;
2632
@@ -276,8 +282,6 @@ public void createContainerWithLinkInCustomNetwork1() throws DockerException {
276282 }
277283
278284
279-
280-
281285 @ Test
282286 public void createweb () throws DockerException {
283287 ExposedPort tcp8080 = ExposedPort .tcp (8080 );
@@ -303,10 +307,57 @@ public void createweb() throws DockerException {
303307 }
304308
305309
310+ private String filePath = "C:\\ Users\\ sofia\\ Desktop\\ 文档清单\\ ecm_cloud\\ application\\ dockerfile" ;
311+
312+ private String imageName = "106.14.196.164:8888/test/ecm_cloud-test:v2.0.0" ;
313+ /*
314+ * docker build -t $name:$tag -f /home/docker/deploy/edc/application/dockerfile/Dockerfile /home/docker/deploy/edc/application/dockerfile;
315+ * */
316+
306317 @ Test
307- public void execCreateTest () {
318+ public void 制作镜像_1 () {
319+ File baseDir = new File (filePath );
320+ Collection <File > files = FileUtils .listFiles (baseDir , TrueFileFilter .INSTANCE , TrueFileFilter .INSTANCE );
321+ try {
322+ File tarFile = CompressArchiveUtil .archiveTARFiles (baseDir , files , UUID .randomUUID ().toString ());
323+ dockerfileBuild (new FileInputStream (tarFile ));
324+ } catch (IOException e ) {
325+ e .printStackTrace ();
326+ }
327+ }
328+
329+ private void dockerfileBuild (InputStream tarInputStream , String dockerFilePath ) {
330+ BuildImageCmd buildImageCmd = dockerClient .buildImageCmd ()
331+ .withTags (new HashSet <String >(Arrays .asList (imageName )))
332+ .withTarInputStream (tarInputStream )
333+ .withDockerfilePath (dockerFilePath );
334+ execBuild (buildImageCmd );
335+ }
336+
337+ private void dockerfileBuild (InputStream tarInputStream ) {
338+ BuildImageCmd buildImageCmd = dockerClient .buildImageCmd ()
339+ .withTags (new HashSet <String >(Arrays .asList (imageName )))
340+ .withTarInputStream (tarInputStream );
341+ execBuild (buildImageCmd );
342+ }
343+
344+ private void dockerfileBuild (File baseDir ) {
345+ BuildImageCmd buildImageCmd = dockerClient .buildImageCmd (baseDir )
346+ .withTags (new HashSet <String >(Arrays .asList (imageName )));
347+ execBuild (buildImageCmd );
348+ }
349+
350+ private void execBuild (BuildImageCmd buildImageCmd ) {
351+ String imageId = buildImageCmd .withNoCache (true ).exec (new BuildImageResultCallback ()).awaitImageId ();
352+
353+ /* try {
354+ dockerClient.pushImageCmd(imageName)
355+ .withAuthConfig(dockerClient.authConfig())
356+ .exec(new PushImageResultCallback())
357+ .awaitCompletion(30, TimeUnit.SECONDS);
358+ } catch (InterruptedException e) {
359+ e.printStackTrace();
360+ }*/
308361
309- ExecCreateCmdResponse execCreateCmdResponse = dockerClient .execCreateCmd ("3139c430c28d" )
310- .withCmd ("touch" ,"/etc/nginx/conf.d/1.txt" ).exec ();
311362 }
312363}
0 commit comments