Wiki
Introduction
1.🐳 What is Docker?
Docker is an open-source application container engine, which allows developers to package an application and all its dependencies into a "container",build once, run anywhere.
“Build once, run anywhere”That is to say:
- Previously:
Previously, when I wanted to run ROS2+OpenCV+CUDA+CuDNN, I needed to install and configure each environment one by one on a single computer. If I also wanted to run this on another computer, I would need to configure the second computer in the same way. If my system environment crashed and I needed to reinstall the operating system, after reinstalling I would yet again have to go through the configuration process once more, which is very troublesome.
- After using Docker:
(The concepts of Docker images and Docker containers are explained further below; for now, in the following passage, just accept the concepts of images and containers when you encounter them.)
I only need to configure this ROS2+OpenCV+CUDA+CuDNN environment once on my computer using Docker, then use Docker to generate an image and package this image properly. From then on, on any computer, I can directly use this image to generate a container, and this container will contain the ROS2+OpenCV+CUDA+CuDNN environment I need. If my container environment crashes, I only need to delete the broken container and regenerate a new container from the image. I only need to configure it once, and afterward I can install this environment with one click.
2.📦 How does it work?
-
Traditional approach: Running software requires installing various libraries and configuring environments on different systems, which is very troublesome.
-
Docker approach: Package it into a "container," encapsulating the environment and application together, so it runs stably no matter where it is. .
3.🔧 Several basic concepts of Docker
| Concept | Explanation |
|---|---|
| Image | A template for running containers, like an application snapshot |
| Container | A running instance of an image, with its own filesystem, network, etc. |
| Dockerfile | A script for building images, specifying which packages to install, which environment variables to set, etc. |
| Registry | A place for storing images, such as Docker Hub |
Abstract understanding:
Docker image ≈ C++ class
Docker container ≈ C++ class instance (i.e., object)
Visualized understanding: An image can be likened to an ISO image file used to install an operating system on a computer. A container can be likened to an operating system that has already been installed on the computer and can run.
When turning an image into a container, you need to use the docker run command to add many parameters, which can be understood as finding out what hardware configuration this computer actually has.
4.🔍 Analogical Understanding
| Traditional deployment | Docker deployment |
|---|---|
| Manually install dependencies and debug version inconsistency issues | Package the environment and code once |
| The program runs "bare" on the system | The program runs in an isolated "container" |
| Prone to "it runs on my machine" | Ensures "it runs anywhere" |
Like a courier package: You no longer care how the contents are transported, because the packaging has already done all the isolation for you.
5.✅ Core advantages of Docker
| Advantages | Description |
|---|---|
| Lightweight | Based on system kernel sharing, fast startup, low resource usage |
| Cross-platform | Build once, run anywhere (consistent on Windows, Linux, and macOS) |
| Easy to migrate and deploy | Applications and environments are packaged together, eliminating concerns about inconsistent dependencies |
| Easy version control | Image versions are controllable, with rollback support |
| Rich ecosystem | Thousands of ready-made images are available on Docker Hub |
- On Linux, this can be achieved with almost no performance loss.
The distributions in Docker share the Linux kernel with the host machine.
CPU loss is less than 1%. Memory is close to native with no loss. Disk loss is less than 2%. Network performance is close to native with no loss. GPU loss is less than 1%.
- It can be quickly deployed on the vast majority of Linux distributions.
You want to run ROS2, which previously was relatively easy to deploy only on Ubuntu, but now you can use any distribution, such as Fedora, Arch Linux, and other distributions, to run ROS2 via Docker.
- Simple environment configuration
Previously, you needed to follow tutorials to install ROS2, CUDA, CuDNN, OpenCV4, and so on on Ubuntu, but as long as you use Docker, you can directly use the docker pull command to pull a pre-configured development environment set up by someone else, and all it takes is a single command to reach Rome directly.
You only need to configure the most basic things of a distribution, such as mirror switching for repositories, input methods, graphics card drivers (you only need to get the graphics card working, no need to configure CUDA and CuDNN locally), etc.
- Rich ecology
The ecosystem is extremely rich; there are many things that, even if you do not build them yourself, you can find images already built by others on Docker Hub, saving yourself even the trouble of compilation.
For example, when configuring CUDA and cuDNN previously, you needed to first install the NVIDIA driver on the local machine, and then install CUDA and cuDNN. Now, we only need to install the NVIDIA driver on the local machine; NVIDIA officially provides images for CUDA and cuDNN on Docker Hub, which have already been compiled, and we can use them directly.
6.📁 Common Docker Use Cases
-
Local development: quickly set up various development environments (such as Python + Jupyter, ROS + Gazebo)
-
Testing and deployment: automatic testing, building, and deployment in CI/CD
-
Microservices architecture: one container per service, flexibly composable
-
Research tool packaging: reproduce others' paper environments, or package your own project to send to others for use