Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Ask three questions before typing: Does it need my files? Add -v and -w. Does it need to reach other containers? Add --network. Does it need my keyboard? Add -it. Always add --rm.

Counting lines in quicknotes' codeStep 1 of 4
  1. The tool

    Any small Linux image has wc. Use alpine.

A common trap: running an interactive tool without -it. The program starts, finds no terminal, and exits immediately or appears frozen.

Drills

Answer fast — trust the rule.

quick drill

What does --rm add to a one-off tool run?

quick drill

A containerised linter reports "file not found" for your code. What is most likely missing?

quick drill

You start docker run --rm postgres:16 psql -h notes-db -U postgres and it cannot find host notes-db. What should you add?

quick drill

When do you need -it?

Remember

  • Borrow a tool for one command: --rm, mount what it needs, run, gone.
  • Shortcut: my files → -v/-w; other containers → --network; my keyboard → -it.
  • Trap: forgetting the mount (file not found) or -it (interactive tool quits or hangs).

Practice — level up

0/1 solved

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

ExplainUse a tool without installing it

You need to check quicknotes' requirements.txt with a Python 3.12 tool but your laptop has no Python. Write a docker run command that does it, and explain --rm, -v, -w and when you would add -it.