<p>When you run a Docker container using the <code>docker run</code> command with the <code>-d</code> 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.</p>
<p>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.</p>
<p>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 <code>/bin/bash</code>) and then exits, causing the container to stop immediately after it starts.</p>
<p>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:</p>
<pre><code>docker container run -d ubuntu tail -f /dev/null
</code></pre>
<p>If you're using a Docker Compose, you would need to specify a command or <code>entry point</code> in your <code>docker-compose.yml</code> file to ensure the container keeps running:</p>
<pre class="lang-yml prettyprint-override"><code>version: '3'
services:
  ubuntu:
    image: ubuntu
    command: tail -f /dev/null
</code></pre>

CC BY-SA 4.0
2024-04-03T06:36:28.253Z
Thanks! This actually works :)
Jilson
CC BY-SA 4.0
2024-04-03T04:29:21.520Z
2024-04-03T04:29:21.520Z
Ajay
<p>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:</p>
<pre><code>docker run ubuntu:latest
</code></pre>
<p>You will see that the container exits immediately.</p>
<p>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:</p>
<pre><code>docker run -it -d ubuntu:latest
</code></pre>
<p>You can use <code>docker attach</code> to connect to the container console.</p>
<hr />
<p>Just to be thorough:</p>
<p>If you connect standard input (<code>-i</code>) but don't create a terminal (not <code>-t</code>), like this:</p>
<pre><code>docker run -i ubuntu:latest
</code></pre>
<p>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).</p>
<p>If you connect standard input and create a terminal, you get the interactive behavior you expect:</p>
<pre><code>docker run -it ubuntu:latest
</code></pre>

CC BY-SA 4.0
2024-04-03T06:35:34.777Z
I'm already aware about the `-it` flags. I wanted to run the Ubuntu container in detached mode.
Jilson
CC BY-SA 4.0
2024-04-03T13:05:59.627Z
And I think this answer shows you exactly how to do that, and why it was failing previously.
larsks
CC BY-SA 4.0
2024-04-03T03:41:41.810Z
2024-04-03T03:41:41.810Z
larsks
<p>I tried running my containers in detached mode and noticed some issues.</p>
<p>When I ran the command <code>docker container run -d nginx</code>, 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 <code>docker start</code> command. I tried the same with the docker-compose file and the same is happening to it. Does anyone know why is it happening?</p>
<p>I can run the Ubuntu server using the command <code>docker run -it ubuntu</code>. But I want to run the container in the detached mode.</p>

<docker><docker-compose><ubuntu>
Ubuntu container not running in detached mode
Jilson
# Ubuntu container not running in detached mode

(Asked by: username_0 on 2024-04-02)

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.

**Tags:** docker, docker-compose, ubuntu


---

## 2 answers

### Answer by username_1 on 2024-04-03. Score: 0 | Accepted answer

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'
services:
  ubuntu:
    image: ubuntu
    command: tail -f /dev/null

> **Comments:**
> **username_0**: Thanks! This actually works :)  

---

### Answer by username_2 on 2024-04-03. Score: 1

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

> **Comments:**
> **username_0**: I'm already aware about the `-it` flags. I wanted to run the Ubuntu container in detached mode.  
> **username_2**: And I think this answer shows you exactly how to do that, and why it was failing previously.  

---

