Docker cheatsheet

Tue 30 March 2021
# run docker container with mounted host directory
# (use a full absolute path for <host_dir>
docker run -v <host_dir>:<container_dir> -it <image_id|image_name> /bin/bash

# connect to a running docker container
docker ps
# >> container_id    image    container_name
# >> ...             ...      ...
docker exec -it <container_id|container_name> /bin/bash

# stop all containers
docker stop $(docker ps -a -q)

# remove all stopped containers
docker rm $(docker ps -q -f status=exited)

# remove dangling (none) images after building
docker rmi $(docker images -f “dangling=true” -q)

# remove images which have none tag or repository name
docker rmi $(docker images | grep "none" | awk '{print $3}')

# get docker container id from within container
cat /etc/hostname

# build docker image with custom name
docker build -t <image_name> .

# cleanup - remove all containers and all  images
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)

Category: Snippets [Docker]