Codementor Events

Dockerfile #instructions

Published Feb 11, 2021Last updated Feb 17, 2021
Dockerfile #instructions

Instructions

What is Dockerfile

Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession

https://docs.docker.com/engine/reference/builder/

Instructions to write a Dockerfile

FROM --> Sets the Base Image for subsequent instructions.
MAINTAINER --> (deprecated - use LABEL instead) Set the Author field of the generated images.
RUN --> execute any commands in a new layer on top of the current image and commit the results.
CMD --> provide defaults for an executing container.
EXPOSE --> informs Docker that the container listens on the specified network ports at runtime. NOTE: does not actually make ports accessible.
ENV --> sets environment variable.
ADD --> copies new files, directories or remote file to container. Invalidates caches. Avoid ADD and use COPY instead.
COPY --> copies new files or directories to container. By default this copies as root regardless of the USER/WORKDIR settings.(Same for ADD.)
ENTRYPOINT --> configures a container that will run as an executable.
VOLUME --> creates a mount point for externally mounted volumes or other containers.
USER --> sets the user name for following RUN / CMD / ENTRYPOINT commands.
WORKDIR --> sets the working directory.
ARG --> defines a build-time variable.
ONBUILD --> adds a trigger instruction when the image is used as the base for another build.
STOPSIGNAL --> sets the system call signal that will be sent to the container to exit.
LABEL --> apply key/value metadata to your images, containers, or daemons.
SHELL --> override default shell is used by docker to run commands.
HEALTHCHECK --> tells docker how to test a container to check that it is still working.

Build your own docker image:

example : Dockerfile for GO application looks like:

FROM golang:1.11-alpine AS build

Note:Install tools required for project
Note: Run docker build --no-cache . to update dependencies
RUN apk add --no-cache git
RUN go get github.com/golang/dep/cmd/dep

Note: List project dependencies with Gopkg.toml and Gopkg.lock
Note: These layers are only re-built when Gopkg files are updated
COPY Gopkg.lock Gopkg.toml /go/src/project/
WORKDIR /go/src/project/
Note: Install library dependencies
RUN dep ensure -vendor-only

Note:Copy the entire project and build it
Note:This layer is rebuilt when a file changes in the project directory
COPY . /go/src/project/
RUN go build -o /bin/project

Note: This results in a single layer image

FROM scratch
COPY --from=build /bin/project /bin/project
ENTRYPOINT ["/bin/project"]
CMD ["--help"]

                                                                     Thanks
                                                                     NK
Discover and read more posts from Naveen
get started
post commentsBe the first to share your opinion
Show more replies