Docker run vs exec: deep-dive into their differences

Both Docker run and exec execute commands in a Docker container. However, there are key differences in their use that suit different situations.

Docker logo. Source: Docker on GithubDocker concepts, such as images and containers- Docker: Get StartedUsing Docker commands- Top 10 Docker commands you can’t live without- Run bash or any command in a Docker containerrun

Use this to run a command in a new container. It suits the situation where you do not have a container running, and you want to create one, start it and then run a process on it.

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Following the docker run command, you must specify the image to create the container from, but there are optional arguments you can pass as well. For example:

docker run --name ubuntu_bash --rm -i -t ubuntu bash

This will create a container named ubuntu_bash and start a Bash session. A more detailed breakdown of the options and arguments used in the example is:

  • --name assigns a name to the container, in this case ubuntu_bash
  • --rm like the bash command rm it removes the container, but when it exits
  • -i short for -interactive, this ensures STDIN is kept open even if not attached to the running container
  • -t, which can also be referenced with -tty , starts an interactive bash shell in the container
  • The image for the container follows the options, here it is the image ubuntu
  • The last part that follows the image, is the command you want to run: bash


This is for when you want to run a command in an existing container. This is better if you already have a container running and want to change it or obtain something from it. For example, if you are using docker-compose you will probably spin-up multiple containers and you may want to access one or more of them once they are created.


Docker exec also has a range of options and arguments you can specify, although you must state the container and command to execute. You can start an interactive bash shell on a container named ubuntu_bash using:

docker exec -it ubuntu_bash bash

Here the options -it have the same effect as with run. An example with more options and arguments is:

docker exec -d -w /temp ubuntu_bash touch
  • -w followed by the directory or file path allows you to state which working directory you want to run the command in.
  • -d or -detached means that the container will run in detached mode, so you can still continue to use your terminal session with the container running in the background. Don’t use this if you want to see what the container sends to STDOUT.
  • The command is touch used to create the file with the name inside the /temp directory of the running container ubuntu_bash

Aside from these two commands, there are other Docker commands that have subtle differences, such as run’s similarities to build, create. Learning more and trying out the different docker commands can help you become a pro at using this powerful cloud technology.

Read more from Ryan Davidson

Read more from Medium

Read more from the web

Docker run vs exec: deep-dive into their differences was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.

Publication date: 
04/29/2018 - 20:50

The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.