Skip to the workspace
Pebellslearn by building
Using Third-Party Images  ›  Capstone Project

Borrowing Command-Line Tools

Command Line Utilities 12 minRun a tool from an image for a single command with docker run --rm, mounting the folder it should work on, so nothing is installed and nothing is left behind.

docker / Borrowing Command-Line Tools

Explore each scene at your own pace.

01 · The rule

The rule

Borrowing Command-Line Tools

You want to peek into the quicknotes database with psql, test an HTTP endpoint with curl, or run a Python script, but you do not want to install any of those tools on your laptop. Containers make a tool something you can borrow for one command and hand back.

The rule

Borrow a tool for one command: --rm, mount what it needs, run, gone.

The pattern has three ingredients:

  • --rm deletes the container as soon as the command finishes, so nothing piles up.
  • -v "$PWD":/work -w /work mounts your current folder into the container and starts the command there, so the tool can see your files. Without a mount, it sees only the image's own files.
  • -it gives the tool your keyboard and a terminal. Add it only for interactive programs, such as a shell or psql prompt.
# run a Python script from the current folder, no Python installed
docker run --rm -v "$PWD":/work -w /work python:3.12-slim python check.py

# call quicknotes from inside its network
docker run --rm --network notes-net curlimages/curl -s http://notes:5000/

# open an interactive psql prompt against the database
docker run --rm -it --network notes-net postgres:16 \
  psql -h notes-db -U postgres -d quicknotes

In the last two lines, joining notes-net lets the borrowed tool reach containers by name, exactly like quicknotes does. And in the psql case you borrow only the client program from the postgres image; no second database starts, because your command replaces the image's default one.

The first run downloads the image; later runs reuse it from the cache, so the tool starts almost instantly.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.