第 1 節

Introduction

0瀏覽次數0訪問次數--跳出率--平均停留

🐳 What is Docker?

Docker is an open-source application container engine that allows developers to package an application and all its dependencies into a single "container," enabling build once, run anywhere.

"Build once, run anywhere" means:

  1. Previously:

Previously, to run ROS2 + OpenCV + CUDA + CuDNN, I had to install and configure each environment one by one on a single computer. If I wanted to run it on another computer, I would have to go through the same setup process all over again. And if my system environment crashed and I needed to reinstall the OS, I would have to repeat the entire configuration process yet again — which was very tedious.

  1. After using Docker:

(The concepts of Docker images and Docker containers are explained further below. For now, just accept the concepts of images and containers as you read the following passage.)

I just need to configure this ROS2 + OpenCV + CUDA + CuDNN environment once on my computer using Docker, then generate an image from Docker and package it. From then on, on any computer, I can directly use this image to create a container, and that container will contain the ROS2 + OpenCV + CUDA + CuDNN environment I need. If my container environment crashes, I just delete the broken container and generate a new one from the image again. I only need to configure it once, and I can install this environment with one click in the future.

📦 How does it work?

  • Traditional approach: Running software requires installing various libraries and configuring environments on different systems, which is very cumbersome.
  • Docker method: Package into a "container," encapsulating both the environment and the application together, ensuring stable operation no matter where it runs.

🔧 A few basic concepts of Docker

conceptExplanation
Mirror imageImage, a template for running containers, like an application snapshot.
containerContainer, a running instance of an image, has its own file system, network, and more.
DockerfileA script for building the image, specifying which packages to install, which environment variables to set, and so on.
repositoryRegistry, a place to store images, such as Docker Hub.

Abstract Understanding:

A Docker image is roughly equivalent to a C++ class.

A Docker container is roughly equivalent to a C++ class instance (i.e., an object).

Visualization: An image can be likened to an ISO file used to install an operating system on a computer. A container can be likened to a system that has been installed on the computer and is ready to run.

When turning an image into a container, you need to use the docker run command with many parameters. This is understandable because it tells your computer exactly what hardware configuration it has.

🔍 Understanding by Analogy

Traditional deploymentDocker deployment
Manually install dependencies and debug version mismatch issues.Package the environment and code together at once.
The program runs "bare metal" on the system.The program runs in isolation within a container.
Easily "runs on my machine"Ensure it "runs anywhere"

Just like a delivery package: You no longer care about how the contents are transported, because the packaging has already handled all the isolation for you.

The core advantages of Docker

AdvantageExplanation
LightweightBased on system kernel sharing, it has fast startup speed and low resource usage.
cross-platformBuild once, run everywhere (consistent across Windows, Linux, and macOS).
Easy to migrate and deployThe application and environment are packaged together, eliminating concerns about dependency inconsistencies.
Easy to version controlMirror version is controllable and supports rollback.
Rich ecosystemThere are tens of thousands of ready-made images available on Docker Hub.
  1. On Linux, it can be achieved with almost no performance loss.

The distribution in Docker shares the Linux kernel with the host machine.

CPU overhead is less than 1%. Memory is near-native with no overhead. Disk overhead is less than 2%. Network performance is near-native with no overhead. Graphics card overhead is less than 1%.

  1. Can be quickly deployed on most Linux distributions.

If you want to run ROS2, it was previously easier to deploy only on Ubuntu. However, now you can use any distribution, such as Fedora, ArchLinux, and others, to run ROS2 via Docker.

  1. Setting up the environment is simple.

Previously, you had to install ROS2, CUDA, CuDNN, OpenCV4, and more on Ubuntu by following a tutorial. But as long as you use Docker, you can directly pull a pre-configured development environment from someone else using the docker pull command — just one command and you're there.

You only need to configure the most basic components of a distribution, such as switching repository mirrors, setting up the input method, and installing the graphics driver (just enough to get the graphics card working—no need to configure CUDA or cuDNN locally).

  1. Rich ecosystem

The ecosystem is extremely rich. Even if you don't build things yourself, you can find pre-built images on Docker Hub, saving you the trouble of compiling.

For example, when configuring CUDA and cuDNN in the past, you had to first install the NVIDIA driver on your local machine, then install CUDA and cuDNN. Now, we only need to install the NVIDIA driver locally. NVIDIA officially provides pre-compiled CUDA and cuDNN images on DockerHub, so we can use them directly.

📁 Common Docker Use Cases

  • Local development: quickly set up various development environments (such as Python + Jupyter, ROS + Gazebo)
  • Test deployment: automated testing, building, and deployment in CI/CD
  • Microservices architecture: one container per service, flexible composition
  • Research Tool Packaging: Reproducing someone else's paper environment, or packaging your own project for others to use.
音乐页