2. Docker build
Builds Docker images from a Dockerfile and a “context”.
A build’s context is the files located in the specified PATH or URL.
The build process can refer to any of the files in the context.
Each directive in Dockerfile creates a layer.
Each Dockerfile generates on image.
3. Docker image build best practices
Avoid installing unnecessary packages
Each container should have only one concern
Minimize the number of layers
Minimize the size of the image
4. Packaging an application in docker
image
application
compiler
test code
build /test logs/reports
application
5. Docker Builder pattern
Build in multiple stages
2 Docker files
1 – Build environment
2 – Run time environment
Work flow
Doesn’t run everywhere
6. Docker multi stage builds
Available from Docker 17.05.
Extended FROM and COPY directives.
Supports Multiple FROM directives.
Each FROM statement is start of a new context.
COPY from any previous stage to the current context.
The last FROM directive creates the final image.
The FROM syntax also supports AS keyword
7. Multistage build exampleFROM node:latest AS storefront
WORKDIR /usr/src/atsea/app/react-app
COPY react-app .
RUN npm install
RUN npm run build
FROM maven:latest AS appserver
WORKDIR /usr/src/atsea
COPY pom.xml .
RUN mvn -B -f pom.xml -s /usr/share/maven/ref/settings-docker.xml dependency:resolve
COPY . .
RUN mvn -B -s /usr/share/maven/ref/settings-docker.xml package -DskipTests
FROM java:8-jdk-alpine
RUN adduser -Dh /home/gordon gordon
WORKDIR /static
COPY --from=storefront /usr/src/atsea/app/react-app/build/ .
WORKDIR /app
COPY --from=appserver /usr/src/atsea/target/AtSea-0.0.1-SNAPSHOT.jar .
ENTRYPOINT ["java", "-jar", "/app/AtSea-0.0.1-SNAPSHOT.jar"]
CMD ["--spring.profiles.active=postgres"]
https://github.com/dockersamples/atsea-sample-shop-app/blob/master/app/Dockerfile