- Beginner
- Building images
How it works
FROM picks the base, WORKDIR sets the folder, COPY brings files from the build context, RUN executes a command at build time, ENV and EXPOSE describe the configuration, USER sets who runs the program, and CMD or ENTRYPOINT set what runs. Instructions apply in order, from top to bottom.
What breaks
RUN runs at build time, CMD when the container starts: RUN node server.js hangs the build, since the server never exits. Each RUN also starts a fresh shell, so RUN cd /app has no effect on the next line; use WORKDIR.
In a Dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["node", "src/server.js"]
See it break, then fix it
Write the Dockerfile or the compose file, type the docker commands, and watch a simulated server run them through reboots, upgrades and traffic. Free to start.