DevOps Classroom notes 28/Jun/2025

Dockerfile

Asp.dotnet core application containerization

  • Refer Here for the repo
  • The Dockerfile only building the code is
From mcr.microsoft.com/dotnet/sdk:9.0 As build
Add . /nop
WORKDIR /nop
RUN dotnet build -c Release src/Presentation/Nop.Web/Nop.Web.csproj
  • Refer Here for the full version
  • Exercise: Ensure we have a user created in Dockerfile as of now we are running application as root user.

SHELL FORM and EXEC FORM

  • shell form
RUN apt update
CMD java -jar spc.jar
  • EXEC form
CMD ["java", "-jar", "spc.jar"]
  • SHell form any command is executed with /bin/sh -c <command> and this does not react well with signals sigint
  • Exec form directly executes the command which makes application PID-1 in a container and handles signals. You can expand variables in EXEC form
  • Use Shell form for RUN and EXEC form for ENTRYPOINT and CMD

Other Dockerfile instructions to consider

  • ENTRYPOINT/CMD
  • ARG
  • ENV

While building docker image i want to pass variables

  • Consider the following docker file
FROM openjdk:17
ADD https://referenceappslt.s3.ap-south-1.amazonaws.com/spring-petclinic-3.3.0-SNAPSHOT.jar /spring-petclinic-3.3.0-SNAPSHOT.jar
EXPOSE 8080
CMD java -jar /spring-petclinic-3.3.0-SNAPSHOT.jar
FROM openjdk:17
ARG JAR_URL='https://referenceappslt.s3.ap-south-1.amazonaws.com/spring-petclinic-3.3.0-SNAPSHOT.jar'
ADD ${JAR_URL} /spring-petclinic-3.3.0-SNAPSHOT.jar
EXPOSE 8080
CMD ["java", "-jar", "/spring-petclinic-3.3.0-SNAPSHOT.jar"]
  • build command
docker image build --build-arg JAR_URL='https://referenceappslt.s3.ap-south-1.amazonaws.com/spring-petclinic-3.3.0-SNAPSHOT.jar' -t spc:1 .
  • ARGs are accesible only while building image not while running container.

I Want to set values which i will be using in a container

  • Lets consider the following
FROM openjdk:17
ARG JAR_URL='https://referenceappslt.s3.ap-south-1.amazonaws.com/spring-petclinic-3.3.0-SNAPSHOT.jar'
ADD ${JAR_URL} /spring-petclinic-3.3.0-SNAPSHOT.jar
EXPOSE 8080
CMD ["java", "-jar", "/spring-petclinic-3.3.0-SNAPSHOT.jar"]
FROM openjdk:17
ARG JAR_URL='https://referenceappslt.s3.ap-south-1.amazonaws.com/spring-petclinic-3.3.0-SNAPSHOT.jar'
ADD ${JAR_URL} /spring-petclinic-3.3.0-SNAPSHOT.jar
ENV TEST='DEFAULT'
EXPOSE 8080
CMD ["java", "-jar", "/spring-petclinic-3.3.0-SNAPSHOT.jar"]

Preview

ENTRYPOINT and CMD

  • CMD can be overriten while running the docker by passing any thing after image name during run
docker run ..... <image> [------]
  • ENTRYPOINT can be change via –entrypoint during run
  • When you run a docker container it will run ENTRYPOINT + CMD as PID 1
  • Case 1: This will execute echo hello when container starts
FROM alpine
ENTRYPOINT [ "echo" ]
CMD ["hello"]
  • Case 2: Only entrypoint
FROM alpine
ENTRYPOINT [ "echo", "hello" ]
  • Case 3: Only CMD
FROM alpine
CMD [ "echo", "hello" ]
  • Case 4: NO CMD and NO Entrypoint the container will try to run base images Start command

Optimizations in Dockerfile

  • Storage
  • Security

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