DEV Community

Cover image for Background vs. Foreground Containers
chandra penugonda
chandra penugonda

Posted on

Background vs. Foreground Containers

Background vs. Foreground Containers

Foreground: Attaches to your terminal (e.g., -it); stops on exit.

Background (detached): Runs silently; use -d flag.

Run clock demo in background:

docker run -d jpetazzo/clock
Enter fullscreen mode Exit fullscreen mode
  • -d: Detached mode—container runs without tying to terminal.
  • Starts jpetazzo/clock image (displays time); ID returned for reference.
  • Frees terminal for other commands.

Monitoring Containers

Check processes:

ps
Enter fullscreen mode Exit fullscreen mode
  • Lists host processes; Docker containers appear as child processes of dockerd.

List running containers:

docker ps
Enter fullscreen mode Exit fullscreen mode
  • Shows active containers: ID, image, status, ports.
  • Filters to running only; use for quick status.

View last container:

docker ps -l
Enter fullscreen mode Exit fullscreen mode
  • -l: Latest—shows most recent container (running or stopped).

Quiet IDs:

docker ps -q
Enter fullscreen mode Exit fullscreen mode
  • -q: Quiet—outputs only container IDs.
  • Pipe to other commands (e.g., docker ps -q | xargs docker stop).

All containers:

docker ps -a
Enter fullscreen mode Exit fullscreen mode
  • -a: All—includes stopped/exited ones.
  • Useful for cleanup or history.

Last ID only:

docker ps -ql
Enter fullscreen mode Exit fullscreen mode
  • Combines -q and -l: Single ID of latest container.

Logs and Introspection

View logs:

docker logs container-ID
Enter fullscreen mode Exit fullscreen mode
  • Fetches stdout/stderr output since start.
  • Replace container-ID with ID from docker ps; no real-time.

Follow logs:

docker logs container-ID -f
Enter fullscreen mode Exit fullscreen mode
  • -f: Follow—tails logs in real-time like tail -f.
  • Monitors ongoing output; Ctrl+C to stop.

Stopping Containers

Graceful stop:

docker stop containerId
Enter fullscreen mode Exit fullscreen mode
  • Sends SIGTERM; allows cleanup (e.g., save state).
  • Waits up to 10s default; container stops cleanly.

Immediate kill:

docker kill containerId
Enter fullscreen mode Exit fullscreen mode
  • Sends SIGKILL; forceful shutdown—no cleanup.
  • Use for hung processes; risks data loss.

Resource Limits

Limit memory:

docker run -it --memory 100M python
Enter fullscreen mode Exit fullscreen mode
  • --memory 100M: Caps container RAM at 100MB.
  • Prevents OOM kills; enforces isolation (e.g., Python script).
  • Other limits: --cpus 0.5 for CPU shares.

Pulling and Running Images

Pull image:

docker pull busybox
Enter fullscreen mode Exit fullscreen mode
  • Downloads image from registry (Docker Hub default).
  • Prepares for offline use; checks for updates if tagged latest.

Run multiple versions:

Use tags (e.g., redis:7 vs. redis:6).

docker run -d redis:7
docker run -d redis:6
Enter fullscreen mode Exit fullscreen mode
  • Tags specify versions; run side-by-side without conflict.
  • Containers isolated—ports/names differentiate.

Ports: Container vs. Host

Containers expose internal ports; map to host for access.

Map ports:

docker run -p 6000:6379 redis
Enter fullscreen mode Exit fullscreen mode
  • -p 6000:6379: Binds host port 6000 to container's 6379 (Redis default).
  • Access via localhost:6000; host:container direction.
  • TCP default; UDP via -p 6000:6379/udp.

Custom name:

docker run -d -p 6000:6379 --name redis-old redis
Enter fullscreen mode Exit fullscreen mode
  • --name redis-old: Assigns human-readable name.
  • Easier reference than IDs; prevents auto-naming.
  • Unique per host; use docker rm to reuse.

Executing in Containers

Shell into running container:

docker exec -it containerID /bin/bash
Enter fullscreen mode Exit fullscreen mode
  • exec: Runs command in live container.
  • -it: Interactive terminal; /bin/bash starts shell.
  • Modifies running state (e.g., install tools); exit returns to host.

Dive Deeper: Ubuntu Container with figlet

Launch:

docker run -it ubuntu
Enter fullscreen mode Exit fullscreen mode
  • Pulls Ubuntu image.
  • -it: Interactive root shell (bare-bones system).

Install:

apt-get update
apt-get install figlet
Enter fullscreen mode Exit fullscreen mode
  • update: Refreshes repos.
  • install: Adds figlet (ASCII art); root skips sudo.

Test:

figlet hello
Enter fullscreen mode Exit fullscreen mode
  • Prints "HELLO" art; install if missing—shows isolation.

Key Takeaways

Containers: foreground for dev, background for services. Monitor with ps/logs, limit resources, map ports. Pull/tag for versions; name/exec for control. Experiment safely—docker rm cleans.

Day 1: Containers mastered—detach and conquer!

Top comments (0)