Skip to content

Containers

Parse build arguments from a file

Terminal window
docker build -t foo \
$(while IFS= read -r line; do args+="--build-arg ${line} "; \above
done < <(cat .arg); \
echo "$args"; unset args) .

From here

Remove all ‘exited’ containers

Terminal window
docker rm $(docker ps --all -q -f status=exited)

Build and run container

Terminal window
docker build -t foo . && docker run --rm -it foo

Prune everything that shouldn’t exist

Terminal window
docker system prune -a -f

Remove all images except ‘latest’

Terminal window
docker images | grep -v "latest" | tail -n +2 | awk '{ print $3 }' | xargs --no-run-if-empty docker rmi

Improve ‘RUN’ execution in a Dockerfile

Benefit is that when a specific line fails, then the error message is much more concise as opposed to the standard method of using ampersands.

RUN set -eu; \
python3 -m venv venv; \
venv/bin/pip install -r requirements.txt; \
venv/bin/pip install -r requirements-dev.txt; \
echo 'Venv creation + requirements installation: OK';

Remove dangling images

Terminal window
docker rmi $(docker images -f "dangling=true" -q)