ПІДТРИМАЙ УКРАЇНУ ПІДТРИМАТИ АРМІЮ
Uk Uk

Mastering Dockerfile: Key Instructions Explained in Simple Terms

Mastering Dockerfile: Key Instructions Explained in Simple Terms

If you're working with Docker, mastering Dockerfiles is essential! A Dockerfile is a script that...

If you're working with Docker, mastering Dockerfiles is essential! ADockerfileis a script that defines how your Docker image is built. In this post, I'll break downessential Dockerfile instructionswith examples, comparisons, and best practices. ????

1️⃣ FROM – Define Base Image

Every Dockerfilemuststart with FROM , which specifies the base image.

Example:

FROM node:18

Best Practice:Useslimoralpineversions for smaller images:

FROM node:18-slim

✅Reduces image sizeand improves security.

2️⃣ WORKDIR – Set Working Directory

Defines where the app runs inside the container.

Example:

WORKDIR /app

Why use it?

✔️ Avoids cd commands.

✔️ Keeps the directory structure clean.

3️⃣ COPY vs ADD – Copying Files

Both copy files into the container, butADDcan also extract archives.

Instruction Function Extracts .tar.gz? Supports URLs?
COPY Copies files/folders ❌ No ❌ No
ADD Copies + extracts files ✅ Yes ✅ Yes

Example:

COPY package.json .
ADD my-archive.tar.gz /data/

✅Best Practice:Use COPY unless youneed extraction.

4️⃣ RUN – Execute Commands During Build

Used to install dependencies or configure the container.

Example:

RUN apt-get update && apt-get install -y curl

✅Best Practice:Use && to reduce layers.

5️⃣ CMD vs ENTRYPOINT – What’s the Difference?

These define how thecontainer starts, but they work differently.

Instruction Purpose Can Be Overridden?
CMD Default command ✅ Yes
ENTRYPOINT Fixed command ❌ No

CMD Example:

CMD ["node", "app.js"]

ENTRYPOINT Example:

ENTRYPOINT ["java", "-jar", "app.jar"]

✅Use CMDif users might override the command.

✅Use ENTRYPOINTfor fixed commands like Docker CLI tools.

6️⃣ EXPOSE – Define Port

Tells Docker which port the app will use.

Example:

EXPOSE 3000

Note:EXPOSEdoes notactually open the port! You still need -p when running the container.

docker run -p 3000:3000 my-app

7️⃣ ENV – Set Environment Variables

Used to defineconfiguration variables.

Example:

ENV NODE_ENV=production

Best Practice:Use .env files for secrets instead of hardcoding values.

8️⃣ VOLUME – Persistent Storage

Ensures data isnot lostwhen the container stops.

Example:

VOLUME /data

✅Useful for:Databases, logs, and file storage.

9️⃣ ARG vs ENV – Build vs Runtime Variables

Instruction Purpose Available at Runtime?
ARG Temporary build-time variable ❌ No
ENV Runtime configuration ✅ Yes

Example:

ARG BUILD_VERSION=1.0
ENV APP_ENV=production

✅Use ARGfor build-time secrets.

✅Use ENVfor runtime configs.

Optimized Dockerfile Example

FROM node:18-slim
WORKDIR /app
COPY package.json .
RUN npm install --production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]

✅Best Practices Applied:

  • Uses aslim base image.
  • Sets aworking directory.
  • Minimizes layersin RUN .
  • Avoids unnecessary files.
  • UsesCMD for flexibility.

Summary

Instruction Purpose
FROM Defines the base image
WORKDIR Sets working directory
COPY vs ADD Copies files (ADD also extracts)
RUN Executes build commands
CMD vs ENTRYPOINT Defines how the container runs
EXPOSE Declares port usage
ENV Sets environment variables
ARG Temporary build-time variables
VOLUME Persistent storage

Final Tip

Alwaysuse multi-stage buildsandminimize image sizefor better performance!

Did you find this guide helpful? Drop a comment below! ????

Docker #DevOps #Cloud #Containers #CICD #DevopsInsiders #devins

Теги #Dockerfile
Ресурс : dev.to


Scroll to Top