In a Dockerfile, RUN and CMD are two important instructions, but they serve different purposes.
RUN is used to execute commands during the build phase of a Docker image. When you run docker build command, each RUN instruction in the Dockerfile is executed in order to build the image. The RUN instruction can be used to install packages, configure the environment, and perform any other tasks that are required to prepare the image for running containers.
For example, the following RUN instruction installs the curl package in the Docker image:
RUN apt-get update && apt-get install -y curl
CMD, on the other hand, is used to specify the default command that should be executed when a container is started from the Docker image. Only the last CMD instruction in the Dockerfile has any effect, and it specifies the command and arguments to be executed when the container is started.
For example, the following CMD instruction sets the default command to run a Python script:
CMD ["python", "app.py"]
If you run a container from the image with this CMD instruction, it will execute the app.py script when the container starts.
In summary, RUN is used during the build phase to modify the Docker image, while CMD specifies the default command to run when a container is started from the image.