Belajar
← All lessons

How containers work

From image to running process

Image
Runtime
Host kernel
Container

docker-run.sh

# docker run --memory=256m --cpus=0.5 shop, roughly# 1. one filesystem: image layers + a writable onemount -t overlay overlay \  -o lowerdir=l4:l3:l2:l1,upperdir=rw,workdir=tmp /merged# 2. a cgroup with the limits, then join itmkdir /sys/fs/cgroup/shopecho 268435456 > /sys/fs/cgroup/shop/memory.maxecho "50000 100000" > /sys/fs/cgroup/shop/cpu.maxecho $$ > /sys/fs/cgroup/shop/cgroup.procs# 3. new namespaces, then the app runs as PID 1 insideunshare --pid --uts --net --mount --ipc --fork \  chroot /merged node /app/server.js

Rebuild after a change which layers come from cache

You changedNothingserver.jspackage.jsonDockerfile orderDeps firstCopy all first
docker build

The cache holds the last build of this same Dockerfile, with the files before your change

Dockerfile

FROM node:24-slimWORKDIR /app# dependencies first, so code edits don't rerun npm ciCOPY package*.json ./RUN npm ciCOPY . .CMD ["node", "server.js"]

build-cache.js

// how the builder picks a cached layerlet key = "";for (const step of dockerfile) {  const files = step.copies ? checksum(step.copies) : ""; // COPY also hashes the files it copies  key = hash(key, step.text, files); // each key includes the one before it  if (cache.has(key)) {    layers.push(cache.get(key));  } else {    layers.push(run(step)); // a miss changes every key after it  }}

Hit the memory limit a cgroup at work

memory.max256 MB
Memory in use
Events

    The host has 16 GB free. The limit belongs to this cgroup alone

    server.js

    const held = [];app.post("/grow", (req, res) => {  // alloc fills the buffer, so the pages are really used  held.push(Buffer.alloc(64 * 1024 * 1024));  res.send(`${held.length * 64} MB held`);});

    memcg.js

    // the kernel, every time the cgroup asks for pagesfunction charge(cgroup, bytes) {  if (cgroup.usage + bytes > cgroup.memoryMax) {    reclaim(cgroup); // drop page cache first  }  if (cgroup.usage + bytes > cgroup.memoryMax) {    return oomKill(cgroup); // SIGKILL, exit code 137  }  cgroup.usage += bytes;}

    Topics

    Next: How CI/CD works

    Open lesson