DEV Community

Cover image for Why Your Docker Ubuntu Container Exits Immediately (and How to Fix It)🐳
Chaitanya Rai
Chaitanya Rai

Posted on

Why Your Docker Ubuntu Container Exits Immediately (and How to Fix It)🐳

If you’ve ever tried running an Ubuntu container on macOS (or any system) and noticed that it stops immediately, you’re not alone.

You might see something like this in Docker Desktop:

✅ Docker is running (green icon)

🧱 Image is downloaded

❌ Container won’t stay alive

Let’s break down why that happens — and how to fix it like a pro 💪


🧩 The Problem

You pull the image:

docker pull ubuntu
Enter fullscreen mode Exit fullscreen mode

Then try to run it:

docker run ubuntu
Enter fullscreen mode Exit fullscreen mode

But when you check:

docker ps -a
Enter fullscreen mode Exit fullscreen mode

You see:

CONTAINER ID   IMAGE     COMMAND       STATUS                     PORTS   NAMES
123abc456def   ubuntu    "/bin/bash"   Exited (0) 3 seconds ago   ---     bold_blackburn
Enter fullscreen mode Exit fullscreen mode

👉 It starts and exits immediately!
You might wonder: “Why is my container not staying alive?”

💡 The Reason

A Docker container only runs as long as its main process is running.

When you start the ubuntu image, it runs /bin/bash by default.
But if you don’t attach to it interactively, that shell exits right away.

Think of it like opening a terminal window that instantly closes because you didn’t type anything.

⚙️ The Fix — Run It Interactively

Start the container with an interactive shell:

docker run -it ubuntu
Enter fullscreen mode Exit fullscreen mode

You’ll now drop into a shell like:

root@b7f9c4f8f9e0:/#
Enter fullscreen mode Exit fullscreen mode

🎉 Boom! The container stays alive because you’re inside it.

You can explore, install packages, and do anything:

apt update && apt install curl
Enter fullscreen mode Exit fullscreen mode

When you type exit, it will stop again — as expected.

🏷️ Add a Name to Manage It Easily

Add a friendly name to your container:

docker run -it --name myubuntu ubuntu
Enter fullscreen mode Exit fullscreen mode

Now you can easily restart or attach later:

docker start -ai myubuntu
Enter fullscreen mode Exit fullscreen mode

No need to remember a random container ID!

🌀 Keep It Running in the Background

If you want your Ubuntu container to stay alive in the background, you need a process that never ends.
For example:

docker run -d --name myubuntu ubuntu tail -f /dev/null
Enter fullscreen mode Exit fullscreen mode

Here’s what’s happening:

-d →detached mode (runs in background)

tail -f /dev/null →keeps the process alive forever

Now check:

docker ps
Enter fullscreen mode Exit fullscreen mode

You’ll see your container happily running 😄

🔍 Summary

Mode Command Behavior
Basic docker run ubuntu Starts and exits immediately
Interactive docker run -it ubuntu Opens shell, stays alive
Named docker run -it --name myubuntu ubuntu Easier to manage
Background docker run -d --name myubuntu ubuntu tail -f /dev/null Persistent background container

💬 Final Thoughts

So, the reason your Ubuntu container wasn’t running wasn’t a Docker Desktop bug — it was just Docker doing exactly what it should.
Containers are designed to run a single process, not act as full-blown virtual machines (though you can make them behave like one 😉).

If you remember this golden rule 👇

🧠 “No running process → container exits.”

…you’ll never be confused again.

Top comments (0)