Containers, Privileges and Secrets
Overview
Containers improve packaging and isolation, but they do not automatically provide complete security. A process running as root in a container may also run as root inside the container namespace. If the container has a host-mount or unnecessary capability, an exploit can create wider impact.
Detailed Notes
Insecure Dockerfile
FROM node:22
COPY . .
RUN npm ci
CMD ["npm", "start"]
This may run as root and include the whole build context.
Safer Dockerfile
FROM node:22-alpine
ENV NODE_ENV=production
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY --chown=node:node . .
USER node
EXPOSE 3000
CMD ["node", "server.js"]
USER node runs the final process as the existing unprivileged node account. --chown assigns files but cannot replace OS-level permissions.
Runtime hardening
docker run -d --name api \
--read-only --tmpfs /tmp \
--cap-drop ALL \
--pids-limit 256 \
--memory 512m --cpus 1 \
my-api:1.0
--read-only: reject writes to the container root filesystem.--tmpfs /tmp: allow a writable temporary directory.--cap-drop ALL: remove default Linux capabilities.--pids-limit: limit process creation.--memoryand--cpus: set resource limits.
Secrets
Prefer an orchestration secret, cloud secret manager or environment injected at runtime. An environment variable is not an encryption boundary: process administrators may inspect a container environment.
Scan with your registry's scanner or:
docker scout cves my-api:1.0
A scan result needs prioritization: reachability, exploitability and fix availability matter, not just the count.
Security spans the build, image, runtime and supply chain.
Common Interview Questions
Finished this lesson?
0 of 27 lessons done · 0% of the course

