I tried running my containers in detached mode and noticed some issues.

When I ran the command docker container run -d nginx, the image of nginx got pulled, the container got created, and kept running in the detached mode. But when I did the same with the Ubuntu image, the container stopped running and did not start using the docker start command. I tried the same with the docker-compose file and the same is happening to it. Does anyone know why is it happening?

I can run the Ubuntu server using the command docker run -it ubuntu. But I want to run the container in the detached mode.

2 Answers 2


When you run a Docker container using the docker run command with the -d flag, you're instructing Docker to run the container in detached mode, meaning it runs in the background. However, whether the container remains running or stops immediately after being started depends on the command that the container is configured to execute upon startup.

In the case of the nginx image, it's designed to start a web server upon startup, which typically runs continuously, keeping the container running. This behavior aligns with what you observed.

However, the Ubuntu image doesn't have a default command to keep the container running in the background. Instead, it executes the default command (usually /bin/bash) and then exits, causing the container to stop immediately after it starts.

To keep the Ubuntu container running, you can specify a command to execute when starting the container. If you don't want the interactive shell session you can use this command:

docker container run -d ubuntu tail -f /dev/null

If you're using a Docker Compose, you would need to specify a command or entry point in your docker-compose.yml file to ensure the container keeps running:

version: '3'
    image: ubuntu
    command: tail -f /dev/null
  • Thanks! This actually works :)
    – Jilson
    Commented Apr 3 at 6:36

The ubuntu image, by defaults, starts a shell. If standard input isn't connected, the container exits immediately. You can see this even without detached mode; try this:

docker run ubuntu:latest

You will see that the container exits immediately.

Detached mode isn't really useful for an interactive shell session; it makes more sense for a persistent service (for example, a webserver like nginx). You can still use it:

docker run -it -d ubuntu:latest

You can use docker attach to connect to the container console.

Just to be thorough:

If you connect standard input (-i) but don't create a terminal (not -t), like this:

docker run -i ubuntu:latest

The container won't exit immediately, but you won't get a prompt (because it's not attached to a terminal, the shell assumes it's reading a script from stdin).

If you connect standard input and create a terminal, you get the interactive behavior you expect:

docker run -it ubuntu:latest
  • I'm already aware about the -it flags. I wanted to run the Ubuntu container in detached mode.
    – Jilson
    Commented Apr 3 at 6:35
  • And I think this answer shows you exactly how to do that, and why it was failing previously.
    – larsks
    Commented Apr 3 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.