How to do it...

  1. The first step will be to create an account on Docker Hub so that we can publish our images. Go to https://hub.docker.com and create an account. You can also use your GitHub account and log in using it if you have one.
  2. Once you have an account, we will need to create a repository named springbootcookbook.
  3. With this account created, now is the time to build the image. For this, we will use one of the Gradle Docker plugins. We will start by changing build.gradle to modify the buildscript block with the following change:
buildscript { 
  dependencies { 
    classpath("org.springframework.boot:spring-boot-gradle- 
      plugin:${springBootVersion}") 
    classpath("se.transmode.gradle:gradle-docker:1.2") 
  } 
} 
  1. We will also need to apply this plugin by adding the apply plugin: 'docker' directive to the build.gradle file.
  2. We also need to explicitly add the application plugin to build.gradle as well, since it is no longer automatically included by the Spring Boot Gradle plugin.
  3. Add apply plugin: 'application' to the list of plugins in the build.gradle file.
  4. Lastly, we will need to add the following Docker configuration to the build.gradle file as well:
task distDocker(type: Docker,  
                overwrite: true,  
                dependsOn: bootDistTar) { 
    group = 'docker' 
    description = "Packs the project's JVM application
as a Docker image." inputs.files project.bootDistTar def installDir = "/" + project.bootDistTar.archiveName - ".${project.bootDistTar.extension}" doFirst { tag "ch6" push false exposePort 8080 exposePort 8443 addFile file("${System.properties['user.home']}
/.keystore"), "/root/" applicationName = project.applicationName addFile project.bootDistTar.outputs.files.singleFile entryPoint = ["$installDir/bin/${project.applicationName}"] } }
  1. Assuming that you already have Docker installed on your machine, we can proceed to creating the image by executing ./gradlew clean distDocker.
  1. For Docker installation instructions, please visit the tutorial that is located at https://docs.docker.com/installation/#installation. If everything has worked out correctly, you should see the following output:
> Task :distDocker
Sending build context to Docker daemon 68.22MB Step 1/6 : FROM aglover/java8-pier ---> 3f3822d3ece5 Step 2/6 : EXPOSE 8080 ---> Using cache ---> 73717aaca6f3 Step 3/6 : EXPOSE 8443 ---> Using cache ---> 6ef3c0fc3d2a Step 4/6 : ADD .keystore /root/ ---> Using cache ---> 6efebb5a868b Step 5/6 : ADD ch6-boot-0.0.1-SNAPSHOT.tar / ---> Using cache ---> 0634eace4952 Step 6/6 : ENTRYPOINT /ch6-boot-0.0.1-SNAPSHOT/bin/ch6 ---> Using cache ---> 39a853b7ddbb Successfully built 39a853b7ddbb Successfully tagged ch6:0.0.1-SNAPSHOT BUILD SUCCESSFUL
Total time: 1 mins 0.009 secs.
  1. We can also execute the following Docker images command so as to see the newly created image:
$ docker images
REPOSITORY           TAG                IMAGE ID         CREATED             VIRTUAL  SIZE
ch6                  0.0.1-SNAPSHOT     39a853b7ddbb     17 minutes ago      1.04 GB
aglover/java8-pier   latest             69f4574a230e     11 months ago       1.01 GB
  1. With the image built successfully, we are now ready to start it in Docker by executing the following command:
docker run -d -P ch6:0.0.1-SNAPSHOT.  
  1. After the container has started, we can query the Docker registry for the port bindings so that we can access the HTTP endpoints for our service. This can be done via the docker ps command. If the container is running successfully, we should see the following result (names and ports will vary):
    CONTAINER ID        IMAGE               COMMAND               
CREATED STATUS PORTS
NAMES
37b37e411b9e ch6:latest "/ch6-boot-0.0.1-S..."
10 minutes ago Up 10 minutes 0.0.0.0:32778-
>8080/tcp, 0.0.0.0:32779->8443/tcp drunk_carson
  1. From this output, we can tell that the port mapping for the internal port 8080 has been set up to be 32778 (your port will vary for every run). Let's open http://localhost:32778/books in the browser to see our application in action, as shown in the following screenshot:
If you are using macOS X with boot2docker, then you won't be running the Docker container locally. In this scenario, you will be using the boot2docker ip instead of the local host to connect to the application. For more tips on how to make the boot2docker integration easier, please visit http://viget.com/extend/how-to-use-docker-on-os-x-the-missing-guide. One can also use a nice Docker façade, generously created by Ian Sinnott, which will automatically start boot2docker and handle the environment variables as well. To get the wrapper, go to https://gist.github.com/iansinnott/0a0c212260386bdbfafb.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.63.145