Optimizing Images
Simple Fastapi application
- Refer Here for steps
- Required:
- Steps:
- copy code
- install dependencies
- Expose port
- How to run Appl
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
- Refer Here for first version of Dockerfile
- Refer Here for change to slim version
- Refer Here for changes to use a non root user
- When we tried as a non root user, we are getting permission issues
- Refer Here for the possible solution where we build dependencies in one stage and copy them into other with permissions to the user
Simple Spring boot (Java) application
FROM eclipse-temurin:17-jre
ADD https://khajareferenceapps.s3.ap-south-1.amazonaws.com/spring-petclinic-3.2.0-SNAPSHOT.jar /spc.jar
EXPOSE 8080
CMD ["java", "-jar", "/spc.jar"]
- We are building the jar file seperately and using them in Docker Image building process.
- The idea is to build the jar and use the jar as part of Docker Image building itself
Multi stage Dockerfiles
- This is inspired from a builder pattern
- Refer Here for multistage docker for spring petclinic
FROM maven:3.9-eclipse-temurin-17 AS builder
# build the java code
COPY . /spc
WORKDIR /spc
RUN mvn package
# this will create a spring petclinic jar file
FROM eclipse-temurin:17-jre AS runner
COPY --from=builder --chown=ubuntu /spc/target/spring-petclinic-3.3.0-SNAPSHOT.jar /app/spring-petclinic.jar
USER ubuntu
WORKDIR /app
EXPOSE 8080
CMD ["java", "-jar", "spring-petclinic.jar"]