How to run a docker container in the background or detached mode in the terminal?

August 7, 2021 - 2 min read

To run a docker container in the background or the detached mode from the terminal, you can use the docker run command followed by the -d flag (or detached flag) and followed by the name of the docker image you need to use in the terminal.

TL;DR

# Run docker container in the background
# or detached mode in the terminal
docker run -d <YOUR_DOCKER_IMAGE_NAME>

For example, let's say we want to make a docker container from the docker/getting-started docker image and then run the docker container in the background or detached mode. So here we can use the docker run command like this,

# Run docker container in the background
# or detached mode in the terminal
docker run -d docker/getting-started
  • After running the above command you may see some logs stating that it is downloading the image from the remote docker hub server since it is not in the system locally

The logs may look like this,

# Logs after running the above command
# for the first time may look like this

Unable to find image 'docker/getting-started:latest' locally
latest: Pulling from docker/getting-started
540db60ca938: Pull complete
0ae30075c5da: Pull complete
9da81141e74e: Pull complete
b2e41dd2ded0: Pull complete
7f40e809fb2d: Pull complete
758848c48411: Pull complete
23ded5c3e3fe: Pull complete
38a847d4d941: Pull complete
Digest: sha256:10555bb0c50e13fc4dd965ddb5f00e948ffa53c13ff15dcdc85b7ab65e1f240b
Status: Downloaded newer image for docker/getting-started:latest
99da43876210b14242b9ecb6ebfe62c55189789b8ef85ad2b4c4da011ec48a4f

This means the docker/getting-started image has been downloaded and the container has been started by the docker engine.

Now since the docker container is started in the background you can use the terminal for other purposes too.

The above log will only for the first time while downloading, running the command again after this may show only the docker container id which is started.

Now if you go to the localhost or localhost:80 in your browser, you can see it opens up the container application from the docker/getting-started image.

If you want to try out docker commands online, see Play with docker lab to test out the commands.

That's all 😃!

Feel free to share if you found this useful 😃.