Advanced docker build technique
Advanced usage of docker build
Usage of Dockerignore
- Basically,
docker buildloads all sub directories which cause heavy overhead.- If there are lots of sub directories,
buildneeds a lot of time to load all data. - We need to ignore unnecessary directories to reduce loading time.
- If there are lots of sub directories,
.dockerignoreprevents to load some files and directories.- Basically, we can describe original names of files or directories, but it supports some advanced description technique.
buildmeans a simple directory name form.*/temp*describes directories started withtempinside 1-level sub-directories.- For example,
log/tempwill be ignored from building context.
- For example,
*/*/temp*similarly describes directories started withtempinside 2-level sub-directories.temp?means all directories named by 5 characters started withtemp.- For example, there are
tempa,tempband so on.
- For example, there are
# comment
build
*/temp*
*/*/temp*
temp?
Separating building image from production image
- In Dockerfile script, I have described a way to build an image by
Dockerfile.- When building docker images, we uses unnecessary packages in production environment.
- Therefore, we need to remove the redundant files.
- We can separate production deployment images from build images.
- When building docker images, we uses unnecessary packages in production environment.
- I have named a building image to
buildbyas build.- Similarly, if you need lots of different building environments, you can define multiple environments named by different one.
- In building production image, I could have access to
buildimage by--from={{name}}.- At this, I checked that
golang:latestimage was based ondebian:buster, therefore I used the same image for production build. - As I’ve mentioned, we can copy files from multiple environments defined in advance.
- At this, I checked that
FROM golang:latest as build
ADD . /workdir
WORKDIR /workdir
RUN go build -o app
FROM debian/buster
WORKDIR /app
COPY --from=build /workdir/app /app/app
CMD ["/app/app"]