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
RUN apt update
CMD java -jar spc.jar
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
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"]
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"]

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"]
FROM alpine
ENTRYPOINT [ "echo", "hello" ]
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
Like this:
Like Loading...