Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
Docker Docker & Containers: From Zero to Production Docker Images Dockerfile Module 2 — Docker Images

Dockerfile Instructions — FROM, RUN, COPY, ADD, and More

Reviewed & accurate
AI Summary

What You'll Learn

Beginner

  • Every Dockerfile instruction and when to use it
  • The difference between CMD and ENTRYPOINT
  • COPY vs ADD — which to use when

Complete Instruction Reference

InstructionPurposeLayer?
FROMBase imageYes
RUNExecute during buildYes
COPYCopy files from hostYes
ADDLike COPY + extract tarballsYes
WORKDIRSet working directoryNo
ENVSet environment variableNo
ARGBuild-time variableNo
EXPOSEDocument portNo
CMDDefault commandNo
ENTRYPOINTFixed commandNo

COPY vs ADD

Rule of thumb
Use COPY for everything. Only use ADD when you need to extract a tarball.
FeatureCOPYADD
Copy local filesYesYes
Extract tarballsNoYes
Recommended?YesAvoid

CMD vs ENTRYPOINT

Most confused topic
Both define what runs when the container starts. CMD is overridable; ENTRYPOINT is fixed.
AspectCMDENTRYPOINT
PurposeDefault command + argsFixed command
docker run image argReplaces CMDAppends to ENTRYPOINT
Best forGeneric imagesSpecific apps

The best pattern: ENTRYPOINT + CMD

Dockerfiledockerfile
ENTRYPOINT ["node"]
CMD ["server.js"]

# docker run myapp          -> node server.js
# docker run myapp test.js  -> node test.js

USER — Run as Non-Root

Security best practice
By default, containers run as root. Create a non-root user and switch to it.
Dockerfiledockerfile
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser

Practical Exercise

Review the Dockerfile instructions table
Write a Dockerfile with FROM, COPY, RUN, CMD
Test CMD vs ENTRYPOINT behavior
Add a non-root USER

Key Takeaways

  • FROM sets the base. RUN executes during build. COPY adds files.
  • Use COPY over ADD.
  • ENV persists at runtime; ARG is build-time only.
  • CMD is overridable; ENTRYPOINT is fixed. Use both for flexibility.
  • Always run as non-root with USER.
Previously: Lesson 11 built your first image.
Today: You learned: Every Dockerfile instruction explained with examples.
Next: Lesson 13 covers best practices.
Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments