01: Docker Tutorial – compile & run Helloworld.java

Docker, Jenkins, CI/CD, & DEV/OPS have been the buzz words for the last several years. You will be seeing this in more often in the job requirements as well. 21 Docker interview questions & answers | DevOps & CI/CD Interview Q&As

Pre-requisite: Docker is installed on your machine for example:

Mac OS X

Linux systems

Windows10

Step 1: Create a folder “docker-hadoop” under say “projects” folder with “Helloworld.java” file under folder “com/myapp”. Create Dockerfile & run.sh under “docker-hadoop“. I am using the Visual Studio Code editor. We will be compiling and running the “Helloworld” application inside the Docker container.

docker-tutorial

docker-tutorial

Step 2: The “Helloworld.java“.

Step 3: The “run.sh” to compile and run the “Helloworld”.

Step 4: Finally, the “Dockerfile” to compile & run the application isolated in a container.

Alpine is a Linux distribution that is smaller and more resource efficient than traditional GNU/Linux distributions. The Docker container image is built based on the below Dockerfile. The OS is Alpine Linux, and then install the following packages – “openssh, openssl, openjdk8, rsync, bash, procps, and nss”.

Step 5: Build the image. You can build the image via “docker” on a command line. If you are using “Visual Source Code” editor open a new terminal window via “Terminal -> New Terminal”. You will be running it from the “~/projects/docker-hadoop” folder where the “Dockerfile” is located.

-t” is for naming or tagging the image as “java-success/simple-java:0.1”, and “.” is to read the Dockerfile from the current folder.

This will build an image by downloading the relevant packages like “openjdk8”. You can now list the images with:

Docker image

Docker image

Step 6: Once the image is built, you can run the image where a container will be created and the “run.sh” will be executed inside the container to output “Hello World”. The last line of the Dockerfile is “CMD [“/app/run.sh”]”

Image id is used

Image tag is used

run.bat executed within a Docker container

run.bat executed within a Docker container

Note: Docker containers are designed to shut down immediately after initial CMD or ENTRYPOINT command is run.

You can run the following command to list the terminated containers using the option “-a” or “–all”. Run without the option “-a” to list only the running containers.

In the next tutorial we will inspect inside the Docker container.

(Visited 4 times, 1 visits today)

800+ Java & Big Data Interview Q&As

200+ Java & Big Data Tutorials

Top