DockerFile
Estimated reading: 4 minutes 41 views

πŸ“˜ Dockerfile Directives – FROM, RUN, CMD, COPY, and More

🧲 Introduction – Why Learn Dockerfile Directives?

Every Dockerfile is built from a series of directivesβ€”simple instructions that define how your Docker image should be created. Mastering these directives allows you to write efficient, reliable, and secure Dockerfiles.

Whether you’re setting a base image, copying files, running commands, or exposing ports, each directive plays a vital role in shaping your container environment.


πŸ“‹ Topics Covered

🧩 DirectiveπŸ“ Description
FROMSets the base image for your Dockerfile
MAINTAINERSpecifies the author of the Dockerfile (deprecated)
RUNExecutes commands in the image at build time
CMDSets the default command when the container starts
ENTRYPOINTDefines a fixed command to run
COPY / ADDTransfers files into the image
ENVSets environment variables
EXPOSEInforms Docker which ports should be opened
VOLUMEDefines mount points for persistent or shared data
WORKDIRSets the working directory for RUN, CMD, etc.
USERSpecifies the user to run commands inside the container

πŸ”Ή FROM

Sets the base image for the container.

πŸ“Œ Example:

FROM ubuntu:22.04

You must always start a Dockerfile with the FROM directive.


πŸ§‘β€πŸ’Ό MAINTAINER (Deprecated)

Used to specify the Dockerfile author. Replaced by LABEL.

πŸ“Œ Example:

MAINTAINER Jane Doe <jane@example.com>

βœ… Use LABEL instead:

LABEL maintainer="Jane Doe <jane@example.com>"

βš™οΈ RUN

Executes shell commands when building the image.

πŸ“Œ Example:

RUN apt update && apt install -y curl

πŸ” Use RUN to install packages or configure the OS.


🏁 CMD

Defines the default runtime command. Only the last CMD is used.

πŸ“Œ Example:

CMD ["npm", "start"]

Use CMD to provide default arguments to ENTRYPOINT or as the main command.


🧩 ENTRYPOINT

Specifies the main process that always runs.

πŸ“Œ Example:

ENTRYPOINT ["python3", "app.py"]

βœ… Can be combined with CMD:

ENTRYPOINT ["python3"]
CMD ["app.py"]

πŸ“‚ COPY / ADD

Transfer files or directories from host to container.

πŸ“Œ COPY:

COPY index.html /usr/share/nginx/html

πŸ“Œ ADD:

ADD archive.tar.gz /data/

πŸ” ADD supports archives and URLs; COPY is preferred for simplicity.


🌐 ENV

Sets environment variables inside the image.

πŸ“Œ Example:

ENV NODE_ENV=production

These values are available during the build and runtime.


πŸ“‘ EXPOSE

Informs Docker that the container will listen on specific ports.

πŸ“Œ Example:

EXPOSE 80

Note: This doesn’t actually publish the port β€” it’s metadata.


πŸ’Ύ VOLUME

Creates a mount point for persistent storage.

πŸ“Œ Example:

VOLUME ["/data"]

Allows containers to share or persist data.


πŸ“ WORKDIR

Sets the working directory for RUN, CMD, and ENTRYPOINT.

πŸ“Œ Example:

WORKDIR /app

Avoids repeatedly using cd in scripts.


πŸ‘€ USER

Specifies the username or UID to run the container.

πŸ“Œ Example:

USER node

Improves security by avoiding the root user.


πŸ“Œ Summary – Recap & Takeaways

Dockerfile directives give you full control over how your image is built and how the resulting container behaves.

πŸ” Key Takeaways:

  • Use FROM to set a reliable base.
  • Replace MAINTAINER with LABEL.
  • Combine RUN layers efficiently.
  • Use CMD and ENTRYPOINT appropriately.
  • COPY is safer than ADD for local file transfers.
  • Secure your image with USER and organize it with WORKDIR.

βš™οΈ Understanding these directives is essential for optimizing builds, reducing image size, and improving security in Docker containers.


❓ Frequently Asked Questions (FAQs)

❓ What’s the difference between CMD and ENTRYPOINT?
βœ… CMD sets a default command. ENTRYPOINT defines a fixed entry process. Use both together for flexibility.


❓ Should I use COPY or ADD?
βœ… Prefer COPY. Use ADD only when you need to extract archives or fetch URLs.


❓ How do I switch users in a Dockerfile?
βœ… Use the USER directive. Example: USER node


❓ Is MAINTAINER still valid?
βœ… It works but is deprecated. Use LABEL instead.


❓ Can I set multiple CMDs or ENTRYPOINTs?
βœ… Only the last CMD or ENTRYPOINT is respected. Overwrites previous ones.


❓ What is the difference between WORKDIR and RUN cd?
βœ… WORKDIR persists across layers. RUN cd affects only the current layer.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Common DockerFile directives:

Or Copy Link

CONTENTS
Scroll to Top