Run Docker Container in Background (Detached Mode)

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
Run Docker Container in Foreground Mode
Run Docker Container in Foreground Mode

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
Run Docker Container in Detached Mode
Run Docker Container in Detached Mode

To list all containers, run the following command (default shows just running).

# docker ps -a
List All Running Docker Containers
List All Running Docker Containers

In addition, to reattach to a detached container, use docker attach command.

# docker attach --name pandorafms
# 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.

  1. Install Docker and Learn Basic Container Manipulation in CentOS and RHEL 7/6 – Part 1
  2. How to Name or Rename Docker Containers
  3. 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.

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

5 thoughts on “Run Docker Container in Background (Detached Mode)”

  1. In this article, you treated running background images the same as a detached mode. But both are not the same.

    Detached mode = Standard console will note be attached for Terminal IO. This may stop once the main process in docker image is completed

    Background mode = Run in the background continuously without stopping (like daemon).

    Foreground or console = If you exit console the process may stop.

    I was looking to run a python-3.7.5 image in the background but it seems detached mode option doe not works.

  2. Good summary of basic commands. However I think there is a typo with the explanation of the ‘–rm’ parameter. This parameter removes the container when it “exits”, not when it “exists”


Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.