Wiki
Docker Command Learning
Existing English translations were reused. 1 current source blocks remain in Simplified Chinese, without triggering paid translation.
1.Reference Documentation
https://www.runoob.com/docker/docker-tutorial.html
2.Common Commands
Commonly used ones are marked in red, occasionally used ones are marked in green, and the rest just need to be understood.
| Command | Description | Example |
|---|---|---|
| docker run | Create and start a new container. | docker run -it ubuntu bash |
| docker build | Build a new image using the specified Dockerfile. | docker build -t myimage . |
| docker pull | Pull an image from a Docker registry. | docker pull ubuntu |
| docker push | Push a local image to a Docker registry. | docker push myimage |
| docker stop | Stop a running container. | docker stop container_id |
| docker start | Start an existing container. | docker start container_id |
| docker restart | Restart a container. | docker restart container_id |
| docker ps | List currently running containers. | docker ps |
| docker rm | Remove one or more stopped containers. | docker rm container_id |
| docker exec | Execute a command inside a running container. | docker exec -it container_id bash |
| docker logs | View the log output of a container. | docker logs container_id |
| docker images | List all local images. | docker images |
| docker rmi | Remove one or more images. | docker rmi myimage |
| docker network | Manage Docker networks. | docker network ls |
| docker volume | Manage Docker volumes. | docker volume ls |
| docker-compose up | Start all services defined in docker-compose.yml. | docker-compose up |
| docker-compose down | Stop and remove all services defined in docker-compose.yml and their associated resources. | docker-compose down |
| docker info | Display detailed information about the Docker system. | docker info |
| docker stats | View real-time resource usage (CPU, memory, etc.) of running containers. | docker stats |
| docker inspect | View detailed information (in JSON format) about a container or image. | docker inspect container_id |
| docker save | Save an image as a tar file. | docker save -o myimage.tar myimage |
| docker load | Load an image from a tar file. | docker load -i myimage.tar |
| docker tag | Add a tag to an image. | docker tag myimage myimage:v1 |
| docker buildx build | Build multi-architecture images using Buildx. | docker buildx build -t myimage . |
| docker buildx create | Create a new Buildx builder instance. | docker buildx create --use |
| docker buildx ls | List all available Buildx builder instances. | docker buildx ls |
| docker buildx use | Set the current Buildx builder instance. | docker buildx use mybuilder |
| docker buildx bake | Build images in batch using a Bake file. | docker buildx bake -f bake.hcl |
| docker buildx build --push | Build an image and push it to a registry. | docker buildx build --push -t myimage . |
| docker buildx build --platform | Build an image and generate support for multiple platforms. | docker buildx build --platform linux/amd64,linux/arm64 -t myimage . |
2.1.Parameters of the run command (very important)
| 参数/配置 | 功能说明 | 重要性与参考依据 |
|---|---|---|
| --name=ros_jazzy_opencv411_cuda128_cudnn971_noble | 指定容器名称,便于后续管理 | 替代随机生成的容器名。 |
| --gpus all | 允许容器访问宿主机所有GPU资源,需NVIDIA驱动支持 | 用于CUDA加速等GPU依赖任务。 |
| -e NVIDIA_DRIVER_CAPABILITIES=all | 启用NVIDIA驱动的全部功能(如CUDA、图形渲染) | 确保容器内GPU功能完整67。 |
| -dit | 组合参数:- -d:后台运行容器(Detached模式)- -i:保持标准输入(STDIN)开放- -t:分配伪终端(TTY) | 允许容器在后台运行并支持交互操作。 |
| --privileged | 赋予容器完全主机权限(可访问设备、内核模块等) | 用于需要直接操作硬件的场景(如访问USB设备),但存在安全风险。 |
| -v /dev:/dev \ | 映射宿主机的/dev目录 | 用于需要直接操作硬件的场景(如访问USB设备),这样可以热插拔,容器会直接看到宿主机生成的设备别名,但存在安全风险。 |
| --net=host | 共享宿主机网络命名空间(容器使用宿主机IP和端口) | 简化网络配置,无NAT,这样的话,网络效率更高,局域网设备更容易发现。 |
| --group-add audio--group-add video--group-add dialout | 将容器用户加入宿主机用户组:- audio:音频设备访问- video:视频设备访问- dialout:串口设备访问 | 避免权限问题(如避免无法调用摄像头、麦克风)。 |
| -e DISPLAY=$DISPLAY-e XAUTHORITY=/home/tungchiahui/.Xauthority-e WAYLAND_DISPLAY-e XDG_RUNTIME_DIR-e QT_QPA_PLATFORM=xcb | 配置图形显示环境:- 绑定宿主机显示接口(X11或Wayland)- 设置GUI应用渲染后端 | 支持容器内运行图形界面应用(如OpenCV可视化)。 |
| -v /tmp/.X11-unix:/tmp/.X11-unix:rw-v /dev/dri:/dev/dri | 挂载宿主机图形设备:- X11套接字目录- 直接渲染管理器(DRI)设备 | 实现容器内图形显示。 |
| -v $HOME/.Xauthority:/home/tungchiahui/.Xauthority:ro | 挂载X11认证文件(只读) | 确保容器有权连接宿主机显示服务。 |
| -v /run/user/1000/wayland-0-v /run/user/1000 | 挂载Wayland显示协议相关目录 | 支持Wayland协议的图形显示。 |
| --ulimit nofile=1024:524288 | 设置进程**最大可打开文件数(nofile)**的方式,用于控制容器或进程运行时的文件句柄数量限制。--ulimit <限制类型>=<软限制>:<硬限制> | 如果默认限制太小,可能会出现 "too many open files" 的错误。所以在容器运行或系统服务启动时,需要调大这个值。--ulimit nofile=4096:65536 |
| -v /home/tungchiahui:/home/tungchiahui | 挂载宿主机用户目录到容器内同名路径 | 实现宿主机与容器间文件共享(如代码、数据持久化)。 |
| -w /home/tungchiahui | 设置容器启动后的默认工作目录 | 直接进入项目路径,方便执行命令2324。 |
| tungchiahui/ros-opencv:jazzy-411-cuda128-cudnn971-noble | 镜像名称指定镜像及标签,包含:- ROS 2 Jazzy- OpenCV 4.11- CUDA 12.8- cuDNN 9.7.1 | 提供预配置的深度学习与机器人开发环境。 |
I have already written a very complete run command, you can just use mine directly.
The command below must be run under a regular user, not under the root user; in fact, whether or not you add sudowhether or not you add sudo -Edoes not matter.
The user has already been added to the docker group, so there is no need forsudoto run either. Secondly,sudoIf you run it, your$HOMEvariable will not change either, let alone if you add -E, in which case your$HOMEis even less likely to change.
sudo docker run --name=ros_opencv_cuda \
--gpus all \
-e NVIDIA_DRIVER_CAPABILITIES=all \
-e DISPLAY=$DISPLAY \
-dit \
--privileged \
--net=host \
--group-add audio \
--group-add video \
--group-add dialout \
-e XAUTHORITY=$HOME/.Xauthority \
-e WAYLAND_DISPLAY=$WAYLAND_DISPLAY \
-e XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR \
-e QT_QPA_PLATFORM=xcb \
-v /tmp/.X11-unix:/tmp/.X11-unix:rw \
-v /dev:/dev \
-v $HOME/.Xauthority:$HOME/.Xauthority:ro \
-v /run/user/$(id -u)/wayland-0:/run/user/$(id -u)/wayland-0 \
-v /run/user/$(id -u):/run/user/$(id -u) \
-v $HOME:$HOME \
-w $HOME \
tungchiahui/ros-opencv:humble-411-cuda128-cudnn970-jammy
Content you need to manually modify yourself:
-
NVIDIA_DRIVER_CAPABILITIES=all--gpus allIf you do not have an NVIDIA graphics card, please comment it out. -
After \--name, please give the container a name yourself.
-
For the repository name in the last line, please find the corresponding image yourself and fill it in.
-
ROS1 runs out of memory under Fedora distributions, so you need to add the following parameter. If you are not using Fedora and ROS1,please do not add it(In fact, ROS1 is rarely used now, and ROS1 has gradually been phased out).
--ulimit nofile=1024:524288 \
- If you want to log in to the container as the current user, you can add the following lines, but it is very strongly not recommended.
--user $(id -u):$(id -g) \
-v /etc/passwd:/etc/passwd:ro \
-v /etc/group:/etc/group:ro \
2.2.save and load
As is well known, due to force majeure, Docker Hub was blocked by 🇨🇳 in May 2024, so we cannot directly pull images.
There is a local approach: package the image into a compressed archive, then transfer it to another computer for Docker to read.
Packaging:
docker save -o myimage.tar myimage
Read:
docker load -i myimage.tar
2.3.Third-party Docker Hub repository (not a Docker Hub mirror repository)
You can upload your own image to some domestic image repositories (image hubs, not mirrors).