CodeTheRobot

Ubuntu Linux for Robotics · Lesson 29 of 96

Docker basics

Images, containers, running commands, mounting folders and ROS 2 images.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain docker basics in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

Docker runs programs in containers: isolated environments built from an image that contains an operating system layer and your software. This gives every developer and robot exactly the same ROS 2 installation regardless of the host. docker run starts a container, docker ps lists them and docker images lists images.

Mount folders into a container with -v host:container so files survive, and use --network host for ROS 2 discovery to work simply. The official osrf/ros images provide ROS 2. Give containers no more rights than they need; never run untrusted images with --privileged.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the docker basics commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

sudo apt install docker.io
# Install Docker from Ubuntu's repository.
sudo usermod -aG docker $USER
# Let your user run docker without sudo (log out and in afterwards).
docker run --rm hello-world
# Download and run a tiny test container.
docker run -it --rm --network host osrf/ros:lyrical-desktop bash
# Start an interactive ROS 2 container that shares the host network.
docker run -it --rm -v ~/robot_ws:/ws osrf/ros:lyrical-desktop bash
# Mount your workspace inside the container at /ws.
docker ps -a
# List all containers, including stopped ones.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · The robot's computer

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

robot computercpumemdiskdriverstoppedplannerstoppedloggerstoppedsafetystoppedTHE ROBOT'S COMPUTER

Demo for Docker basics. Expected result: Hello from Docker!

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

# TODO: run the hello-world image and remove the container afterwards

Expected output

Hello from Docker!
Stuck? Show the full solution (try it yourself first)
docker run --rm hello-world

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Run a ROS 2 container with your workspace mounted, build a package inside it, run a talker in the container and a listener on the host, and check that they see each other with --network host.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.