Skip to main content

Lab 6: Demystifying Docker

Published

2026-12-02

This lab answers the comprehension questions from Chapter 6 of DO4DS (Demystifying Docker). Lab 5 promoted code through dev, test, and prod with Git and CI/CD. This lab packages that code in containers, starting with how Dockerfiles, images, registries, and containers fit together. Then we’ll install Docker Desktop on Pop!_OS 24.04, sign in, and prepare the Python and R APIs for containers. The DO4DS sixth lab puts the penguin model API in a container.

The original files in the DO4DS _labs/lab6 folder are below:

├── Dockerfile
├── app.py
└── vetiver_requirements.txt
1
Dockerfile generated by vetiver: starts from python:3.9, installs the requirements, and runs the API with uvicorn on port 8080
2
vetiver API that reads the pinned penguin_model from a pins folder board (./model)
3
Python packages for the API, pinned with pip-compile

Comprehension questions

Docker mental map

“Draw a mental map of the relationship between the following: Dockerfile, Docker Image, Docker Registry, Docker Container.”

  1. A Dockerfile is a plain-text recipe that describes exactly what goes into an image.
  2. Running docker build executes the Dockerfile instructions and produces a Docker Image (an immutable, layered snapshot of the filesystem and configuration).
  3. Docker Images can be pushed to a Docker Registry (such as Docker Hub or GitHub Container Registry) for storage and sharing.
  4. Running docker run on an image creates a Docker Container, a live, isolated process that uses the image as its read-only base.

%%{init: {'theme': 'base', 'look': 'classic', 'themeVariables': {'fontFamily': 'monospace', 'fontSize': '16px'}}}%%

graph LR
    subgraph Build["<strong>Build</strong>"]
        DF("<strong>Dockerfile</strong>")
        Img("<strong>Docker Image</strong>")
        DF -->|"<code>docker<br>build</code>"| Img
    end

    subgraph Share["<strong>Share</strong>"]
        Reg[("<strong>Docker<br>Registry</strong><br>(Docker Hub, GHCR)")]
        Img -->|"<code>docker<br>push</code>"| Reg
        Reg -->|"<code>docker<br>pull</code>"| Img
    end

    subgraph Run["<strong>Run</strong>"]
        Con("<strong>Docker<br>Container</strong>")
        Img -->|"<code>docker<br>run</code>"| Con
    end

    style Build fill:#fbf7ec,stroke:#5B8C5A,color:#1B2A41
    style Share fill:#fbf7ec,stroke:#2A6F77,color:#1B2A41
    style Run fill:#fbf7ec,stroke:#2A6F77,color:#1B2A41
    style DF fill:#5B8C5A,stroke:#000000,stroke-width:1px,color:#ffffff
    style Img fill:#D2562B,stroke:#000000,stroke-width:1px,color:#ffffff
    style Reg fill:#1B2A41,stroke:#000000,stroke-width:1px,color:#ffffff
    style Con fill:#2A6F77,stroke:#000000,stroke-width:1px,color:#ffffff

Docker components

The key distinction is that an image is static: it never changes after it is built. A container is ephemeral: it runs, does work, and can be stopped and removed. Multiple containers can be created from the same image simultaneously, each isolated from the others.

Docker run flags

“When would you want to use each of the following flags for docker run? When wouldn’t you? -p, --name, -d, --rm, -v”

Flag Use Don’t Use
-p <host>:<container> The container runs a service (API, Shiny app, database) that needs to be reachable from the host or network The container does not expose a network service and runs entirely internally
--name <name> You need to reference the container by name in other commands (docker logs, docker stop, docker exec), or when running multiple containers that need to find each other Running a quick one-off command where the auto-generated name is fine
-d Running a long-lived background service (a web server, an API) where you want the terminal back immediately You need to watch live output, or the container runs a short job and you want to see when it finishes
--rm Short-lived, disposable jobs: running a script, rendering a document, or any task where you do not need to inspect the container afterward Long-lived services you want to restart later, or when you may need to examine the stopped container to debug a failure
-v <host_path>:<container_path> Persisting data beyond the container’s lifetime, sharing config files or secrets, or mounting code during development so changes are reflected without rebuilding The container should be fully self-contained and stateless; mounting volumes couples the container to the host filesystem and can undermine reproducibility

Dockerfile commands

“What are the most important Dockerfile commands?”

Every Dockerfile starts with FROM, which names the base image (e.g., FROM rocker/r-ver:4.5). All subsequent instructions build on top of it.

Command Purpose
FROM Set the base image (always the first instruction)
RUN Execute shell commands during the build (install packages, create directories)
COPY Copy files from the build context into the image
WORKDIR Set the working directory for all subsequent RUN, COPY, CMD, and ENTRYPOINT instructions
ENV Set environment variables available at both build time and runtime
EXPOSE Document which port the container listens on (informational; does not publish the port)
CMD Default command to run when the container starts; can be overridden at docker run
ENTRYPOINT Main executable for the container; CMD provides its default arguments

CMD and ENTRYPOINT

The two commands that most often cause confusion are CMD and ENTRYPOINT:

  • CMD is the default command and is easily replaced (docker run my-image Rscript other.R)

  • ENTRYPOINT locks in the executable so the container always behaves like that program (docker run my-api --port 8080 passes --port 8080 as arguments to the entrypoint)

For most data science services, CMD is enough, and both Dockerfiles in this lab use it. ENTRYPOINT earns its place when the container should always start the same program and only its arguments change.

