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".
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:
--name [containerName]
specify name instead of automatically generating one
-d
run in detach node
--memory="256m"
specify maximum memory
--cpus=".5"
specify maximum CPU load
--publish [localPort]:[containerPort]
map of port from host to container
-it -- /bin/bash
to attach bash to container
-v [volumeName]:[logicalPath]
to attach a volume
-v [path]
to attach a persistent local directory
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
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:
inherit specific features provided by shell
inherit .rc
files from shell and all environment variables
do not forward process signals to child processes (hard to kill by SIGINT
)
Exec Form:
forward process signals to child processes
when the first item is not executable, it automatically becomes a parameter (e.g. param1
) for the previous executable
Always use exec form. See this guide.
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
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]
When internet isn't able to connect during docker build
:
first test if docker run busybox nslookup google.com
would work
if not, then test if docker run --dns 8.8.8.8 busybox nslookup google.com
would work
make a /etc/docker/daemon.json
file
{
"dns": ["8.8.8.8"]
}
# 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