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
| Instruction | Purpose | Layer? |
|---|---|---|
FROM | Base image | Yes |
RUN | Execute during build | Yes |
COPY | Copy files from host | Yes |
ADD | Like COPY + extract tarballs | Yes |
WORKDIR | Set working directory | No |
ENV | Set environment variable | No |
ARG | Build-time variable | No |
EXPOSE | Document port | No |
CMD | Default command | No |
ENTRYPOINT | Fixed command | No |
COPY vs ADD
Rule of thumb
Use COPY for everything. Only use ADD when you need to extract a tarball.| Feature | COPY | ADD |
|---|---|---|
| Copy local files | Yes | Yes |
| Extract tarballs | No | Yes |
| Recommended? | Yes | Avoid |
CMD vs ENTRYPOINT
Most confused topic
Both define what runs when the container starts. CMD is overridable; ENTRYPOINT is fixed.| Aspect | CMD | ENTRYPOINT |
|---|---|---|
| Purpose | Default command + args | Fixed command |
docker run image arg | Replaces CMD | Appends to ENTRYPOINT |
| Best for | Generic images | Specific 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.jsUSER — 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 appuserPractical 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
FROMsets the base.RUNexecutes during build.COPYadds files.- Use
COPYoverADD. ENVpersists at runtime;ARGis build-time only.CMDis overridable;ENTRYPOINTis fixed. Use both for flexibility.- Always run as non-root with
USER.
Comments
Comments
Post a Comment