Command Library
0%
Beginner ~35 min

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.

0 / 11 done

Prerequisites

  • Docker installed, from the Install and Configure Docker guide
  • Your user added to the docker group, or prefix each command with sudo
1
Step 1 Snippet

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.

Docker pulled it from Docker Hub on first use. An image is the read-only recipe; a container is one running instance of it, and you can start many from the same image.
2
Step 2 Snippet

List Docker Containers


                            

Without -a you see only what is running. With it you also see what stopped and why.

3
Step 3 Snippet

View Docker Container Logs


                            

A container that exits immediately is not broken silently. Its reason is in the logs.

4
Step 4 Snippet

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.

5
Step 5 Snippet

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.

6
Step 6 Snippet

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.

By service name. Compose puts both on one network where "db" resolves to that container, so no IP address is ever hardcoded.
7
Step 7 Snippet

Docker Compose Up (Start Services)


                            

Starts everything in the file. Run it again after any edit and only what changed is recreated.

8
Step 8 Snippet

Rebuild and Restart Docker Compose Services


                            

When you changed the Dockerfile rather than the compose file, this forces the image to be rebuilt.

9
Step 9 Snippet

See What Containers Are Doing to the Machine


                            

Where the disk went. Old images and orphaned volumes are usually the answer.

10
Step 10 Snippet

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.

11
Step 11 Snippet

Remove Stopped Containers & Unused Images


                            

Reclaims space from stopped containers and unused images. Read what it lists before confirming.