Docker Build and Run In One Command

Sep 13, 2019 min read

Option 1)

docker build -t myapp . && docker run -it myapp

Option 2) Preferred

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

By using the -q flag Docker will build the image according to our Dockerfile in quiet mode, and only return the hash of the generated Docker image. The hash value is then passed into the docker run... command.

I like to add the --rm flag so the container is removed when we exit the interactive session

Example:

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