DevOps Classroom Series – 12/Jun/2020

Docker Image building using Dockerfile

  • Docker images are built using Dockerfile. Official reference of Dockerfile
  • Consider this Dockerfile
FROM openjdk:8
RUN wget
CMD ["java",  "-jar",  "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
  • When we create a image using Dockerfile docker image build -t hellodocker2 .

    1. A container using the base image gets created
      • container with image openjdk:8 gets created
    2. Then some instructions such as RUN etc gets executed
    3. Copy of this container is created as a Docker image
      • Image is created with the name specified in the command
  • When we run a container using our created image (hellodocker2), then ENTRYPOINT/CMD gets executed

  • While building a docker image divide activities into following categories

    1. Base image with necessary software (if available)
    2. execute steps for configuring/deploying application
    3. steps for running your application
  • docker image build builds an image from Dockerfile and set of files specified in the PATH (Local directory) or URL (GIT Repository Location) which is called as context docker image build -t hellodocker <PATH or URL>

  • Dockerfile is a standard name but if you want to write instruction in a different file that is possible docker image build -t <imagetag> -f <path to Dockerfile>

Docker image conventions

  • Docker image naming convention

openjdk  => openjdk:latest

  • Default tag is latest Preview Preview Preview Preview
  • Docker pull is downloading the image from docker public registry to local
docker image ls
docker image pull jenkins
docker image ls
docker image pull jenkins:alpine
docker image ls



  • Docker file is sequence of instructions in the format of
INSTRUCTION  arguments
  • Popular Instructions available are
    • FROM
      • This instructuion sets the base image
      • Syntax
      FROM <image>[:tag] [AS <name>]
      • Examples:
      FROM ubuntu
      FROM openjdk:8
      • Best practice: always mention tag donot use latest
      • also gives and optional way to specify platforms lick linux/amd64, linux/arm64, window/arm64 etc
      FROM [--platform=<platform>] <image>[:tag] [AS <name>]
    • RUN:
      • This instruction execute the commands in the created base image container in a new layer
      • Run command runs with /bin/bash -c on Linux and cmd on windows
      • Syntax:
      RUN <command>
      RUN ["executable", "param1", "param2"]
      • Example:
      RUN wget
      RUN /bin/bash -c 'echo sample'
      RUN ["/bin/bash", "-c", "echo sample"]
      • Commands have two forms
        • shell =>
        /bin/bash -c 'echo sample'
        java -jar spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
        • exec => represent in square brackets
        ["/bin/bash", "-c", "echo sample"]
        ["java",  "-jar",  "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
      • This is the command that gets executed when starting the container. if ENTRYPOINT is not found then CMD is executed when starting the container
      • Syntax:
      ENTRYPOINT ["executable", "param-1", "param-2" ]
      ENTRYPOINT executable  param-1 param-2
      • If both ENTRYPOINT and CMD Exists then ENTRYPOINT will be executable and CMD will be arguments
    • CMD
      • Syntax
      CMD ["executable", "param-1", "param-2" ]
      CMD executable  param-1 param-2
      CMD param-1 param-2


  1. Create a image with tag testing:1.0 with the following Dockerfile
FROM openjdk:8
RUN wget
ENTRYPOINT ["java",  "-jar",  "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
  1. Create a image with tag testing:2.0 with the following Dockerfile
FROM openjdk:8
RUN wget
CMD ["java",  "-jar",  "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
  1. Create a image with tag testing:2.0 with the following Dockerfile
FROM openjdk:8
RUN wget
CMD ["-jar",  "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]

  1. Execute docker image ls Preview
  2. Now execute the following commands
docker container run -P -d testing:1.0
docker container run -P -d testing:2.0
docker container run -P -d testing:3.0
docker container ls
  1. To the container run command we can pass arguments Preview
  2. ENTRYPOINT cannot be changed with args but whatever we write in CMD can be overwritten with args Preview
# no impact on application startup
docker container run -P testing:1.0 echo helloworld

# startup is changed
docker container run -P testing:2.0 echo helloworld

# startup is notchanged but the arguments are changed
docker container run -P testing:3.0 echo helloworld

Preview Preview Preview

  • Next Instructions



  • ADD



  • USER

  • ARG





  • ENV

Leave a Reply

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

About learningthoughtsadmin