Run, list, inspect, and remove containers from the CLI — start a throwaway container, run a real PostgreSQL in the background, read its logs, and open a shell inside it.
A container is a running instance of an image — a packaged, ready-to-run program. docker run downloads the image the first time and starts it. The first command runs a tiny test image and exits; the second runs the Nginx web server in the background (-d = detached) and publishes container port 80 to localhost:8080 (-p host:container).
A one-off container that prints a message and exits
docker run hello-worldRun Nginx in the background, reachable at http://localhost:8080
docker run -d -p 8080:80 --name web nginxdocker ps lists running containers; add -a to include stopped ones. You start, stop, and remove containers by the name you gave them (or their id). Stopping a container keeps it around to restart later; removing it throws it away. logs shows everything the container has printed.
List running containers
docker psInclude stopped ones too
docker ps -aSee what a container has printed
docker logs webGraceful stop (keeps the container so you can restart it)
docker stop webStart it again
docker start webDelete it (must be stopped first)
docker rm webThe real payoff: spin up dependencies in seconds without installing them. This runs PostgreSQL in the background, sets its password with an environment variable (-e), and publishes its port. -d keeps it running; you now have a database on localhost:5432 that you can delete and recreate at will.
Run PostgreSQL in the background with a password and published port
docker run -d \
--name db \
-e POSTGRES_PASSWORD=secret \
-p 5432:5432 \
postgres:16Confirm the "db" container is up
docker psdocker exec runs a command inside an already-running container. With -it (interactive + terminal) you get a live shell or REPL — here, the psql client connected to the database you just started. Type \q to leave. This is how you poke around inside a container without installing anything on your machine.
Open the psql prompt inside the running "db" container
docker exec -it db psql -U postgresOr get a plain shell inside any container
docker exec -it db bash