Occasionally, it's just annoying to build a docker image, then run the container from that image. Even if it's only two 2 commands, it can become very annoying when done repetitively.
Luckily, there is a command to run a container directly from a Dockerfile.
docker run $(docker build -q .)
- The
-q
indicates the βquiet modeβ option, that suppresses the build output and prints image ID on success. - The
.
specifies the directory containing the dockerfile. $(...)
: This syntax captures the output of the command inside the parentheses and uses it as part of another command. In this case, it captures the image ID from the build command to use withdocker run
.
If you have to expose a port for your container, you can just add the -p
flag to your command, and there you go!
docker run -p 8000:8000 $(docker build -q . -t image-name:tag)
^ ^
| |
| 'port in your container'
|
'port in your computer'
- The
-t
assigns a nameimage-name:tag
to the image. docker run -p 8000:8000 ...
β This part creates and runs a container from the image built in the previous step. The -p 8000:8000 option maps port 8000 of your local machine to port 8000 of the Docker container, allowing you to access any services running on that port inside the container from your computer.
π. Similar posts
The Simple Way to Run a Long Docker Command in Multiline
14 Jan 2025
How to Hard Reset Your Git Repository to 10 Minutes Ago
04 Sep 2024
How to Easily Generate a Java Project Using Maven Command
04 Sep 2024