Skip to content

Instantly share code, notes, and snippets.

@ohjho
Last active January 13, 2024 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohjho/af86af1c1dfab4979f6d0543000351cd to your computer and use it in GitHub Desktop.
Save ohjho/af86af1c1dfab4979f6d0543000351cd to your computer and use it in GitHub Desktop.
my docker cheatsheet

basics

  • check that docker is working: docker run hello-world
  • prune images: docker image prune -a
  • prune containers: docker container prune
  • check nvidia-docker2 is working (with CUDA 10.0 in base): docker run --rm --gpus all nvidia/cuda:10.0-runtime nvidia-smi
  • build with Dockerfile: docker build -t your_image_name .
  • run built image as container: docker run --name your_container_name your_image_name (for extra options see here)
  • kill running container: docker kill your_container_name

Mount a volume into the docker container

use the -v flag, You need to give the absolute local path or a volume name and map it to a directory within the container -v <source>:<target>. See this stackoverflow post or this article on freecodecamp for more details.

docker run -it --rm -v $(pwd)/dir/to/share:/container/path/to/shared/dir --name your_container_name your_container_id

Make Docker container start automatically on system boot

docker run --restart=always

Note that with this you cannot use the option --rm. Credit to this stackoverflow post and the official doc

watch a running container

once in interactive mode it's kind of hard to exit without killing the container. The best way to watch a container is actually by combining watch and docker logs. For example:

watch -n 0.1 "docker logs --tail 30 your_container_name_or_id"

Debug something in your Dockerfile

best to just run the base image interactively and try the command in bash:

docker run -it --rm {image:tag} /bin/bash

or to override an image's entry point:

docker run -it --rm --entrypoint /bin/bash {image:tag|image_id}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment