Set user for security
Docker enters container as root by default which may lead to security issue.
check adduser
options
bash
/app # adduser
BusyBox v1.36.1 (2023-07-27 17:12:24 UTC) multi-call binary.
Usage: adduser [OPTIONS] USER [GROUP]
Create new user, or add USER to GROUP
-h DIR Home directory
-g GECOS GECOS field
-s SHELL Login shell
-G GRP Group
-S Create a system user
-D Don't assign a password
-H Don't create home directory
-u UID User id
-k SKEL Skeleton directory (/etc/skel)
/app #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Add command for creating user
dockerfile
FROM node:alpine
RUN addgroup app && adduser -S -G app app
WORKDIR /app
COPY . .
RUN npm install
ENV API_URL=http://api.myapp.com
EXPOSE 8080
1
2
3
4
5
6
7
2
3
4
5
6
7
Set default user
dockerfile
FROM node:alpine
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /app
COPY . .
RUN npm install
ENV API_URL=http://api.myapp.com
EXPOSE 8080
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Notice: If you create any files before specifying user, it may lead to failure launching the application since these files are created by root.