Using Docker: From One Container to a Compose Stack
Run your first container, see inside it, keep its data, then move to a compose file where an app and a database talk to each other. Ends with reading resource usage and cleaning up safely.
Prerequisites
- Docker installed, from the Install and Configure Docker guide
- Your user added to the docker group, or prefix each command with sudo
Run Docker Container in Background
Your first container. -d sends it to the background, -p maps a port on the machine to a port inside it.
List Docker Containers
Without -a you see only what is running. With it you also see what stopped and why.
View Docker Container Logs
A container that exits immediately is not broken silently. Its reason is in the logs.
Open a Shell Inside a Running Container
Step inside a running container to look around. Anything you change here disappears when the container is removed.
Test this before you trust a database container. Recreate it and confirm the data is still there.
Run a Container with Persistent Storage
The lesson that costs people real data: without a volume, docker rm takes everything the container wrote with it.
Write a docker-compose.yml for App and Database
Two containers that need each other is where single commands stop scaling. The file becomes the documentation.
Docker Compose Up (Start Services)
Starts everything in the file. Run it again after any edit and only what changed is recreated.
Rebuild and Restart Docker Compose Services
When you changed the Dockerfile rather than the compose file, this forces the image to be rebuilt.
See What Containers Are Doing to the Machine
Where the disk went. Old images and orphaned volumes are usually the answer.
Adding -v to this command deletes those volumes too, and with them the database. Type it deliberately.
Docker Compose Down (Stop & Remove)
Stops and removes the containers. Named volumes survive, which is exactly why the database is on one.
Remove Stopped Containers & Unused Images
Reclaims space from stopped containers and unused images. Read what it lists before confirming.