Home · Tags · Source Code

Docker - Build and Run One-liner

Tags: docker, tech

Last Updated on Sat, 12 Nov 2022 03:46:15 GMT - Edit Page

From the path containing the Dockerfile:

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

Add other flags if necessary, for example -p 8080:80 to map container port 80 to host port 8080:

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

Watch out for Dangling Images.


Command docker build builds an image from a Dockerfile.

Flag -q (quiet). Output the image ID only. In our case, the image ID is then passed directly to the docker run command.

Path to the Dockerfile. The . is used to specify the current directory.

$ docker build -q .

Command docker run creates and starts a new container.

$ docker run --rm IMAGE-ID

An image ID is required.

Flag --rm deletes the container when it exists.