itsakettle

#6 | Docker build, run and remove

A mega simple script to build a docker image, run the container and then remove the container. This is useful when you’re interating or you want to repeatrun the container like a script or binary many times.

#!/bin/bash
dockerfile=$1
image_name="image_${dockerfile}"
docker build -t $image_name -f "./${dockerfile}" . && docker run -t --rm $image_name
echo $?

If this was in an executable file called run_dockerfile.sh then you could do

./run_dockerfile.sh my_app.docker

Possible improvements:

It seems you can achieve something similar with docker-compose. However when I tried it I had some difficulty getting stdout and stderr.