Run a one off container

Build and run a container in one command, automatically cleaning up when done:

docker run --rm -it $(docker build -q .)

Breakdown

  • docker build -q . - Builds image from current directory (quiet mode, returns only image ID)
  • $(...) - Command substitution to capture the image ID
  • --rm - Automatically remove container when it exits
  • -it - Interactive mode with TTY allocation

Use cases

  • Quick testing of a Dockerfile
  • Running build scripts in a clean environment
  • One-time data processing tasks

Alternative: If you need to pass arguments or mount volumes:

docker run --rm -it -v $(pwd):/app $(docker build -q .) /bin/bash