# Run an Echo App as a Non-Root User in Docker

Yes. An agent adds an unprivileged user to the runtime stage of your Echo app's Dockerfile, makes the SQLite path writable, proves it in a container, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-non-root-user/echo/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the image before it changes it. In an API laid out like xesina/golang-echo-realworld-example-app, `go.mod` declares `go 1.18` with `labstack/echo/v4` 4.1.16 and `jinzhu/gorm` 1.9.8 on the SQLite dialect, which needs cgo. The `Dockerfile` builds on `golang:1.12.1` with `GOOS=linux GOARCH=amd64`, runs `go mod download`, then `go install -installsuffix cgo -ldflags="-w -s"` and copies the binary to `/app`; the runtime stage is plain `alpine`, installs `sqlite`, `tzdata`, `ca-certificates` and `bash`, sets `TZ=Asia/Tehran`, symlinks `/lib64/ld-linux-x86-64.so.2` to musl's loader because the cgo binary was linked against glibc in the build stage, sets `WORKDIR /app`, exposes 8585 and runs the binary. There is no `USER` line, so the process is root, and there is no `.dockerignore`, so `COPY . /src` sends `.git` and any local `realworld.db` into the build. `db/db.go` opens `./realworld.db` relative to the working directory, which is `/app`, and `main.go` listens on `127.0.0.1:8585`. The `Makefile` has `container` and `run-container` targets, and `.github/workflows/go.yml` builds and tests on Go 1.18 without touching Docker.

The runtime stage gains a group and a user created with `addgroup -S app` and `adduser -S -G app app`, a `chown -R app:app /app` after the binary is copied, and `USER app` before `CMD`. The ownership matters because the SQLite file is created on first start inside `/app`; a read-only directory would make `gorm.Open` fail at the first `AutoMigrate`. The symlink, the packages and `TZ` stay as they are, since none of them depend on who runs the process. A `.dockerignore` listing `.git` and `*.db` keeps a developer's local database and history out of the context; the generated `docs` package stays in, because `main.go` imports it.

The build stage moves from `golang:1.12.1` to `golang:1.18`, the version `go.mod` declares and `go.yml` tests with, and keeps the Debian-based image so the cgo link and the loader symlink in the runtime stage continue to match. The agent leaves the bind address alone but names it in the pull request: a server listening on `127.0.0.1` inside a container is unreachable through `-p` port mapping, which is a separate change the team may want next.

Rush's sandbox has no Docker, so the agent installs it with `sudo apt-get install -y docker.io`, starts `dockerd`, and runs `make container`. It then starts the image, runs `docker exec <container> id -u` to show a non-zero uid, `docker exec <container> wget -qO- http://127.0.0.1:8585/api/tags` to show the API answers from inside the container, and `docker exec <container> ls -l /app/realworld.db` to show the database file is owned by `app`. Those three outputs go into the pull request. `go test ./...` still runs on the host as `go.yml` does, and is unaffected.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Make this Echo app's Docker image run as a non-root user. Read Dockerfile, Makefile, go.mod, main.go, db/db.go, readme.md and .github/workflows/go.yml first. In the runtime stage add a system group and user named app, chown /app to them after the binary is copied because db/db.go creates ./realworld.db in the working directory, and add USER app before CMD; keep the musl loader symlink, the packages and TZ. Add a .dockerignore with .git and *.db, keeping the docs package that main.go imports. Move the build stage to the golang:1.18 image that go.mod and CI use, keeping a Debian-based image so the cgo link still matches the runtime symlink. The sandbox has no Docker: install docker.io, start dockerd, run make container, start the container, and show with docker exec that id -u is not 0, that wget against 127.0.0.1:8585/api/tags answers inside the container, and that /app/realworld.db is owned by app. Note in the pull request that main.go binds 127.0.0.1 and leave that unchanged. Run go test ./... and open a pull request that quotes the outputs.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request changes the `Dockerfile` (the user, the ownership, the `USER` line and the build image), adds `.dockerignore`, and adds a short section to the README on running the image. Its description quotes the three `docker exec` checks, the build log and the `go test ./...` summary, and separately notes the loopback bind address in `main.go`.

A reviewer reads the ownership line and the order of `COPY` and `chown`, since a `chown` before the copy leaves the binary owned by root, and checks that nothing the process writes lands outside `/app`. They also decide whether the SQLite path should move to a mounted volume in a follow-up, which the pull request suggests but does not do.

The image's behaviour for a client is unchanged: same port, same binary, same time zone, one fewer privilege.

## Run it on every new repository with a Dockerfile

A platform team that wants every image unprivileged sends this body once per repository from its own server, each `POST /o/{org}/sessions` naming the repository in `resources` with a `request_id` that is a UUID v5 of the repository's full name, so a repeated call returns the run already started. Each run reads that repository's own `Dockerfile` and finds the paths its process writes before choosing what to own.

An automation on the Project with a `github` trigger on `pull_request.opened` can read each later pull request that edits a `Dockerfile` and comment when a `USER` line is missing or a `COPY` lands after it; automation runs read and comment, so the fix itself is a run started as above.

## Why run it on Rush?

The only proof that an image runs unprivileged is the running container, which needs a Docker daemon, a build and a start. The agent sets all of that up in its own sandbox, runs the checks with `docker exec`, and keeps their output with the pull request, so the reviewer sees the uid and the file owner rather than reading a `USER` line and assuming.

The change is fitted to this image: the cgo binary, the loader symlink, the SQLite file in the working directory. A team with several Go services starts the same request on each at once and gets one pull request per service, each owning the paths that service writes.

## Frequently asked questions

### Why chown /app instead of a data directory?

`db/db.go` opens `./realworld.db`, and the working directory is `/app`, so the file is created there on first start. Changing the path is a code change the pull request leaves for later; owning `/app` makes the current code work as `app`.

### Why keep the /lib64 symlink?

The binary is built with cgo on a Debian-based Go image, so it links against glibc, and alpine provides musl. The symlink lets the glibc-linked binary find a loader. Running as a different user does not change that, so the line stays.

### Does the build image change behaviour?

It moves from Go 1.12.1 to 1.18, the version `go.mod` requires and `go.yml` tests on, so the image builds what CI builds. The pull request quotes the build log for it.

### Why does the check use docker exec rather than curl from the host?

`main.go` listens on `127.0.0.1:8585`, which a port mapping cannot reach from outside the container. The check runs `wget` inside the container, and the pull request names the bind address as the next thing to change.

### Does the sandbox really run Docker?

Not until the agent installs it. The run installs `docker.io`, starts `dockerd`, and then uses `make container` as the Makefile defines it, so the build matches what a developer runs.

## More Echo jobs

- [Add OpenTelemetry Tracing to an Echo API](https://getrush.ai/run/add-opentelemetry/echo/)
- [Add Prometheus Metrics to an Echo API](https://getrush.ai/run/add-prometheus-metrics/echo/)
- [Add Rate Limiting to an Echo API](https://getrush.ai/run/add-rate-limiting/echo/)

## The same job on other stacks

- [Run a Fiber Docker Image as a Non-Root User](https://getrush.ai/run/add-non-root-user/fiber/)
- [Run Your Docker Images as a Non-Root User](https://getrush.ai/run/add-non-root-user/docker/)
- [Run Docker Compose Services as a Non-Root User](https://getrush.ai/run/add-non-root-user/docker-compose/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
