Review interactive and system commands in RUN

Keep image builds noninteractive and start long-running services with the container.

Description

A Dockerfile RUN instruction executes as the image is built. Commands such as ps show only the processes visible in that build step.

An interactive editor such as vim can leave the build waiting. A service started in RUN stops when the step ends.

Potential impact

  • Interactive commands can prevent an unattended build from finishing.
  • An application may fail to connect to a service that was started only during the build.

Remediation

  • Remove commands the build does not need and configure required tools to finish without user input.
  • Use the container’s startup command to launch long-running services.

Examples

The existing Go 1.22 examples compare build commands. Use a supported compatible image and supply the required source and module files for actual builds.

Before

dockerfile
FROM golang:1.22

WORKDIR /app
COPY . .
RUN top
RUN ps -ef
CMD ["go", "run", "main.go"]

After

dockerfile
FROM golang:1.22

WORKDIR /app
COPY . .
RUN go build -o app .
CMD ["./app"]

Explanation:

  • Before: top and ps run during the build. top may be unsuitable for a noninteractive build.
  • After: The application is built and the resulting executable is started. This change alone does not remove the Go tools from the image.

References