ryanwhocodes Feb 20, 2018 · 3 min read

Docker Run Vs Exec - CLI Reference and Key Differences

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

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

Contents

Run

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.

    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 is like the bash command rm it removes the container, but when it exits

  • -i is 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

Exec

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 [OPTIONS] CONTAINER COMMAND [ARG...]

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 my_file.sh
  • -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 my_file.sh inside the /temp directory of the running container ubuntu_bash

Find out more

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. Check out the Docker docs and my other posts for more.