Run Your First Real Docker Container (And Understand Every Command)

If you’re new to Docker, most tutorials jump too fast.

They show commands, things start running, and suddenly you’re expected to “just know” what happened.

This post is different.

We’ll run real Docker containers, step by step, and understand exactly what each command does — no magic, no skipped steps.

By the end, Docker will feel predictable, not mysterious.


What You Need Before Starting

  • Docker installed and running
    (Docker Desktop on Windows/macOS, or Docker Engine on Linux)
  • Basic terminal knowledge

That’s it.


Step 1: Verify Docker Is Working (The Simplest Container)

Start with this command:

docker run hello-world

What just happened?

Behind the scenes, Docker did five things:

  1. Checked if the hello-world image exists locally
  2. Didn’t find it → pulled it from Docker Hub
  3. Created a container from the image
  4. Ran the container
  5. Printed a message and exited

Important detail:

The container stopped automatically because its job was finished.

This teaches an important rule early: ✅ Containers run a process. When the process ends, the container exits.


Step 2: Run a Real Service (Nginx Web Server)

Now let’s run something that keeps running.

docker run -d -p 8080:80 nginx

Let’s break this down

  • docker run → create + start a container
  • -d → detached mode (runs in background)
  • -p 8080:80 → map host port 8080 → container port 80
  • nginx → official Nginx image

Open your browser:

http://localhost:8080

🎉 You’re running a web server inside a container.


Step 3: See What’s Running

List running containers:

docker ps

You’ll see:

  • container ID
  • image name
  • exposed ports
  • container name

To see all containers (including stopped ones):

docker ps -a

This helps you understand: ✅ Containers can exist even when they’re not running.


Step 4: View Container Logs

Containers don’t have screens — logs are how you debug them.

docker logs <container_id>

Example:

docker logs a1b2c3d4

Logs come from:

  • stdout
  • stderr

Which means: ✅ If your app logs correctly, Docker captures it automatically.


Step 5: Enter the Container (Very Important)

Now let’s step inside the container.

docker exec -it <container_id> /bin/sh

You’re now inside the container.

Try this:

ls
cd /usr/share/nginx/html
ls

You’ll see:

index.html

This teaches a critical concept: ✅ Containers have their own filesystem.
✅ They are isolated from your host system.

Exit the container:

exit

Step 6: Stop and Remove the Container (Clean Exit)

Stop the container:

docker stop <container_id>

Remove it:

docker rm <container_id>

Now check again:

docker ps -a

The container is gone.

Important takeaway: ✅ Containers are disposable
✅ Images remain unless you delete them


Step 7: Understand Images vs Containers (Quick Reality Check)

Run this:

docker images

You’ll still see:

nginx
hello-world

Even though the containers were removed.

Simple rule:

  • Image → blueprint (read‑only)
  • Container → running instance

You can create 100 containers from one image.


Step 8: Run a Container Interactively (Ubuntu Shell)

Now let’s run Ubuntu interactively:

docker run -it ubuntu

You’re dropped into a shell.

Try:

cat /etc/os-release

Exit:

exit

Check containers:

docker ps -a

You’ll see Ubuntu exited immediately after you left the shell.

✅ Again: when the main process ends, the container stops.


Step 9: Why Containers Stop (This Trips Many Beginners)

Docker containers must have a foreground process.

If your app:

  • crashes
  • finishes execution
  • exits cleanly

👉 the container stops.

This explains:

  • restart loops
  • “container exited immediately” issues
  • why Docker isn’t “broken”

Docker is doing exactly what it’s told.


Common Beginner Mistakes (Avoid These)

❌ Expecting containers to stay alive without a running process
❌ Treating containers like virtual machines
❌ Not checking logs
❌ Forgetting to clean up stopped containers

Docker rewards intentional behavior, not assumptions.


What You’ve Actually Learned

Without realizing it, you now understand:

✅ How Docker pulls images
✅ How containers start and stop
✅ Port mapping
✅ Logs
✅ Exec vs run
✅ Filesystem isolation
✅ Image vs container lifecycle

This is more important than memorizing commands.


InfraDecode Closing

Docker becomes easy the moment you stop treating it like magic.
Containers just run processes — nothing more, nothing less.

Once you understand that, everything else clicks.

— InfraDecode


Discover more from

Subscribe to get the latest posts sent to your email.

Leave a Comment

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

Scroll to Top