Docker

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers.

Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.

Docker Docker Build and Run

Docker commands

  • docker info - show docker system info

Inspect (containers, networks, images, volumes)

  • docker inspect <container name> or <id> - show container info (IP address, etc)

  • docker inspect <network name> or <id> - show network info

  • docker inspect <image name> or <id> - show image info

  • docker inspect <volume name> or <id> - show volume info

Free up disk space

  • docker system df - show docker disk usage

  • docker builder du - show disk usage by build cache

  • docker system prune - cleans up dangling resources (unused data such as stopped containers, dangling images, networks, and more)

  • docker system prune -a -a removes all unused images (those that are not associated with any container) not just the dangling ones. NB! all images will be removed

  • docker builder prune - remove build cache

To capture docker build logs to a file

docker build -f Dockerfile.bfb_camera_d435i -t bfb_camera_d435i:latest ../.. 2>&1 | tee build.log 2>&1 redirects stderr to stdout, and then | tee build.log pipes stdout to tee, which writes it to build.log and also displays it on the screen.

Docker compose

Create, start, stop and remove containers

  • docker compose -f docker-compose_lenovo.yml up

  • docker compose up

  • docker compose up --build (Build images before starting containers.)

  • docker compose start- start the stopped containers, can’t create new ones

  • docker compose down - stop and remove containers, networks, images, and volumes

Docker Installation

  1. Docker Intallation Guide

  2. After installation, add your user to the docker group to avoid using sudo when you use the docker command: sudo usermod -aG docker $USER

Acess Nvidia GPUs in Containers (GPU-accelerated containers)

NVIDIA Container Toolkit enables users to build and run GPU-accelerated containers. The toolkit includes a container runtime library and utilities to automatically configure containers to leverage NVIDIA GPUs.

Official documentation

Note

This installation guide is for Linux only. In Windows GPU support is included in Docker Desktop. Dockerdocs

  1. Install Nvidia Container Toolkit: Nvidia Container Toolkit Installation Guide

  2. Restart Docker: sudo systemctl restart docker

  3. Test Nvidia Container Toolkit: docker run -it --rm --gpus all ubuntu nvidia-smi

  4. Run your container with GPU support:

    docker run -it --rm --runtime=nvidia \
    -e DISPLAY \
    -e NVIDIA_VISIBLE_DEVICES=all \
    -e NVIDIA_DRIVER_CAPABILITIES=all \
    -v /tmp/.X11-unix:/tmp/.X11-unix:rw \
    <image name>
    

Buildx

plugin that alllows to build images for different architectures (e.g. amd64, arm64, etc.) on a single machine.

  • docker info | grep -i buildx - check if buildx plugin is installed

  • sudo apt update && sudo apt install docker-buildx-plugin - install buildx plugin