DevOps Classroom notes 06/Jan/2024

Containerization

  • To containerize the application, we need to create the docker image of the application.
  • To create docker image of the application (manual steps of running application are required), we have two ways
    • create image from running container:
      • should not be adopted
    • Write Dockerfile.
      • Preferred approach
      • repeatable, automatable

First sample Application

  • A custom home page on a webserver
  • manual steps:
    • install webserver (nginx or apache)
    • custom homepage <h1> This is my home page </h1>
  • For an ubuntu distribution
sudo apt update && sudo apt install apache2 -y
echo "<h1> This is my home page </h1>" > /var/www/html/index.html
  • For an redhat distribution
sudo dnf update && sudo dnf install httpd -y
sudo systemctl enable httpd.service
sudo systemctl start httpd.service
echo "<h1> This is my home page </h1>" > /var/www/html/index.html
Manual approach using containers
  • To create a Docker image find the suitable docker image to start with.
  • Docker has a image called as scratch which has nothing.
  • Lets take a image called as httpd
  • To create a container and login into container during start
docker container run -d -P --name mycontainer -it httpd /bin/bash
  • To login into running container
docker container exec -it <name/id> /bin/bash
  • Lets create a container and then login interactively
# creates the container and runs in the background
docker container run -d -P --name mycontainer httpd
  • Make the changes echo "<h1> This is my home page </h1>" > /usr/local/apache2/htdocs/index.html
  • Now create a image from container
docker container commit mycontainer firstimage
  • Then to test create a container from first image
docker container run -d -P --name myfirstcontainer firstimage
  • This is not recommended as it is not suitable for automation.
  • To remove all the containers
# Get all container ids docker container ls -a -q
# pass this to rm -f
docker container rm -f $(docker container ls -a -q)
  • To remove all the images
docker image rm $(docker image ls -q)
Dockerfile instruction approach
  • We need to create a file filled with instructions for docker to create image
  • Dockerfile can be any text but generally Dockerfile is used.
  • This file has set of instructions
<INSTRUCTION> <VALUE>
  • Some of the instructions are

    • FROM
    • RUN
    • EXPOSE
    • CMD
    • ENTRYPOINT
    • LABEL
    • ADD
    • COPY
    • VOLUME
  • For the above appliation the Dockerfile
FROM httpd
EXPOSE 80
RUN echo "<h1> This is my home page </h1>" >  /usr/local/apache2/htdocs/index.html
  • Instruction categories
    • Building
      • FROM
      • ADD
      • COPY
      • ARG
    • METADATA
      • LABEL
      • EXPOSE
    • RUNNING
      • CMD
      • ENTRYPOINT
FROM: Choosing the base image
  • Docker image needs a base image and this is specified by using FROM Refer Here
  • Never use latest tag
  • Generally dont use images with hefty sizes in linux.
RUN: Executing a command during image build
  • Refer Here for official docs
  • Lets download jar file in the docker image with java 17 installed
  • jar file location Refer Here
  • Base image: FROM amazoncorretto:17-alpine3.17-jdk
FROM amazoncorretto:17-alpine3.17-jdk
RUN wget https://referenceapplicationskhaja.s3.us-west-2.amazonaws.com/spring-petclinic-3.1.0-SNAPSHOT.jar

LABEL: Add metadata
  • Refer Here for official docs
  • Lets add author name and projects name

EXPOSE

  • This instruction exposes ports for tcp or udp Refer Here
  • If you dont mention EXPOSE -P will not work
CMD: command to start the application
  • CMD is command executed when container is starting.
  • Container will be in running state as long as command in CMD is running.
  • CMD should have a command which starts your application and doesn’t exist till your application is executing
  • If CMD is not written then the base images CMD is considered.

Spring petclinic

  • Dockerfile
FROM amazoncorretto:17-alpine3.17-jdk
LABEL author="khaja" 
LABEL project="learning"
RUN wget https://referenceapplicationskhaja.s3.us-west-2.amazonaws.com/spring-petclinic-3.1.0-SNAPSHOT.jar
EXPOSE 8080
CMD ["java", "-jar", "spring-petclinic-3.1.0-SNAPSHOT.jar"]

Preview
Preview
Preview

References

  • Install Docker Extension into visual studio code
    Preview

Exercise

  • Refer Here for war file
  • This requires tomcat 8 or 9 with java 8
  • Ensure you are downloading gameoflife.war in webapps folder

Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube