Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Underlying Technologies  ›  Lesson

Shell Commands and Package Managers

Shell Commands 14 minImages are built by running shell commands, so you need everyday commands, simple scripts and the right package manager for the base image.

Shell Commands and Package Managers

quicknotes needs curl inside its image so a health check can call the app. Nobody clicks an installer inside a container: every tool gets there because a line in a Dockerfile ran a shell command. Before going deeper into Docker, you need the small slice of Linux those lines are written in.

The rule

A Dockerfile is a shell script that leaves a filesystem behind.

Each RUN line in a Dockerfile is handed to a shell (/bin/sh -c on Linux images). Whatever files the command creates or changes are saved into the image. So three skills carry you a long way:

Everyday commands, which you will also type inside running containers:

ls -l /app          # list files with owners and permissions
cat requirements.txt  # print a file
echo "$PATH"        # print a variable
mkdir -p /app/data  # create a folder, parents included

Package managers, which install software. Which one you use depends on the base image's Linux family:

# Debian or Ubuntu base
RUN apt-get update && apt-get install -y curl

# Alpine base
RUN apk add --no-cache curl

apt-get update downloads a fresh list of available packages; install -y answers "yes" automatically, because a build has nobody at the keyboard. Alpine's apk add --no-cache fetches the list just for this command and does not store it in the image.

Small scripts, often used as a container's start-up step:

#!/bin/sh
set -e
echo "Starting quicknotes"
exec python app.py

set -e stops the script at the first failing command instead of carrying on in a broken state. exec replaces the shell with the app, so the app receives stop signals directly.

The shortcut

Know the base, know the package manager. Look at the FROM line first: debian, ubuntu or a -slim Python/Node tag means apt-get; alpine means apk; Fedora-style bases use dnf. Then make every command non-interactive.

Adding curl to quicknotesStep 1 of 4
  1. Read the FROM line

    FROM python:3.12-slim is built on Debian, so the tool is apt-get.

The common mistake is putting apt-get update alone on one RUN line and the install on the next. Docker may reuse the old update layer from its cache, and the install then works from an outdated package list.

Drills

Answer fast — trust the rule.

quick drill

Your Dockerfile starts FROM alpine:3.20. Which command installs curl?

quick drill

Why does a Dockerfile use apt-get install -y rather than plain apt-get install?

quick drill

What does set -e do at the top of a shell script?

quick drill

Why join apt-get update && apt-get install -y curl in one RUN?

Remember

  • A Dockerfile is a shell script that leaves a filesystem behind: each RUN is a shell command whose results are saved.
  • Know the base, know the package manager: Debian/Ubuntu → apt-get, Alpine → apk, Fedora → dnf.
  • Trap: interactive prompts and a separate, cached apt-get update.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainInstall a tool in two bases

quicknotes needs curl for a health check. Write the install line for a Debian-based image and for an Alpine-based image, and explain each flag you used and why the build must never stop to ask a question.