DevOps Classroom Series – 01/Nov/2020

Consideration for building Docker image for any application

  • Right base image
  • Steps for installing and configuring your application
  • Expressing details about the ports on which your application works
  • Commands to start your application.

Docker images in depth

  • Docker image is collection of layers.
  • Theroitically each layer is result of the change (RUN, COPY, ADD) will lead to creation of image layers
  • Lets take the openjdk image docker image pull openjdk:8 and execute docker image inspect openjdk:8 Preview
  • Lets try to create our new docker image with openjdk:8 as base image
FROM openjdk:8

Preview

  • Now lets inspect myjdk:8 and look into layers Preview
  • myjdk:8 is exactly the collection of 7 image layers which openjdk:8 has
  • Lets add some stuff to this image
FROM openjdk:8
RUN wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar

Preview

  • now lets inspect myjdk:8.1 for image layers Preview
  • myjdk 8.1 add approx 40 mb of file to existing openjdk:8 in the new layer added Preview
  • If the image layer which is used by new image which you are pull already exists docker image will not download it again Preview
  • Docker images can be built from scratch Refer Here
  • docker image is collection of image layers Preview
  • Then what will container have one extra read write layer for the changes Preview

Dockerfile IDE

  • For developing Dockerfile any text editor can be used.
  • In this series i will be using visual studio code + Docker extension from Microsoft added Preview

Dockerfile instructions

  • Refer Here for the Dockerfile official docs

  • FROM: Using from instruction we select baseimage, always choose a base image with some tag not latest Refer Here

  • ADD and COPY: Are used to copy the contents into the docker image. ADD instruction can download the content from internet also (URI/URL).

  • EXPOSE: instruction lets Docker know that when the image is execute, the port and protocol defined will be exposed at runtime Refer Here

  • Refer Here for the sample used

  • ENTRYPOINT and CMD:

Docker container creation

  • when ever you want to create a container execute run command
docker container run <image>:<tag>
  • The entrypoint/cmd gets executed and as long as that command is executing container will be alive
  • Refer Here for the sample docker image Preview
  • Now lets run this image and check for the containers Preview
  • Lets look at all the container status Preview
  • docker container has executed echo hello-world which completed its activity & now docker engine since the command execution is completed stopped the container.
  • Now lets run the spc:trail1 with different cmd options Preview
  • Whatever command which is passed after image:tag in docker container run will replace CMD instruction
  • Refer Here for the changes done
  • Build the docker image Preview Preview Preview
  • In Docker entrypoint and CMD instruction can be written in two formats
  • Docker container running modes:
    • attached: This is default mode
    • detached/background: in this mode your container will just show the container id and run in background
    docker container run -d <image>:<tag>
    
    • interactive: In this mode we login into the terminal of container
    docker container run -it <image>:<tag> <terminalpath>
    
    Preview
  • Docker container is a process isolation.
    • It gets cpu and memory
    • It gets storage (mount of image layers+read write layer)
    • It gets network interface
  • Network port mapping b/w docker host and containers Preview
  • Exercise: Try building a docker image for

Leave a Reply

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

About learningthoughtsadmin