Docker¶
Introduction¶
Whether you are developing a web application, a backend service, or a complex robotic algorithm, you almost always rely on external dependencies: specific libraries, software packages, and various configuration files.
You can install all these components directly onto your local machine, but this approach often leads to several critical bottlenecks:
Version Conflicts. For instance, one application might require Python 3.12, while another is only compatible with Python 3.8. Managing these conflicting requirements manually is difficult and risks breaking one project when updating another.
Environment Inconsistency. A program that runs perfectly on your laptop may fail on another computer or production server due to slight differences in the operating system, incompatible library versions, or missing dependencies.
Complex Setup Processes. Setting up a new project can be time-consuming. You may need to install databases, configure servers, and manually define file paths just to get a single application running.
System Clutter. Over time, your computer becomes bloated with unnecessary libraries, packages, and background services left over from abandoned projects.
To overcome these challenges, developers use containerization. One of the industry-standard tools for this is Docker.
Operating Principle¶
Docker allows you to package an application along with every single dependency it needs into a single, isolated unit called a container. Instead of installing components directly into your computer’s operating system, you run the application within its own dedicated environment.
A container typically includes:
Software components (e.g., Python, ROS 2, or OpenCV).
Configuration files: pre-set environments for both your program and its dependencies.
Runtime tools: all the libraries and components necessary for the software to execute.
The application itself.
To avoid manually configuring a container every time you want to run it, Docker uses images.
An image — serves as a read-only template used to create containers. It contains the file system, software components, and launch settings required for your application. You can create a single image and use it to launch multiple containers.
You can save an image and share it with a teammate or upload it to a cloud registry. When the recipient downloads the image, they run the application in a perfectly prepared environment, virtually eliminating errors caused by differing computer settings.
While Docker uses containers, it is not a full-scale Virtual Machine (VM). Unlike a VM, Docker does not require you to run a complete second operating system inside your own. Instead, it isolates the container’s processes and resources at the system level. This makes Docker containers significantly faster and much more resource-efficient than traditional virtual machines.
Docker simplifies dependency management, ensures environment reproducibility, and streamlines deployment by separating your application’s environment from the host operating system.
Installation¶
Warning
Only install Docker on your computer if you intend to create and manage your own containers.
The clover2 image already includes Docker. If you are working with the quadcopter using the pre-configured environment, you can skip this entire section
To proceed with the installation, ensure you have a stable internet connection and administrative privileges on your machine.
Ubuntu¶
For detailed information, refer to the Official Docker Engine for Ubuntu documentation.
Follow these steps in your terminal:
Update package lists and install
curl:
sudo apt update
sudo apt install curl
sudo apt updaterefreshes your local package index from the configured repositories.sudo apt install curlinstalls thecurlutility, used to transfer data via network protocols.
Run the official installation script:
curl -fsSL https://get.docker.com | sudo sh
This command downloads the official Docker installation script and pipes it directly into the shell to execute the setup with administrative rights.
Add your user to the docker:
sudo usermod -aG docker $USER
usermodmodifies user account settings.-aGappends the user to the specified group.$USERautomatically inserts your current username.
Apply the group changes:
newgrp docker
Windows¶
For detailed information, refer to the Official Docker Desktop for Windows documentation.
To run Linux containers on Windows, Docker Desktop requires WSL 2 (Windows Subsystem for Linux). This is also highly recommended for working with ROS 2.
Open PowerShell as an Administrator and run:
wsl --install. Follow the on-screen prompts and restart your computer if requested. Open theMicrosoft Storeand search forUbuntu 24.04.
Installing Ubuntu 24.04¶
Click
Getand wait for the installation to finish (the button will change toOpen). Restart your computer to finalize the installation.
Opening Ubuntu 24.04¶
Launch
Ubuntu 24.04from theStartmenu. On the first launch, you will be prompted to create a Linux username and a strong password. Remember this password; you will need it to executesudo(administrative) commands. Once setup is complete, you will see a command prompt ready for Linux commands.
Creating user in Ubuntu¶
Get the Docker Desktop installer from Official Docker website.
Downloading Docker Desktop for Windows¶
Run the downloaded
.exefile. If the installer presents configuration options, follow the recommended settings (see figure below).
Configuring Docker Desktop¶
Once the installation is complete, save your work and restart your computer if prompted. Upon the first launch, review and accept the Docker Service Agreement.
Docker Desktop first launch¶
Open the
Ubuntu 24.04terminal via theStartmenu. Grant your Linux user permission to interact with Docker by running:
sudo usermod -aG docker $USER
Refresh your current terminal session to apply the changes:
newgrp docker
Running Your First Container¶
In Docker, an image is the static, prepared environment, while a container is the active, running instance of that environment.
Let’s launch a container based on the Ubuntu image. Open your terminal and run:
docker run -it --rm ubuntu
Breakdown of the command:
docker runtells Docker to create and start a new container from a specific image.-itallows you to interact with the container via your keyboard.--rmautomatically removes the container once it stops running, keeping your system clean.ubuntu— the name of the image to be used. If you don’t have this image locally, Docker will automatically download it from the official registry.
Once the command runs, Docker will drop you directly into the container’s command line. Your prompt will change to look something like this:
$ docker run -it --rm ubuntu
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
Digest: sha256:d1e2e92c075e5ca139d51a140fff46f84315c0fdce203eab2807c7e495eff4f9
Status: Downloaded newer image for ubuntu:latest
root@d994718f32d0:/#
The root@...:/# prompt indicates that you are now operating inside the container as the root user (the administrative user for that specific isolated environment).
To demonstrate how Docker isolates processes from your main operating system, let’s install and run htop — a tool used to monitor running processes and resource usage.
Update the container’s package list:
apt update
apt update refreshes the list of available software within the container’s isolated environment.
Install htop:
apt install htop -y
The -y flag automatically confirms the installation prompt.
Launch htop:
htop
An interactive interface will open.
The process list only shows what is happening inside the container (such as bash and htop itself),
To leave the container, simply type exit.
Because we used the --rm flag when we started the container, Docker has already automatically deleted the container.
You can verify that the container has been removed by checking your container history:
docker ps -a
docker psdisplays currently running containers.-aincludes containers that have already exited or stopped.
In the output, you’ll get a table roughly like this:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Because of the --rm flag, your Ubuntu container will not appear in this list, confirming that Docker successfully cleaned up the environment after use.