Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
Docker Docker & Containers: From Zero to Production Docker Containers Module 3 — Working with Containers

Detached vs Foreground Containers — When to Use Each

Reviewed & accurate
AI Summary

What You'll Learn

Beginner

  • The difference between detached and foreground mode
  • When to use each
  • How to attach to and detach from running containers
Terminalbash
# Foreground: output streams to terminal
docker run nginx

# Detached: runs in background
docker run -d nginx
Ctrl+C kills the container
When attached, Ctrl+C sends SIGINT. To detach without stopping, use Ctrl+P then Ctrl+Q.
Usually docker logs -f is safer
Use docker logs -f <name> to follow logs without risking stopping the container.

Practical Exercise

Run a container in detached mode
View logs with docker logs -f
Try attaching with docker attach

Key Takeaways

  • Foreground: Ctrl+C stops the container.
  • Detached (-d): runs in background.
  • Use detached for servers. Use foreground for debugging.
  • docker logs -f is safer than docker attach.
Previously: Lesson 16 covered docker run flags.
Today: You learned: When to use -d, when to stay in foreground.
Next: Lesson 18 covers logging.
Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments