Building Images

Prerequisite

In this section, we are assuming you are running Docker in your local machine (either using Docker Tools or native Docker).

To make it work correctly, you need to run this section in a new terminal window to avoid using the Kubernetes (minikube) environment used in previous sections.

Build your application artifact

First let’s take a quick look at the application we’re looking to build

If you’re running this from within VSCode you can use CTRL+p (or CMD+p on Mac OSX) to quickly open MyRESTController.java

hello world app
Figure 1. MyRESTController.java

Compile, build and test the Spring Boot Java project:

cd apps/helloworld/springboot
mvn clean package
java -jar target/boot-demo-1.0.0.jar

Then curl it in a separate terminal:

curl localhost:8080
Aloha from Spring Boot! 1 on unknown

unknown because the environment variable is not currently set, it will be inside of a Docker container and inside of Kubernetes.

Build container image

Change quay.io for your registry (e.g. docker.io) and your-repo to your organization. This next step does assume you have a working installation of Docker for Mac/Windows/Linux.
docker build -t quay.io/your-repo/myapp:v1 .

Results:

Sending build context to Docker daemon  14.47MB
Step 1/6 : FROM openjdk:8u151
 ---> a30a1e547e6d
Step 2/6 : ENV JAVA_APP_JAR boot-demo-1.0.0.jar
 ---> Using cache
 ---> 62b714308856
Step 3/6 : WORKDIR /app/
 ---> Using cache
 ---> aefc5bf44b15
Step 4/6 : COPY target/$JAVA_APP_JAR .
 ---> f881c5f5815b
Step 5/6 : EXPOSE 8080
 ---> Running in 4e9adc135345
Removing intermediate container 4e9adc135345
 ---> 2909459c83f6
Step 6/6 : CMD java $JAVA_OPTIONS -jar $JAVA_APP_JAR
 ---> Running in 46bcab555de7
Removing intermediate container 46bcab555de7
 ---> 85b78b9b70b1
Successfully built 85b78b9b70b1
Successfully tagged quay.io/your-repo/myapp:v1

Run the container image

Run and Test your newly created Docker container:

docker run --rm -it -p 8080:8080 --name myapp quay.io/your-repo/myapp:v1
curl localhost:8080
Aloha from Spring Boot! 1 on 76851270a3e7
curl localhost:8080/sysresources
Memory: 1268 Cores: 3

These numbers are based on the memory and CPUs allocated to the Docker daemon as seen in the image below:

Docker Settings
Figure 2. Docker settings
curl localhost:8080/consume
Allocated about 80% (1.2 GiB) of the max allowed JVM memory size (1.2 GiB)

Stop & remove the Docker container:

control-c

Run your container with constrained resources

Now, constrain the resources associated with this Linux container

docker run --rm -it -p 8080:8080 -m 400m --cpus="1" --name myapp quay.io/your-repo/myapp:v1

Ask for the container’s resources:

curl localhost:8080/sysresources
Memory: 1268 Cores: 3

Crash it:

curl localhost:8080/consume

Fix memory problems

To correct this behavior use a different Dockerfile:

docker build -t quay.io/your-repo/myapp:v1 -f Dockerfile_Memory .

Now docker run it:

docker run --rm -it -p 8080:8080 -m 400m --cpus="1" --name myapp quay.io/your-repo/myapp:v1

And curl it:

curl localhost:8080/sysresources
Memory: 112 Cores: 3

And try to crash it:

curl localhost:8080/consume
Allocated about 80% (98.0 MiB) of the max allowed JVM memory size (112.0 MiB)

Once you are happy with your container image, push it up to your favorite registry:

docker login quay.io
docker push quay.io/your-repo/myapp:v1
.
.
.
20c527f217db: Pushed
61c06e07759a: Pushed
bcbe43405751: Pushed
e1df5dc88d2c: Pushed
v1: digest: sha256:d22d4af6e297a024b061dbaae05be76c771fdb1db51643dc2dd8b8e047f79647 size: 2630