Sobes.tech
Junior — Middle

What is the role of the RUN command inside a Dockerfile and why is it used when creating a container?

sobes.tech AI

Answer from AI

The RUN command in a Dockerfile is used to execute commands inside the image being built during the build stage. It allows you to install packages, configure the environment, compile source code, and perform any other actions necessary to prepare the image.

Each RUN creates a new layer in the image, so it is important to optimize the number of such commands to reduce the final image size.

Example usage:

FROM node:14

# Install dependencies
RUN apt-get update && apt-get install -y git

# Copy files and install npm dependencies
COPY package.json ./
RUN npm install

CMD ["node", "app.js"]

Here, RUN is used to install system packages and npm dependencies to prepare the container for running the application.

What is the role of the RUN command inside a… - sobes.tech