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
- -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
- Lists host processes; Docker containers appear as child processes of dockerd.
List running containers:
docker ps
- Shows active containers: ID, image, status, ports.
- Filters to running only; use for quick status.
View last container:
docker ps -l
- -l: Latest—shows most recent container (running or stopped).
Quiet IDs:
docker ps -q
- -q: Quiet—outputs only container IDs.
- Pipe to other commands (e.g.,
docker ps -q | xargs docker stop).
All containers:
docker ps -a
- -a: All—includes stopped/exited ones.
- Useful for cleanup or history.
Last ID only:
docker ps -ql
- Combines -q and -l: Single ID of latest container.
Logs and Introspection
View logs:
docker logs container-ID
- Fetches stdout/stderr output since start.
- Replace
container-IDwith ID fromdocker ps; no real-time.
Follow logs:
docker logs container-ID -f
-
-f: Follow—tails logs in real-time like
tail -f. - Monitors ongoing output; Ctrl+C to stop.
Stopping Containers
Graceful stop:
docker stop containerId
- Sends SIGTERM; allows cleanup (e.g., save state).
- Waits up to 10s default; container stops cleanly.
Immediate kill:
docker kill containerId
- Sends SIGKILL; forceful shutdown—no cleanup.
- Use for hung processes; risks data loss.
Resource Limits
Limit memory:
docker run -it --memory 100M python
- --memory 100M: Caps container RAM at 100MB.
- Prevents OOM kills; enforces isolation (e.g., Python script).
- Other limits:
--cpus 0.5for CPU shares.
Pulling and Running Images
Pull image:
docker pull busybox
- 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
- 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
- -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
- --name redis-old: Assigns human-readable name.
- Easier reference than IDs; prevents auto-naming.
- Unique per host; use
docker rmto reuse.
Executing in Containers
Shell into running container:
docker exec -it containerID /bin/bash
- exec: Runs command in live container.
-
-it: Interactive terminal;
/bin/bashstarts shell. - Modifies running state (e.g., install tools); exit returns to host.
Dive Deeper: Ubuntu Container with figlet
Launch:
docker run -it ubuntu
- Pulls Ubuntu image.
- -it: Interactive root shell (bare-bones system).
Install:
apt-get update
apt-get install figlet
- update: Refreshes repos.
- install: Adds figlet (ASCII art); root skips sudo.
Test:
figlet hello
- 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)