Docker :4

DevOps Classroom Series – 19/Mar/2021

Dockerfile

  • Docker can build images by reading instructions from a Dockerfile
  • Lets take one more application as an example
    • This application is based on java
    • This application runs on port 8080
  • Lets try to install this java application on a linux machine first
  • create a ubuntu linux vm on any cloud. login into linux machine using ssh
    • install java
    • download spring pet clinic application
sudo apt update
sudo apt install openjdk-8-jdk -y
java -version
wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
java -jar spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar

  • Now navigate to http://publicip:8080 Preview
  • To create a docker
    • We have to choose a right base image
      • You can choose ubuntu and perform all the steps
      • Choose a image where java is already installed
  • To build Dockerfile we will be using instructions Refer Here
  • So lets build a Dockerfile using ubuntu:18.04 image
    • To set the base image we will be using FROM instrution Refer Here
    • Now we need to divide our commands which we use to install/configure our application into two sections
      • application installation/configuration steps. All the configuration steps we will be using can be written in Dockerfile with RUN instruction Refer Here
      apt update apt install openjdk-8-jdk wget -y wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
      • application execution steps. Execution steps will be configured with instruction CMD Refer Here
      java -jar spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
      • Our application when executed might require some port to connect, in this case the port is 8080, we use EXPOSE instruction to specify ports of application Refer Here
  • Create a folder and then place the Dockerfile in it. Now build the docker image using docker image build command Refer Here
docker image build -t spc:ubuntu .

Preview
  • Now lets check the docker images in the local machine (local repository) docker image ls Refer Here Preview
  • Building a Image using openjdk image
  • To copy the jar file into container use the ADD instruction Refer Here
  • Lets try to build the docker image
docker image build -t spc:openjdk .

Preview
  • Lets view the docker image in the local registry Preview
  • Now lets try to run the application in a docker container using two images which we have built Preview Preview Preview
  • To build a docker image for an app we will have multiple approaches, try to use the approach which is simpler and which results in consuming less resources
  • Refer Here for the dockerfiles used in the class room
  • Refer Here for the slim version of spc