Back to roadmaps docker Course

Writing Dockerfiles: Key Configuration Directives

A Dockerfile is a text script containing instructions to assemble a Docker image. Let us review the key directives used to construct these files.


1. Key Directives Reference

Here is a standard Dockerfile template structure:

# 1. Base Image selection
FROM node:20-alpine

# 2. Define active workspace directory path inside container
WORKDIR /app

# 3. Copy files from host to container workspace
COPY package.json .

# 4. Execute shell commands during the image build process
RUN npm install

# 5. Copy the remaining source code files
COPY . .

# 6. Declare environment variables
ENV NODE_ENV=production

# 7. Document the port the container intends to listen on
EXPOSE 3000

# 8. Define the default executable command when starting a container
CMD ["node", "server.js"]

2. CMD vs ENTRYPOINT

Both directives specify the command that executes when starting a container, but they behave differently:

  • CMD: Defines default arguments that can be overwritten by passing arguments at the end of the docker run command:
# Overwrites the default CMD, running bash instead of node server.js
docker run my-app-image bash
  • ENTRYPOINT: Defines a command that cannot be overwritten easily. Any arguments passed to docker run are appended to the ENTRYPOINT command:
ENTRYPOINT ["ping"]
CMD ["8.8.8.8"]

Running docker run my-ping-image executes ping 8.8.8.8. Running docker run my-ping-image 1.1.1.1 executes ping 1.1.1.1.

Published on Last updated: