Day-to-day Docker work revolves around a core set of commands for pulling images from registries, starting and stopping containers, reading logs, and cleaning up unused resources. Mastering these commands on RHEL 8 means you can confidently deploy, debug, and maintain containerized applications without relying on GUI tools. This tutorial covers the full lifecycle from pulling an image through inspecting a running container, including resource monitoring and image tagging for local or registry use.
Prerequisites
- Docker CE installed and running on RHEL 8
- Root or
sudoaccess, or membership in thedockergroup - Basic familiarity with Linux command-line tools
Step 1 — Pull and List Images
Images are the read-only templates used to create containers. Pull an image from Docker Hub and list all images stored locally.
docker pull nginx:latest
docker pull nginx:1.27-alpine
docker images
The images output shows repository, tag, image ID, creation date, and size. Pulling a specific tag like 1.27-alpine rather than latest gives you a reproducible build artifact for production deployments.
Step 2 — Run, List, Stop, and Remove Containers
Start a container in detached mode with a recognizable name, verify it is running, then stop and remove it.
docker run -d --name webserver -p 8080:80 nginx:latest
docker ps
docker ps -a
docker stop webserver
docker start webserver
docker restart webserver
docker rm webserver
docker rm -f webserver
docker ps shows only running containers; docker ps -a includes stopped ones. The -f flag on rm forces removal of a running container in a single step, equivalent to stop followed by rm.
Step 3 — Execute Commands and Open a Shell
Use docker exec to run a command inside a running container without stopping it. The -it flags attach an interactive pseudo-terminal, giving you a full shell session.
docker run -d --name webserver -p 8080:80 nginx:latest
# Run a one-off command
docker exec webserver nginx -v
# Open an interactive shell
docker exec -it webserver /bin/bash
# For minimal images that ship only sh (Alpine-based):
docker exec -it webserver /bin/sh
Changes made inside an exec session do not persist to the image. They survive only as long as the container exists.
Step 4 — View Logs and Inspect Containers
Container logs stream to stdout/stderr and are accessible via docker logs. Use docker inspect for low-level JSON metadata including environment variables, mounts, network settings, and restart policies.
# Follow logs in real time (Ctrl+C to exit)
docker logs -f webserver
# Show last 50 lines only
docker logs --tail 50 webserver
# Full JSON metadata
docker inspect webserver
# Extract a specific field with --format
docker inspect --format '{{.NetworkSettings.IPAddress}}' webserver
Step 5 — Monitor Resource Usage and Running Processes
Check live CPU, memory, network I/O, and block I/O for all running containers with docker stats, and list the processes inside a container with docker top.
# Live resource stream (Ctrl+C to exit)
docker stats
# One-shot snapshot (no stream)
docker stats --no-stream
# List processes inside a container
docker top webserver
docker stats is particularly useful on RHEL 8 production hosts for spotting a container consuming unexpected memory before it triggers the kernel OOM killer.
Step 6 — Tag Images and Prune Unused Resources
Tag an image to give it a custom name for pushing to a registry or referencing locally, then reclaim disk space by removing unused images, stopped containers, and dangling build layers.
# Tag an existing image
docker tag nginx:latest myregistry.example.com/myapp/nginx:1.0
# Remove a specific image
docker image rm nginx:latest
# Remove all stopped containers, unused networks, dangling images, and build cache
docker system prune
# Also remove unused images (not just dangling ones)
docker system prune -a
docker system prune -a is aggressive: it removes every image not referenced by an existing container, including base images you might want to keep for fast rebuilds. Use docker image prune for a more targeted cleanup that only removes dangling (untagged) images.
Conclusion
You now have a solid command of the Docker image and container lifecycle on RHEL 8: pulling versioned images, running named containers, opening interactive shells, streaming logs, monitoring resources, and pruning unused objects. Combining docker inspect with --format queries is particularly powerful in shell scripts and CI pipelines where you need to extract specific values like IP addresses or health check results. Regular use of docker system prune keeps storage usage predictable on long-lived servers.
Next steps: How to Install Docker Engine on RHEL 8, How to Configure Docker Networking on RHEL 8, and How to Use Docker Volumes and Bind Mounts on RHEL 8.