Docker, start to ship · Chapter 22 of 26

Don't run as root

By the end of this chapter you can make an image run its app as an ordinary user, give that user the files it writes, and check from inside a container who it runs as. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Production habits
  • 4 tasks
  • Docker
  • In the all labs plan

The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.

Why you need this

Ravi is going through the launch checklist, and question one is "which user does each app run as?" For Tapri's orders API, chai-22-api, nobody chose: Bunty's app/Dockerfile never says, so the app runs as whatever the base image starts with. If somebody breaks into the API through a bug, they get exactly the powers of the API's user.

Everything you have built so far runs as that default user. Changing things by hand inside a running container does not help: the next container from the image starts the old way again. The user has to be written into the image.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Build chai-22-api:2.0 with a user that is not root

    Make the orders API image run as an ordinary user, not as root.

    1. Find an ordinary user that the base image node:24-alpine already has.

    2. In app/Dockerfile, add a USER line with that user. Put it before the CMD line.

    3. Build the image from the app folder as chai-22-api:2.0.

    4. Inspect the image to see its user.

    To pass: the image chai-22-api:2.0 has a user that is not root and not uid 0.

  2. Task 2

    Replace chai-22-api with one from 2.0 on port 8022

    Replace the old orders API container with one from your new image.

    1. Delete the running chai-22-api.

    2. Run a new chai-22-api from chai-22-api:2.0, in the background, with its port 8022 published on the lab's port 8022.

    3. If it stops at once, read its logs. The app writes boot.log into /app/data when it starts.

    4. Fix app/Dockerfile, build 2.0 again, and replace the container again.

    5. Run curl localhost:8022. It shows the uid the app runs as.

    To pass: chai-22-api is running from chai-22-api:2.0, as a user that is not root. That user owns /app/data inside the container. localhost:8022 shows that user's uid.

  3. Task 3

    Ask chai-22-api who it runs as, from inside

    Ask the new container which user it runs as, from inside it.

    1. Start the new chai-22-api from chai-22-api:2.0 first (task 2).

    2. Run id (or whoami) inside chai-22-api with docker exec.

    3. A uid of 0 is root. Any other uid is an ordinary user.

    To pass: docker exec chai-22-api id (or whoami) worked, after your last docker run of chai-22-api:2.0. Type both in the terminal, in the chapter folder. If you run the container again, run docker exec again too.

  4. Task 4 · interview question

    Interview: why root in a container is risky

    Interview question: why is it risky to run an app as root inside a container?

    1. Think: what is root in the container on the host (the lab machine here)?

    2. Think: what could somebody who breaks into the app reach?

    3. Write one or two sentences in your own words in answer.txt, in the chapter folder.

    To pass: answer.txt says what root in the container can mean for the host.

What you will learn

  • USER: the Dockerfile line that says who runs the app.
  • COPY --chown: hands the copied files to that user, so the app can still write where it needs to.
  • docker exec <container> id: asks a running container who it is.

More in Production habits

Build "Don't run as root" yourself

Tapri’s menu site works on one laptop and nowhere else. You ship the machine. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.