Cloud
Docker
Cammands_and_examples
Dockerfile

🛠️ Understanding the Dockerfile

A

Dockerfile
is a script that contains a set of instructions used to build a Docker image. Each instruction adds a layer to the image, helping define the runtime environment for your application.


🔧 Common Dockerfile Instructions

  • FROM
    : Sets the base image for the container.
  • WORKDIR
    : Sets the working directory inside the image.
  • COPY
    /
    ADD
    : Copies files from the host into the container.
  • RUN
    : Executes commands in the image during build (e.g., install dependencies).
  • CMD
    /
    ENTRYPOINT
    : Sets the default command to run in the container.
  • EXPOSE
    : Documents the port on which the container will listen.
  • ENV
    : Sets environment variables.
  • ARG
    : Defines build-time variables.
  • VOLUME
    : Creates mount points for external volumes.
  • LABEL
    : Adds metadata to the image.
  • USER
    : Specifies which user the container runs as.

🧪 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"]