Docker

Installation

To install docker, follow Official Guide

sudo apt-get update
sudo apt-get install \
  ca-certificates \
  curl \
  gnupg \
  lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

After install, don't run docker with sudo. Instead, follow Post-Installation Step, especially for "Manage Docker as a non-root user" and "Configure Docker to start on boot".

Basic Commands

Image Commands

docker info
docker version
docker login
docker pull [imageName] # pull from registry
docker images # list images
docker rmi [imageName] # remove image
docker system prune -a # remove all unused images
docker run [imageName]  # run container
docker image inspect [imageName] # get image info
docker system prune -a # remove cache

Container Commands

docker start [containerName] # start stopped container
docker stop [containerName] # stop container
docker kill [containerName] # kill container
docker rm [containerName] # remove stopped container
docker rm $(docker ps -a -q) # remove all stopped container
docker ps -a # list running containers

Run Commands extra flags:

Image Building Commands

docker build -t [name:tag] -f [Dockerfile] # build image using a Dockerfile
docker tag [imageName] [name:tag] tag an existing image

Volume Commands

docker create volume [volumeName]
docker volume ls
docker volume inspect [volumeName]
docker volume rm [volumeName]
docker volume prune

Dockerfile

See Dockerfile Reference for details.

.dockerignore: avoid sending large or secret data to build images

FROM [BaseImage] # specify base-image
ENV [key] [value] # specify environmental variables
COPY [local file path] [container file path or directory] # copy from local repo (e.g. .) to container (e.g. /src)
WORKDIR [container directory path] # all commands will not run in work directory
EXPOSE [port] # docker will listening to this port inside container
RUN [Commands] # run any command in shell form
CMD ["executable", "param1", "param2"] # run command in exec form
CMD <command> # run command in shell form (/bin/sh -c)
ENTRYPOINT ["executable", "param1", "param2"] # run command in exec form

Shell Form:

Exec Form:

Always use exec form. See this guide.

Docker Compose

docker-compose: docker compose v1, originally shipped as python package

docker compose: a docker plugin v2, compatible with v1 commands

Since we need orchastration, docker compose is not covered here. For exact configurations, see this video

Registry

Use following commands to publish to Docker Hub

docker login -u [username] -p [password]
docker tag [imageName] [repoPath]:[tag]docker b
docker push [repoPath]:[tag]
docker pull [repoPath]:[tag]

Trouble Shoot

When internet isn't able to connect during docker build:

{
  "dns": ["8.8.8.8"]
}

Docker for CUDA

# first add repo (make sure your os version is the targeting os version)
distribution=$(. /etc/os-release;echo $ID$VERSION_ID) && curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add - && curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list

# then install nvidia's plugin
sudo apt-get update
sudo apt-get install -y nvidia-docker2

Now your /etc/docker/daemon.json should look like the following

{
    "runtimes": {
        "nvidia": {
            "path": "nvidia-container-runtime",
            "runtimeArgs": []
        }
    },
   "dns": ["8.8.8.8"]
}

Restart docker: sudo systemctl restart docker

You then run your image like this: docker run -it --gpus all docker.io/kokecacao/worker nvidia-smi

Table of Content