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 [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
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
- Published in Hackernoon — Top 10 Docker commands you can’t live without
- Published in Hackernoon — To boldly log: debug Docker apps effectively using logs options, tail and grep
- Published in Hackernoon — Clean out your Docker images, containers and volumes with single commands
- Published in freeCodeCamp — The ups and downs of docker-compose — how to run multi-container applications
- More of my posts on: Docker
Read more from Medium
- Preethi Kasireddy in freeCodeCamp — A Beginner-Friendly Introduction to Containers, VMs and Docker
- Aymen El Amri in Statuscode — My Docker Cheat Sheet
- Travis Reeder in Travis on Docker — Why and How to Use Docker for Development
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.