Pre-requisite: Docker is installed on your machine for Mac OS X (E.g. $ brew cask install docker) or Windows 10. Docker interview Q&As.
Step 1: Create a Java project “docker-test” with “HelloDocker.java” file under “src/main/java” in a package named “com/mypkg”, and “pom.xml” in the base project folder. Using the Visual Studio Code editor.
HelloDocker.java
|
1 2 3 4 5 6 7 8 |
package com.mypkg; public class HelloDocker { public static void main(String[] args) throws Exception{ System.out.println("Hello Docker"); } } |
pom.xml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.myproject</groupId> <artifactId>hellodocker</artifactId> <version>1.0</version> <build> <plugins> <plugin> <!-- Build an executable JAR --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.mypkg.HelloDocker</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project> |
Step 2: Create a “Dockerfile” in the project base directory.
Dockerfile
|
1 2 3 4 5 6 7 |
FROM maven:3.5-jdk-8-alpine MAINTAINER java-success.com WORKDIR /app CMD ["/bin/sh"] |
Step 3: Docker has two steps. Firstly, “build” an image based on the “Dockerfile” and then “run” the container, which is an instance of the image.
From the docker-test folder build the image
it uses the “Dockerfile” in the “.” (i.e. current directory)
|
1 2 |
$ docker build --tag=docker-test . |
once the docker image is built, you can list it with
|
1 2 |
$ docker images |
|
1 2 3 |
REPOSITORY TAG IMAGE ID CREATED SIZE docker-test latest 25a4f7356b10 Less than a second ago 119MB |
It has built an image of size 119MB. The image name will be “docker-test:latest”
You can remove an image with “docker rmi 25a4f7356b10”.
From the docker-test create a container, which is an instance of the image
|
1 2 |
$ docker run --rm -it -v "$(pwd):/app:consistent" docker-test:latest |
Present working directory is mapped to Docker container’s “/app” and synchronised.
Now, you are in the Docker container prompt
|
1 2 |
/app # |
Step 4: You can run “mvn package” to build the jar file “hellodocker-1.0.jar” in “target” folder.
|
1 2 |
/app # mvn -Dmaven.repo.local="/app/mvn-repository" package |
After packaging, there will be a target folder with “hellodocker-1.0.jar” file.
“-v “$(pwd):/app:consistent” keeps the folders & files in the Docker container in the “/app” folder in sync with the current working directory “docker-test” shown above.
Step 5: Run the “hellodocker-1.0.jar”
|
1 2 |
/app # java -jar target/hellodocker-1.0.jar |
Output:
|
1 2 |
Hello Docker |
You could also have directly run
Create an image
|
1 2 |
docker run --rm -it -v "$(pwd):/app:consistent" docker-test:latest mvn package |
Create a container, which is an instance of the image
|
1 2 |
docker run --rm -it -v "$(pwd):/app:consistent" docker-test:latest java -jar target/hellodocker-1.0.jar |
Notes: Look at the “Docker hub” for the images that you can use.
GNU make file to automate docker runs
Assumes that GNU Make is installed on your machine.
Step 6: Create a “Makefile” in the base project folder.
Makefile
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
help: @cat $(MAKEFILE_LIST) | grep -e "^[a-zA-Z_\-]*: *.*" docker-build: docker build --tag=docker-test . docker-run: docker-build docker run --rm -it -v "$(pwd):/app:consistent" docker-test:latest app-run: docker-build docker run --rm -it -v "$(shell pwd):/app:consistent" docker-test:latest \ mvn -Dmaven.repo.local="/app/mvn-repository" package && \ java -jar target/hellodocker-1.0.jar |
You can run the commands like:
|
1 2 3 4 |
$ make $ make docker-run $ make app-run |
If you want to run the app, just execute
|
1 2 |
$ make app-run |


