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:
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 theDockerfilein the current directory-qtells Docker to print only the resulting image ID$(...)captures that image ID in the shelldocker runstarts a container from that image reference--rmremoves 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:
And this program:
Build and run it in one go:
Output:
For a container that serves something, add port mappings and any environment variables it needs:
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:
With build arguments:
For an interactive container:
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:
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.
--rmdoes 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
bashandzsh, 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.
Related reading
- build context for docker image very large
- Build with docker and --privileged
- BundlerGemNotFound Could not find mimemagic-0.3.5 in any of the sources on Rails project with Docker
- Calling an external service from within Minikube
- Build jar and run spring boot from cmd
- Building a highly scalable, failure tolerant flash sales backend
- Calling redis-cli in docker-compose setup
- Can a reference to an element inside an stdmap be invalidated?
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.