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.
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.
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.
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.