Skip to content
BENIBIX Learn

docker run vs docker exec

People think docker run takes you back into a container, and it does not. docker run always makes a new container from an image. docker exec runs a command in one that is already running. Type docker run twice and you have two containers, each with its own files.

Infrastructure📦 Containers

docker run

The command that makes a new box from an image and starts it.

docker run is the command that makes a new container from an image and starts it. If the image is not on the machine yet, it downloads it first. Flags set how the container runs: -d runs it in the background, -p publishes a port, and --name gives it a name. Each run makes a fresh container. Changes made inside one are not seen by the next, unless they are kept in a volume.

Example
Jack types docker run -d -p 8080:80 nginx. Docker downloads the image, starts the web server in the background and prints the new container's ID.
Why it matters
It is the first command most people learn. Nearly every way of starting a container comes back to it.
Full entry
Infrastructure📦 Containers

docker exec

Runs one more command in a live box. Often it is a shell.

docker exec runs a new command inside a container that is already running. The most common use is opening a shell. You type docker exec -it, the name and sh. The -i keeps input open, and -t gives you a proper terminal. The command runs beside the main program and ends when you exit. It only works while the container is running.

Example
Lucy's web container shows an error. She types docker exec -it web sh, reads the config file inside, and leaves with exit.
Why it matters
It is the quickest way to look inside a live container. Most fixing starts there.
Full entry

In one line

docker run
The command that makes a new box from an image and starts it.
docker exec
Runs one more command in a live box. Often it is a shell.