Docker
Dockerfile
DevOps
Containerization
Automation

Docker build and run a Dockerfile in one command

Codemia Knowledge Hub ·

Have docker build print only the image ID and pass that ID straight into docker run:

bash
docker run --rm -p 8080:8080 "$(docker build -q .)"

Docker normally keeps the two steps apart (docker build makes an image, docker run starts a container from it), but command substitution joins them into one line:

  • docker build -q . builds the image from the Dockerfile in the current directory
  • -q tells Docker to print only the resulting image ID
  • $(...) captures that image ID in the shell
  • docker run starts a container from that image reference
  • --rm removes the container when it exits

This is handy for quick local testing because you do not have to invent a tag for every run.

A complete example

Given this Dockerfile:

dockerfile
1FROM python:3.12-slim
2
3WORKDIR /app
4COPY app.py .
5
6CMD ["python", "app.py"]

And this program:

python
print("hello from the container")

Build and run it in one go:

bash
docker run --rm "$(docker build -q .)"

Output:

text
hello from the container

For a container that serves something, add port mappings and any environment variables it needs:

bash
docker run --rm -p 4000:8080 -e APP_ENV=dev "$(docker build -q .)"

It is still one shell command, even though it performs two Docker operations.

Variations

If the Dockerfile has a different name or lives elsewhere, add -f:

bash
docker run --rm "$(docker build -q -f docker/Dockerfile .)"

With build arguments:

bash
docker run --rm "$(docker build -q --build-arg APP_MODE=demo .)"

For an interactive container:

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

Anything that produces a valid image reference can be substituted into docker run.

When to tag the image instead

The one-liner suits short feedback loops: trying out a sample Dockerfile, running a throwaway container, or checking that a change still builds and starts. When you need a reusable, named image, or clearer logs in CI, tag it explicitly:

bash
docker build -t myapp:dev .
docker run --rm -p 4000:8080 myapp:dev

That is two commands, but it is easier to debug, repeat and share.

Limitations of the one-liner

  • The image has no name. It is identified only by a hash unless you inspect the expanded command, which makes troubleshooting harder.
  • --rm does not delete the image. It removes the stopped container only. If you run the command often and the build cache keeps changing, unused images pile up.
  • Command substitution is a shell feature. It works in bash and zsh, but quoting rules differ between environments. Keep the quotes around $(...) where whitespace handling matters, and prefer the explicit two-step form when scripts must be portable across shells and CI systems.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design