Docker
1 Create Dockerfiles
To build Docker images, you first need a Dockerfile. A Dockerfile is a plain text file named Dockerfile and has no file extension.
FROM <baseimage> # get them from https://hub.docker.com
# set the working directory inside the image
WORKDIR /app
# copy the source code from the host machine to the image
# (host: your development machine or your server)
# the first dot (.) is the current working directory (CWD)
# of the host, the second dot (.) is the CWD of the image
COPY . .
# run any shell command inside the image
RUN <command> # e.g., [npm run build]
# does not really expose the port 80 (only documentation)
# so that we know which port to expose
EXPOSE 80 # we expose ports with the --publish flag
# this command gets executed when the container starts
CMD <command> # e.g., ["node”, “server.js”]
# end
Multistage Dockerfiles are used to optimize Dockerfiles. One use case is to create a builder and a serve stage with separate base images. This strategy can make the final image smaller and have a lower attack because it has fewer system libraries. Each stage starts with FROM.
# with as, you can give the current stage a variable name
FROM <baseimage> as builder
# now do something, install dependencies, build your code
RUN <command> # e.g., g++ main.cpp
# the second stage could use a smaller image
# small images are based on alpine or you can build
# FROM scratch (this is a Docker image)
# if you do not need any system libraries
FROM scratch as serve
# you can now copy files from the builder stage
# e.g., the binary file that you build in that stage
COPY --from=builder ./hello-world ./hello-world
# this command gets executed when the container starts
CMD ["./hello-world”]
# end