🛠️ Understanding the Dockerfile
A
Dockerfile
🔧 Common Dockerfile Instructions
- : Sets the base image for the container.
FROM
- : Sets the working directory inside the image.
WORKDIR
- /
COPY
: Copies files from the host into the container.ADD
- : Executes commands in the image during build (e.g., install dependencies).
RUN
- /
CMD
: Sets the default command to run in the container.ENTRYPOINT
- : Documents the port on which the container will listen.
EXPOSE
- : Sets environment variables.
ENV
- : Defines build-time variables.
ARG
- : Creates mount points for external volumes.
VOLUME
- : Adds metadata to the image.
LABEL
- : Specifies which user the container runs as.
USER
🧪 Example Dockerfile (Node.js)
Below is a simple Dockerfile to containerize a Node.js application.
# Use an official Node.js runtime as the base image FROM node:18 # Set the working directory inside the container WORKDIR /usr/src/app # Copy dependency definitions COPY package*.json ./ # Install dependencies RUN npm install # Copy the rest of the application COPY . . # Expose the app port EXPOSE 3000 # Run the application CMD ["npm", "start"]