Under Docker, an image developer can define image defaults related to detached or foreground running, and other useful settings. But, using the docker run [OPTIONS] command, you can add to or override the image defaults set by a developer, thus giving you more control on how a container runs.
Read Also: ctop β Top-like Interface for Monitoring Docker Containers
In this article, we will briefly explain the foreground mode and background mode of running a container and we will also show you how to run a Docker container in the background in detached mode.
Foreground Mode (Default) vs Background/Detached Mode
Before starting a Docker container, you must, first of all, decide if you want to run it in the default foreground mode or in the background in a detached mode.
In the foreground mode, Docker can start the process in the container and attach the console to the processβs standard input, standard output, and standard error.
There are also command line options to configure it more such as -t to allocate a pseudo-tty to the process, and -i to keep STDIN open even if not attached. You can also attach it to one or more file descriptors (STDIN, STDOUT and/or STDERR) using the -a=[value here] flag.
Importantly, the --rm option tells Docker to automatically remove the container when it exits. This example shows how to start a Docker container in foreground mode:
# docker run --rm -ti -p 8000:80 -p 8443:443 --name pandorafms pandorafms/pandorafms:latest
The disadvantage of running a container in the foreground is that you can not access the command prompt anymore, as you can see from the screenshot above. Which means you can not run any other commands while the container is running.
To run a Docker container in the background, use the use -d=true or just -d option. First, stop it from the foreground mode by pressing [Ctrl+C], then run it in a detached mode as shown:
# docker run -d --rm -p 8000:80 -p 8443:443 --name pandorafms pandorafms/pandorafms:latest
To list all containers, run the following command (default shows just running).
# docker ps -a
In addition, to reattach to a detached container, use docker attach command.
# docker attach --name pandorafms OR # docker attach 301aef99c1f3
If you want to stop the above container or any other running container, use the following command (replace 301aef99c1f3 with the actual container ID).
# docker stop 301aef99c1f3
You might also like to read these following related Docker articles.
- Install Docker and Learn Basic Container Manipulation in CentOS and RHEL 7/6 β Part 1
- How to Name or Rename Docker Containers
- How to Remove Docker Images, Containers and Volumes
Thatβs it! In this article, we have shown how to run a Docker container in the background in detached mode. Use the comment form below to give us feedback or ask questions concerning this article.
If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.

Got Something to Say? Join the Discussion... Cancel reply