Build and run Dockerfile with one command
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Docker usually separates image creation from container startup. You run docker build to produce an image and docker run to start a container from that image.
If you want a one-liner for quick development work, you can combine both steps. The trick is to let docker build output the image ID and pass that ID directly into docker run.
The Basic One-Command Pattern
The simplest version looks like this:
Here is what each part does:
- '
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 shell' - '
docker runstarts a container from that temporary image reference' - '
--rmremoves the container when it exits'
This is convenient for fast local testing because you do not need to invent a tag name for every run.
A Complete Example
Suppose you have this Dockerfile:
And this small program:
You can build and run it immediately:
The output will be:
If the container exposes a service, add port mapping and any required environment variables:
That is still one shell command even though it performs two Docker operations internally.
When This Approach Is Useful
This pattern is best for short feedback loops:
- testing a sample Dockerfile
- running a throwaway container during development
- verifying that a code change still builds and starts
It is less useful when you need a reusable, named image. In that case, tagging the image explicitly is clearer:
That is two commands, but it is easier to debug, easier to repeat, and easier to share with teammates.
Variations You Will Actually Use
If your Dockerfile has a non-default name or lives elsewhere, include -f:
If you need build arguments:
If the container should stay interactive:
The pattern is flexible because anything that produces a valid image reference can be substituted into docker run.
Tradeoffs and Limitations
The one-liner is handy, but it hides the image name. That can make troubleshooting harder because the image is identified only by a hash unless you inspect the shell-expanded command.
It also does not remove the built image automatically. --rm cleans up the container, not the image. If you repeat the command many times and the build cache changes often, your machine can accumulate unused images.
Finally, command substitution is a shell feature. It works in shells such as bash and zsh, but the exact quoting rules can differ across environments. If you need maximum portability across scripts and CI systems, the explicit two-step form is often safer.
Common Pitfalls
- Assuming
--rmdeletes the image. It only removes the stopped container. - Forgetting quotes around the command substitution in shells where whitespace handling matters.
- Using the pattern in CI when a tagged image would make logs and debugging clearer.
- Expecting this to work the same way in every shell without checking the shell's command-substitution rules.
Summary
- You can build and run a Docker image in one shell command with
docker run "$(docker build -q .)". - The
-qflag makesdocker buildprint only the image ID. - This approach is useful for fast local experiments and throwaway runs.
- It is less clear than tagging the image explicitly when you need repeatability or debugging.
- '
--rmremoves the container, not the built image.'

