My goal is to build an image with FROM scratch and to know which user is executing the file, maybe with whoami, and to check the file permission, maybe with ls -l.
The problem are, FROM scratch don't have whoami or ls -l. Installing the busybox throws error as well.
The surprising thing is, that I still can do USER after FROM scratch. I just can't choose any user.
What I've did:
- Create non-root user and do
COPY --from=0 /etc/passwd /etc/passwdit to the next layer and doUSER nonroot - Download busybox binary and do
ENTRYPOINT ["/busybox"]
It throws error:
docker: Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/busybox": permission denied: unknown
Dockerfile
# syntax=docker/dockerfile:1
## Build
# Alpine is chosen for its small footprint
# compared to Ubuntu
FROM golang:1.19-alpine
WORKDIR /app
# Download necessary Go modules
COPY go.mod ./
# COPY go.sum ./
# RUN go mod download
COPY *.go ./
RUN go build -o /main
RUN adduser \
-h "/dev/null" \
-g "" \
-s "/sbin/nologin" \
-D \
-H \
-u 10001 \
playerone
## Deploy
FROM scratch
WORKDIR /
COPY --from=0 /main /main
COPY --from=0 /etc/passwd /etc/passwd
USER playerone
ENTRYPOINT [ "/main" ]
main.go
package main
import "log"
func main() {
log.Println("Hello World!")
}
go.mod
module github.com/kidfrom/learn-golang/docker-scratch
go 1.19