Sobes.tech
Junior — Middle

What is the difference between the docker run command and the docker exec command when managing containers?

sobes.tech AI

Answer from AI

The docker run command is used to create and start a new container from a specified image. It performs the following steps: creates the container, starts it, and if specified, attaches a terminal.

The docker exec command is used to run a new command inside an already running container. It does not create a new container but works with an existing one.

Example:

  • docker run -it ubuntu bash — creates and starts a new container with Ubuntu and opens bash in it.
  • docker exec -it <container_id> bash — opens bash in an already running container with the specified ID.

Thus, docker run is for starting a new container, docker exec is for executing commands inside a running container.

What is the difference between the docker run command… - sobes.tech