Docker Command Reference
Create/start/stop a Container
- Create and run a container
- List all (running and stopped) containers
- Run a command in a running container
- Stop/start/restart/remove a container
$ sudo docker stop <target-container>
$ sudo docker start <target-container>
$ sudo docker restart <target-container>
$ sudo docker rm <target-container>
- Check resource usage statistics of a container
- Check log output of a running container
Advanced Configurations
- Map persistent storage volume
# host-src: an absolute path or a name value
# options: rw, ro
$ sudo docker run --rm \
-v [host-src:]container-dest[:<options>] \
<target-container>
Manage Docker Images
- Create a docker image
$ sudo docker build . -t <image-name>:<tag> -f ./Dockerfile
# Do not use cache when building the image
$ sudo docker build . -t <image-name>:<tag> -f ./Dockerfile --no-cache
- List all images
- Remove a docker image
- Tag a docker image
- Create a new image from a container’s changes
# first check changes
$ sudo docker diff <target-container>
# if all changes are okay, commit changes
$ sudo docker commit -a "author-name" -m "change msg" <target-container> <image-name>:<tag>
- Push/pull image from/to a docker registry
Docker Compose
Sample compose
version: '3'
services:
service_one:
privileged: true
devices:
- /dev/video0:/dev/video101
image: "sample-image:latest"
mem_limit: 512m
mem_reservation: 128M
logging:
driver: "json-file"
options:
max-file: "5"
max-size: "10m"
network_mode: "host"
volumes:
- "/opt/config.yaml:/config.yaml"
command: ["arg1", "/config.yaml"]
restart: unless-stopped
service_two:
image: "sample-image:latest"
user: root
logging:
driver: "json-file"
options:
max-file: "5"
max-size: "10m"
network_mode: "host"
command: ["arg1", "arg2", "arg3"]
restart: unless-stopped
Note that you need to enable cgroup function for resource limit (ram/cpu). The reservation memory size should be smaller than the memory limit. [3]
Check Resource Usage
Nvidia Docker
You may need to pass in additional arguments to use the nvidia runtime:
Allow containers to communicate with Xorg
$ sudo xhost +
$ sudo docker run -it --rm --net=host --runtime nvidia -e DISPLAY=$DISPLAY -v /tmp/.X11-unix/:/tmp/.X11-unix nvcr.io/nvidia/l4t-base:r34.1
Option explained:
- -it means run in interactive mode
- --rm will delete the container when finished
- --runtime nvidia will use the NVIDIA container runtime while running the l4t-base container
- -v is the mounting directory, and used to mount hostÂs X11 display in the container filesystem to render output videos
Reference
- [1] https://docs.docker.com/engine/reference/commandline/run/
- [2] https://catalog.ngc.nvidia.com/orgs/nvidia/containers/l4t-base
- [3] https://docs.docker.com/config/containers/resource_constraints/