Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Basics of Docker  ›  Lesson

Running and Managing Containers

Basics of Docker 14 mindocker run creates and starts a container, stop halts it while keeping it, start revives it, and rm deletes it, with ps, logs and exec to look inside along the way.

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 → removed

The run flags you will use constantly:

  • -d runs it detached, in the background.
  • --name notes gives it a name, so you do not have to copy random IDs.
  • -p 8080:5000 publishes a port: host port first, container port second. Your browser opens localhost:8080, and Docker forwards it to port 5000 inside the container.
  • --rm removes 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".

One afternoon with quicknotesStep 1 of 4
  1. Start

    docker run -d --name notes -p 8080:5000 quicknotes:1.0. State: running. Browser opens localhost: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.

quick drill

In -p 8080:5000, which number is the port inside the container?

quick drill

After docker stop notes, where does the container appear?

quick drill

Which command shows what quicknotes has printed so far?

quick drill

docker run --name notes ... fails with "name already in use", but docker ps shows nothing. Why?

quick drill

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 -p as outside : inside.
  • Trap: docker ps hides exited containers, which still hold their names.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWalk the lifecycle

Write the commands to start quicknotes in the background with a name and port 8080 on your machine mapped to port 5000 in the container, check its logs, stop it, and remove it. Explain what state the container is in after each step.