Docker - Run container in background using the docker run --detach command

by
Jeremy Canfield |
Updated: January 26 2025
| Docker articles
By default, when creating a container using the docker run command, the command is run in the foreground, which means that your shell cannot be used to issue other commands, and you would have to use Ctrl C or Ctrl Z to regain your shell, which will stop the container. The --detach flag can be used to run the container in the background, so that you can use your shell to issue other commands.
In this example, a container is created using the foo:latest image and the container is run in the background.
Almost always, this is coupled with the --rm flag to remove the container after is has completed.
[john.doe@docker ~]# docker run --detach --rm --name my-container my-image:latest
Did you find this article helpful?
If so, consider buying me a coffee over at