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
1 |
$ brew cask install docker |
Linux systems
1 2 3 4 5 6 7 8 |
# install newest version curl -fsSL https://get.docker.com | bash # add current user to docker group so that it can execute docker commands sudo usermod -aG docker $USER # Start docker sudo systemctl start docker |
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.
Step 2: The “Helloworld.java“.
1 2 3 4 5 6 7 8 9 |
package com.myapp; class Helloworld { public static void main(String[] args) { System.out.println("Hello World"); } } |
Step 3: The “run.sh” to compile and run the “Helloworld”.
1 2 3 4 |
#!/bin/bash javac com/myapp/Helloworld.java java com.myapp.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”.
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 |
FROM alpine:3.6 MAINTAINER java-success.com # working directory in the container where Helloworld.java & run.sh copied to WORKDIR /app # run as a container root user USER root # install required packages RUN apk add --no-cache openssh openssl openjdk8 rsync bash procps nss # set JAVA_HOME ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk ENV PATH $PATH:$JAVA_HOME/bin # copy into the container working folder ADD com/myapp/Helloworld.java /app/com/myapp/Helloworld.java ADD run.sh /app/run.sh # run.sh +x permission RUN chmod -R 755 . # execute the run.sh bash file CMD ["/app/run.sh"] |
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.
1 |
~/projects/docker-hadoop]$ docker build -t java-success/simple-java:0.1 . |
“-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:
1 |
~/projects/docker-hadoop]$ docker images |
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
1 2 |
~/projects/docker-hadoop]$ docker run 0fe9cfb44ffd Hello World |
Image tag is used
1 2 |
~/projects/docker-hadoop]$ docker run java-success/simple-java:0.1 Hello World |
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.
1 |
~/projects/docker-hadoop]$ docker ps -a |
In the next tutorial we will inspect inside the Docker container.