Docker & Containers (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)
What is a Dockerfile and what are its essential instructions?
junior LevelMost AskedCode ExampleDocker & Containers
In Simple Words:
A Dockerfile is a plain text file containing sequential instructions that Docker executes to assemble an image.
FROM: Sets the base parent image (e.g.,FROM node:20-alpine). Every valid Dockerfile must start withFROM.WORKDIR: Sets the working directory for subsequent instructions (WORKDIR /app). Creates the folder if it does not exist.COPY: Copies files or directories from the host build context into the container filesystem (COPY package*.json ./).RUN: Executes commands during image build time and commits the result as a new image layer (e.g.,RUN npm install).ENV: Defines environment variables persisted in the image and available at container runtime (ENV PORT=3000).EXPOSE: Documents the network port the container listens on at runtime (informational; does not actually publish ports).CMD/ENTRYPOINT: Specifies the command and executable executed when the container starts.
dockerfile
# Simple production Node.js Dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]