Difference between docker run vs exec

docker run and docker exec are both command line instructions used to interact with Docker containers.

  • docker run is used to start a new container from a given image. It creates a new container and runs the specified command in that container. It also allows to set environment variables, mount volumes, expose ports and more.

  • docker exec is used to run a command in an already running container. It allows to run a command in an existing container, without having to start a new one. This command can be used to access the shell of a container and run additional commands inside it.

In summary, docker run creates a new container and runs the command in it, while docker exec runs a command in an existing container.