All jobs

Run Your Docker Images as a Non-Root User

Yes. An agent adds a non-root USER to each Dockerfile, moves services off privileged ports, fixes the compose, stack and Kubernetes files to match, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent clones the repository into its own cloud sandbox and reads every Dockerfile and every file that depends on them. In an application like dockersamples/example-voting-app there are four: vote/Dockerfile builds from python:3.11-slim, installs curl for the health check, copies requirements.txt (Flask, Redis, gunicorn), has a dev stage that adds watchdog and a final stage that runs gunicorn app:app -b 0.0.0.0:80 with four workers; result/Dockerfile builds from node:18-slim, installs curl and tini, moves node_modules to /node_modules, sets ENV PORT=80 and runs node server.js under tini; worker/Dockerfile is a two-stage .NET 7 build that publishes into mcr.microsoft.com/dotnet/runtime:7.0; seed-data/Dockerfile runs a shell script with ab. None of the four has a USER line, so every container runs as root. Port 80 is baked in: vote/app.py ends with app.run(host='0.0.0.0', port=80), result/server.js reads process.env.PORT || 4000, docker-compose.yml publishes 8080:80 and 8081:80 with a health check of curl -f http://localhost, docker-stack.yml repeats the mappings, and k8s-specifications/vote-deployment.yaml and result-deployment.yaml declare containerPort: 80 with no securityContext.

Binding a port under 1024 is the thing that makes a non-root switch fail in production, so the agent moves the two web services to 8080 first, in one place each: the gunicorn bind in the final stage and app.run in vote/app.py, and ENV PORT=8080 in result/Dockerfile, which server.js already reads. Then it adds the user. node:18-slim ships a node user, so result/Dockerfile gets USER node after the COPY and a chown on /usr/local/app so nodemon can still watch files in the dev setup. For python:3.11-slim it creates a system user and group with useradd --system --no-create-home before the final stage's COPY --chown, and sets USER last so the pip install in base still runs as root. The .NET runtime image gets the same useradd in the final stage with USER before the ENTRYPOINT; the worker opens no port, so only file ownership of /app matters. The seed-data image only runs ab against the vote service and gets a user the same way.

Everything that named port 80 follows: docker-compose.yml becomes 8080:8080 and 8081:8080 with the health check pointed at http://localhost:8080, the seed service's depends_on health condition keeps working because the check moved with it, docker-compose.images.yml and docker-stack.yml get the same mappings, vote-service.yaml and result-service.yaml keep port: 80 or their nodePort and change targetPort to 8080, the two Deployments get containerPort: 8080 plus a pod securityContext with runAsNonRoot: true and the uid the Dockerfiles create, and the README.md lines that say to open port 8080 and 8081 stay true. The vote dev stage mounts ./vote over /usr/local/app in compose, and the agent checks that the bind mount still works for the new user.

To prove it, the agent installs Docker in the sandbox (sudo apt-get install -y docker.io, then starts dockerd) and builds all four images. It runs each one and reads id -u inside, starts vote and result and curls localhost:8080 from inside the container, and brings the whole stack up with docker-compose up (the v1 binary that package provides) long enough for the redis and db health checks to pass and for one vote to reach the result page. It also runs hadolint over the four Dockerfiles, since worker/Dockerfile already carries a hadolint ignore comment, and reports what it says about the new lines.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Make every container image in this repository run as a non-root user. Read each Dockerfile and every compose, stack and Kubernetes file that references its ports or user first. For each image add a dedicated user and group (or use one the base image ships), fix file ownership so the application still starts, and move any service that listens on a port below 1024 to an unprivileged port, updating the application config, compose port mappings and health checks, stack file, Kubernetes containerPort and Service targetPort, and README to match. Add runAsNonRoot to the Kubernetes pod specs. Install Docker in the sandbox, build every image, show the uid each container runs as and that each web service answers on its new port, bring the stack up with compose, and open a pull request.",
  "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

What comes back is a pull request on its own branch that changes vote/Dockerfile, vote/app.py, result/Dockerfile, worker/Dockerfile, seed-data/Dockerfile, docker-compose.yml, docker-compose.images.yml, docker-stack.yml, the vote and result Deployment and Service manifests under k8s-specifications/, and README.md. The description has a table per image: base image, the user and uid it now runs as, the port before and after, and the id -u and curl output from the sandbox, followed by the compose run log and the hadolint report.

The run's record keeps every command with its output, including the four docker build runs and the compose session, so a reviewer can see the images built and served from inside the sandbox rather than trust a diff.

Before merging, a reviewer checks whether anything outside the repository still points at port 80 inside the containers, such as an ingress or a load balancer target group, decides whether the uid should be fixed for volume ownership on shared hosts, and whether a read-only root filesystem is the next change now that nothing needs to write as root.

Run it on every new Dockerfile in the repository

In the console, an automation in the Project that has the repository, with a github trigger on pull_request.opened and pull_request.synchronize and branch_patterns for your default branch, starts one run per pull request, with the pull request as its context. Its prompt asks the run to comment with the USER line, the ownership fix and the port change the new image needs when the pull request adds or changes a Dockerfile that has no USER instruction, and to stop otherwise; the saved prompt is the whole instruction and the trigger fills in no placeholder.

For a one-time sweep of many repositories, a script sends one POST /o/{org}/sessions per repository with this body, each with a fresh UUID as request_id so a retry returns the run already started, and each run ends in its own pull request scoped to that repository's images.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

The non-root change is small per file and wide across the repository: a user line in each Dockerfile, a port in the application, and then every compose, stack and Kubernetes file that quietly assumed port 80. The failures are at run time, not build time, which is why the agent installs Docker in its sandbox, builds the images, starts them and brings the stack up before it opens the pull request.

The sandbox is its own computer, so building four images and running a compose stack costs nothing on your laptop and leaves a full record of every command and its output with the run. Several repositories can be swept at once in separate sandboxes, each with its own pull request.

Frequently asked questions

Why change the ports at all?

A process that is not root cannot bind a port below 1024 on a stock Linux kernel unless it is given CAP_NET_BIND_SERVICE or the kernel's unprivileged port range is lowered, and Kubernetes nodes do not lower it for you. Moving vote and result to 8080 inside the container keeps the images portable; the host and Service ports people type stay as they were.

Will the compose dev setup with bind mounts still work?

The vote service mounts ./vote over the working directory and runs the dev stage with Flask's reloader, and result runs nodemon. Both now run as the new user, which can read the mounted files; the agent checks that in the sandbox and notes that a file created inside the container is owned by the container's uid on the host.

What user does each image get?

result uses the node user that node:18-slim ships. The Python and .NET images get a system user created with useradd in the final stage, and the agent picks one uid for all of them and writes it into the Kubernetes securityContext so runAsNonRoot and the image agree.

Does this harden the images in other ways?

No. The change is the user, ownership and ports. Dropping capabilities, a read-only root filesystem and pinning base images by digest are separate changes, and the pull request lists them as follow-ups with the files they would touch.

More Docker jobs

The same job on other stacks

Keep reading