Running and Managing Containers
You have the quicknotes image. Now you want it running in the background, reachable in your browser, and you want to read its logs, peek inside, stop it and clean it up. Six or seven commands cover nearly all of that, and they all follow one lifecycle.
The rule
run = create + start; stop keeps it; rm deletes it.
A container moves through a few states: created → running → exited → removed. The main commands move it between them:
docker run -d --name notes -p 8080:5000 quicknotes:1.0
docker ps # running containers
docker logs notes # what the app printed
docker exec -it notes sh # open a shell inside
docker stop notes # running → exited
docker ps -a # all containers, including exited
docker start notes # exited → running again
docker rm notes # exited → removedThe run flags you will use constantly:
-druns it detached, in the background.--name notesgives it a name, so you do not have to copy random IDs.-p 8080:5000publishes a port: host port first, container port second. Your browser openslocalhost:8080, and Docker forwards it to port 5000 inside the container.--rmremoves the container automatically when it exits; handy for one-off runs.
docker stop sends the main process a polite stop signal, waits (10 seconds by default), then forces it. The container is exited, not gone: its writable layer and settings remain, which is why start can bring it back. Only rm deletes it, and rm refuses a running container unless you stop it first or add -f.
The shortcut
Say where the container is in its lifecycle before choosing a command. Running? Then logs, exec or stop. Exited? Then start, logs or rm. And for -p, read it as "outside : inside".
Start
docker run -d --name notes -p 8080:5000 quicknotes:1.0. State: running. Browser openslocalhost:8080.
A frequent error is "the container name is already in use". It means an exited container still holds the name. Start it, or remove it first.
Drills
Answer fast — trust the rule.
In -p 8080:5000, which number is the port inside the container?
After docker stop notes, where does the container appear?
Which command shows what quicknotes has printed so far?
docker run --name notes ... fails with "name already in use", but docker ps shows nothing. Why?
What does --rm on docker run do?
Remember
- run = create + start; stop keeps it (exited); rm deletes it.
- Shortcut: name the lifecycle state first; read
-pas outside : inside. - Trap:
docker pshides exited containers, which still hold their names.
