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.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

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

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

2 Responses

  1. Cédric says:

    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.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.