Docker Containers & Model APIs

This lab involves creating Docker containers for the APIs we created in R and Python in Lab 3 (see my solutions here).

Docker setup

I’ll cover how to install and set up Docker Desktop on the Pop!_OS 24.04 operating system because it wasn’t very clear from the online documentation.

  1. Download the Docker Desktop .deb from docs.docker.com/get-docker.

    • Docker Desktop depends on docker-ce-cli, which is not in the default Pop!_OS repos. Add Docker’s official APT repository before installing the .deb.
  2. Install the following prerequisites

    sudo apt install ca-certificates curl gnupg lsb-release
  3. Add Docker’s GPG key:

    sudo install -m 0755 -d /etc/apt/keyrings
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    sudo chmod a+r /etc/apt/keyrings/docker.gpg
  4. Add the Docker APT repository (use ubuntu noble for Pop!_OS 24.04):

    echo \
      "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
      https://download.docker.com/linux/ubuntu noble stable" | \
      sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt update
  5. Install Docker Desktop. This install may print a permission warning about _apt; this is harmless and the install completes normally.

    sudo apt install ./Downloads/docker-desktop-amd64.deb
  6. Verify the install and start Docker Desktop:

    docker --version
    # Docker version 29.10.1, build c48360f
    systemctl --user start docker-desktop

Docker Desktop

Docker Desktop

Docker Desktop is installed from a .deb file, not an apt repository, so sudo apt upgrade doesn’t update it. To update, stop Docker Desktop, download the newest .deb (the link always points to the latest release), and install it over the old version:

systemctl --user stop docker-desktop
curl -LO https://desktop.docker.com/linux/main/amd64/docker-desktop-amd64.deb
sudo apt install ./docker-desktop-amd64.deb
systemctl --user start docker-desktop

Images, containers, and the sign in survive the update. Confirm the new version with dpkg -l docker-desktop.

Signing in

On Linux, Docker Desktop uses pass (the GPG-based password store) as its credentials backend, storing your Docker ID token in GPG-encrypted files. Docker Desktop will show a warning and refuse to sign in if pass is not initialized first. This setup must happen before clicking Sign in in the UI.

  1. Install pass and gpg if they are not already present:

    sudo apt install pass gpg

    Both may already be installed; apt then reports their versions and installs nothing:

    pass is already the newest version (1.7.4-6).
    gpg is already the newest version (2.4.4-2ubuntu17.6).
    0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
  2. Generate a GPG key. Enter a name and email when prompted. GnuPG 2.4 (the Pop!_OS 24.04 version) doesn’t ask for a key type; it creates an ed25519 key with a cv25519 subkey:

    gpg --generate-key

    The output includes a pub block; copy the long hex string on the line below pub (the GPG key ID):

    pub   ed25519 2026-06-28 [SC] [expires: 2029-06-27]
          <pub-id>
    uid                      martin frigaard <mjfrigaard@pm.me>
    sub   cv25519 2026-06-28 [E] [expires: 2029-06-27]
  3. Initialize pass with the key ID:

    pass init <pub-id>
  4. Sign in from the Docker Desktop UI (Sign in button in the top-right) or from the Terminal:

    docker login

    docker login starts a web based login: it prints a one time device code and a link to open in your browser. Docker then stores the credentials via pass. (docker login -u <username> prompts for a password or token instead.)

    USING WEB-BASED LOGIN
    
    i Info → To sign in with credentials on the command line, use 'docker login -u <username>'
    
    
    Your one-time device confirmation code is: ABCD-EFGH
    Press ENTER to open your browser or submit your device code here: https://login.docker.com/activate
    
    Waiting for authentication in the browser…
  5. After authentication is complete in the browser, you’ll see the following:

    Docker Desktop then shows that you are signed in:

The Python API folder

The Python API has been moved into _labs/lab06/Python/api:

lab06/Python/
└── api
    ├── api.Rproj
    ├── mod-api.py
    ├── model.py
    ├── models/
    │   └── penguin_model/
    │       └── 20251224T142035Z-c115b/
    ├── my-db.duckdb
    ├── README.md
    ├── requirements-api.txt
    └── requirements.txt

5 directories, 10 files

The R API folder

The R API has been moved into _labs/lab06/R/api:

lab06/R/
└── api
    ├── api.Rproj
    ├── model.R
    ├── models/
    │   └── penguin_model/
    │       └── 20251218T152129Z-74445/
    ├── my-db.duckdb
    ├── plumber.R
    ├── README.md
    ├── renv
    │   ├── activate.R
    │   ├── library/
    │   ├── settings.json
    │   └── staging
    └── renv.lock

9 directories, 9 files

Recap

  1. A Dockerfile is the recipe, docker build turns it into an image, a registry stores and shares images, and docker run starts a container from an image. Images are static, containers are ephemeral, and many containers can run from the same image at once.

  2. Each docker run flag fits a specific situation: -p for services that need to be reachable and -d for long-lived background services. --name helps when other commands need to reference the container, --rm suits disposable jobs, and -v keeps data that should outlive the container. In the Dockerfile itself, CMD starts the API in both labs; ENTRYPOINT fits when only the program’s arguments should change.

  3. On Pop!_OS 24.04, Docker Desktop needs Docker’s APT repository before the .deb will install. Signing in needs pass, initialized with a GPG key. The Python and R APIs from Lab 3 now live in _labs/lab06/, ready to containerize.

We’ll build and run the first container in Python API in a container.