-
Notifications
You must be signed in to change notification settings - Fork 0
/
Non-Root-user
224 lines (122 loc) · 4.56 KB
/
Non-Root-user
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<Check user inside a container>
/> docker exec --interactive --tty <container_name> whoami
https://rmoff.net/2021/01/13/running-as-root-on-docker-images-that-dont-use-root/
/> docker image inspect $(docker image ls -q) -f "{{ .RepoTags }}: {{ .ContainerConfig.User }} {{ .Config.User }}"
https://stackoverflow.com/questions/54675074/how-to-identify-if-any-applications-inside-docker-container-as-running-as-root
** if no user has defined you're the root
php
FROM php:7.2-apache
COPY src/ /var/www/html/
#command to install setcap tool
RUN apt-get update -y
RUN apt-get install -y libcap2-bin
#command to set user non root
RUN setcap 'cap_net_bind_service=+ep' /usr/sbin/apache2
RUN getcap /usr/sbin/apache2
USER www-data
=================================
httpd (aaa in ECS working as a nonRoot user)
FROM httpd:2.4-alpine
#Custom changes
RUN apk update && apk upgrade
RUN apk -q add curl vim libcap
#Change access righs to conf, logs, bin from root to www-data
RUN chown -hR www-data:www-data /usr/local/apache2/
#setcap to bind to privileged ports as non-root
RUN setcap 'cap_net_bind_service=+ep' /usr/local/apache2/bin/httpd
RUN getcap /usr/local/apache2/bin/httpd
HEALTHCHECK --interval=60s --timeout=30s CMD nc -zv localhost 80 || exit 1
#Run as a www-data
USER www-data
==================================
nginx
FROM nginx:stable // FROM nginx
#COPY ./nginx.conf /etc/nginx/nginx.conf
#COPY ./site.conf /etc/nginx/conf.d/default.conf
RUN touch /var/run/nginx.pid && \
chown -R www-data:www-data /var/run/nginx.pid && \
chown -R www-data:www-data /var/cache/nginx
RUN apt-get update -y
RUN apt-get install -y libcap2-bin
#setcap to bind to privileged ports as non-root
RUN setcap 'cap_net_bind_service=+ep' /usr/sbin/nginx
RUN getcap /usr/sbin/nginx
USER www-data
// docker run -d -p 80:8080 mynginx
=============================
mysql, java, , (I run mysql:8.0, MYSQL_USER_PASSWORD, USER -mysql - works in ECS)
FROM oraclelinux:7-slim
ARG MYSQL_SERVER_PACKAGE=mysql-community-server-minimal-5.7.30
ARG MYSQL_SHELL_PACKAGE=mysql-shell-8.0.20
# Install server
RUN yum install -y https://repo.mysql.com/mysql-community-minimal-release-el7.rpm \
https://repo.mysql.com/mysql-community-release-el7.rpm \
&& yum-config-manager --enable mysql57-server-minimal \
&& yum install -y \
$MYSQL_SERVER_PACKAGE \
$MYSQL_SHELL_PACKAGE \
libpwquality \
&& yum clean all \
&& mkdir /docker-entrypoint-initdb.d
VOLUME /var/lib/mysql
#COPY docker-entrypoint.sh /entrypoint.sh
COPY healthcheck.sh /healthcheck.sh
ENTRYPOINT ["/entrypoint.sh"]
HEALTHCHECK CMD /healthcheck.sh
EXPOSE 3306 33060
CMD ["mysqld"]
=================================================
< Mysql > It works!
I created dockerfile:
FROM mysql:5.7
COPY ./init.sql /docker-entrypoint-initdb.d
USER mysql
/> docker build -t aaa .
/> docker run -d --name ms -e MYSQL_ROOT_PASSWORD=my-secret-pw aaa
================================================================================
< Ubuntu > It works!
FROM ubuntu
RUN groupadd --non-unique --gid 23456 awesomegroup
RUN useradd --non-unique --system --uid 12345 --gid 23456 chanseok
USER chanseok
docker build -t ubun .
docker run -itd ubun bash <stays in the running mode>
docker run -it --rm ubunt bash
ps aux | grep mysql
===============================Fix the path/location
FROM openjdk:8-jre-alpine
RUN apk update && apk add bash
WORKDIR /usr/src/myapp
COPY my-app-1.0-SNAPSHOT.jar /usr/src/myapp
RUN setcap 'cap_net_bind_service=+ep' /usr/sbin/java
RUN getcap /usr/sbin/java
EXPOSE 8080
#CMD ["java", "-jar", "my-app-1.0-SNAPSHOT.jar"]
==============================================
you need to add below two lines
RUN setcap 'cap_net_bind_service=+ep' /usr/sbin/apache2
RUN getcap /usr/sbin/apache2
replace the httpd binary location with java
FROM openjdk:8-jre-alpine
RUN apk update && apk add bash
WORKDIR /usr/src/myapp
COPY book-service-0.1.0.jar /usr/src/myapp
RUN setcap 'cap_net_bind_service=+ep' /usr/sbin/java
RUN getcap /usr/sbin/java
EXPOSE 8080
#CMD ["java", "-jar", "book-service-0.1.0.jar"]
FROM mysql:5.7
RUN apt-get update & apt-get upgrade -y
ENV MYSQL_USER=user1 \
MYSQL_PASSWORD=pass5 \
MYSQL_DATABASE=db \
MYSQL_ROOT_PASSWORD=XXX
RUN groupadd -r mysql && useradd -r -g mysql mysql
FROM tomcat:8.0-alpine
LABEL maintainer="[email protected]"
ADD sample.war /usr/local/tomcat/webapps/
RUN addgroup tomcatusers && adduser -D tomcat && adduser tomcat tomcatusers
RUN chown -R tomcat:tomcatusers .
USER tomcat
EXPOSE 8080
CMD ["catalina.sh", "run"]