Docker cheat sheet to handle the Docker universe

Before a while I got in touch with the wonderful world of the Docker (see https://www.docker.com) cosmos. I think it is one of the most important virtualisation technology at the moment.

On this page I will just collect useful commands that helped me a lot in my daily work with Docker, Docker-Compose (https://docs.docker.com/compose/) and other stack related tools such AWS Docker registries. Additionally I will continuously collect useful links, that helped me to get along in the wide field of docker.

Useful Docker commands:

  • run an  arbitrary Docker image (available on Dockerhub) and open a shell into the launched machine:
    $> docker run -i -t python:3.5.1 /bin/bash
    or an alpine based one:
    $> docker run -i -t python:3.6.5-alpine3.7 /bin/ash
  • list all running containers
    $> docker ps
  • mount folder into a container to have access to the files in the current folder:
    $> docker run -v $PWD:/path/inside/docker/container -i -t python:3.6.5-alpine3.7 /bin/as
  • or a file (overrides existing targets f.e. if you want to replace a configuration file at runtime that was created during buildtime of the container image)
    $> docker run -v $(pwd)/test/deploy_conf.yml:/app/config/deploy_conf.yml -i -t mina:test /bin/ash
  • list all orphaned volumes (not used by any container anymore):
    $> docker volume ls -f dangling=true
  • remove a single image:
    $> docker volume rm VOLUMENAME
  • remove all orphaned volumes at once:
    $> docker volume ls -qf dangling=true | xargs --no-run-if-empty docker volume rm
  • t.b.c

Useful Docker shortcuts:

I placed these shortcuts inside my .bash_alias file under Linux, so that I have autocomplete / suggests:

###################
# Docker shortcuts
###################

# start the deamon
alias docker_start_deamon='sudo service docker start'
alias dd_start='docker_start_deamon'

# list running containers (image instances)
alias docker_process_list='docker ps -a'

# list all images
alias docker_list_images='docker images'

# list all container
alias docker_list_container='docker container ls'

# list all volumes
alias docker_list_volumes='docker volume ls'

# list all orphaned volumes
alias docker_list_orphaned_volumes='docker volume ls -f dangling=true'

# kill all running container processes:
alias docker_kill_all_container='(docker ps -q | xargs --no-run-if-empty docker kill) && docker container prune -f'

# remove all containers
alias docker_rm_all_container='docker ps -aq | xargs --no-run-if-empty docker rm -v'

#remove all docker images
alias docker_rm_all_images='docker images -aq | xargs --no-run-if-empty docker rmi -f'

#remove all docker volumes
alias docker_rm_all_volumes='docker volume ls -q | xargs --no-run-if-empty docker volume rm'

# remove all orphaned docker volumes
alias docker_rm_all_orphaned_volumes='docker volume ls -qf dangling=true | xargs --no-run-if-empty docker volume rm'

# remove everything
alias docker_rm_all_*='docker_rm_all_container && docker_rm_all_images && docker_rm_all_volumes && docker_rm_all_orphaned_volumes'

# build image from current folder
alias docker_build_here='dd_start && docker build --rm -t dockerbuildimage .'
alias d_b_h='docker_build_here'

# run current folder build image
alias docker_run_here='dd_start && docker run -it --rm  dockerbuildimage'
alias d_r_h='docker_run_here'

# combined tasks:
alias docker_build_and_run_here='d_b_h && d_r_h'
alias d_br_h='docker_build_and_run_here'

###########################
# Docker Compose shortcuts
###########################

# docker compose (assumes current directory contains the docker-compose.yml)
alias dc='docker-compose '

# start socker stack defined by docker-compose.yml
alias dc_up='docker-compose up -d'

# stop docker stack defined by docker-compose.yml
alias dc_down='docker-compose down'

# rebuild docker stack
alias dc_rebuild='docker-compose up -d --build'

# watch logs in docker stack
alias dc_logs='watch -n2 "docker-compose logs | tail -f"'

Further information about the Docker cosmos:

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.