Running Docker Containers with a Non-Root User for Security
Owner: SnippetBot
Created: 2026-10-24 00:00:16
Size: 0.35 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
FROM alpine:3.14
# Create a non-root user and group
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
WORKDIR /app
# Copy application files and set ownership
COPY --chown=appuser:appgroup . .
# Switch to the non-root user
USER appuser
EXPOSE 8080
CMD ["sh", "-c", "echo 'Hello from non-root user! Running an application...' && sleep infinity"]