Dockerfile Instructions

Main Instructions

InstructionPurpose
FROMbase image; every Dockerfile starts with it
WORKDIRworking directory for the following instructions
COPYcopy files from the build context into the image
RUNrun a command at build time (install packages)
ENVset an environment variable (build and run time)
ARGa build-time variable (--build-arg)
EXPOSEdocuments the port the app listens on
USERuser the container runs as
CMDdefault command when the container starts
ENTRYPOINTfixed executable; CMD then supplies default arguments
HEALTHCHECKcommand Docker runs to check the app is healthy

RUN vs CMD vs ENTRYPOINT

  • RUN executes while building and its result is saved in the image.
  • CMD is the default command at start-up; docker run image other-cmd replaces it.
  • ENTRYPOINT is always run; arguments from CMD or docker run are appended.
ENTRYPOINT ["python", "manage.py"]
CMD ["runserver", "0.0.0.0:8000"]
# docker run app            -> python manage.py runserver 0.0.0.0:8000
# docker run app migrate    -> python manage.py migrate

Tip: Use the JSON (exec) form CMD ["a", "b"]: the process runs as PID 1 and receives stop signals, so containers shut down gracefully.