Skip to content

Commit 8d99afa

Browse files
committed
hardened the dockerfile build for security.
1 parent 1e0a04d commit 8d99afa

3 files changed

Lines changed: 90 additions & 9 deletions

File tree

.dockerignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*
2+
3+
!src/
4+
!docs/
5+
!website/
6+
!examples/
7+
!mkdocs.yml
8+
!pyproject.toml
9+
!uv.lock
10+
!README.md
11+
!CHANGELOG.md
12+
!LICENSE
13+
!nginx.conf

Dockerfile

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
FROM python:3.12-slim AS build
22

3-
RUN apt-get update && apt-get install -y --no-install-recommends git && rm -rf /var/lib/apt/lists/*
4-
RUN pip install uv
3+
RUN apt-get update \
4+
&& apt-get install -y --no-install-recommends git \
5+
&& rm -rf /var/lib/apt/lists/*
6+
7+
RUN pip install --no-cache-dir uv
58

69
WORKDIR /app
710
COPY . .
811

9-
RUN uv sync --frozen
10-
RUN git init && git add -A && git -c user.name=build -c user.email=build@localhost commit -m "build"
11-
RUN uv run --group dev python -m mkdocs build --strict --site-dir _site/docs
12-
13-
RUN mkdir -p _site && cp -r website/* _site/
12+
RUN uv sync --frozen \
13+
&& git init \
14+
&& git add -A \
15+
&& git -c user.name=build -c user.email=build@localhost commit --allow-empty -m "build" \
16+
&& uv run --group dev python -m mkdocs build --strict --site-dir _site/docs \
17+
&& cp -r website/* _site/
1418

1519
FROM nginx:alpine
20+
21+
RUN rm -rf /usr/share/nginx/html/* /etc/nginx/conf.d/default.conf \
22+
&& chown -R nginx:nginx /var/cache/nginx /var/log/nginx
23+
24+
COPY nginx.conf /etc/nginx/nginx.conf
1625
COPY --from=build /app/_site /usr/share/nginx/html
17-
RUN sed -i 's/listen\s*80;/listen 3000;/g' /etc/nginx/conf.d/default.conf
18-
EXPOSE 3000
26+
27+
USER nginx
28+
29+
EXPOSE 3000

nginx.conf

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
worker_processes 1;
2+
pid /tmp/nginx.pid;
3+
error_log /var/log/nginx/error.log warn;
4+
5+
events {
6+
worker_connections 1024;
7+
}
8+
9+
http {
10+
include /etc/nginx/mime.types;
11+
default_type application/octet-stream;
12+
13+
client_body_temp_path /tmp/client_body;
14+
proxy_temp_path /tmp/proxy;
15+
fastcgi_temp_path /tmp/fastcgi;
16+
uwsgi_temp_path /tmp/uwsgi;
17+
scgi_temp_path /tmp/scgi;
18+
19+
server_tokens off;
20+
21+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
22+
'$status $body_bytes_sent "$http_referer" '
23+
'"$http_user_agent"';
24+
access_log /var/log/nginx/access.log main;
25+
26+
sendfile on;
27+
tcp_nopush on;
28+
keepalive_timeout 65;
29+
30+
gzip on;
31+
gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
32+
33+
server {
34+
listen 3000;
35+
server_name _;
36+
root /usr/share/nginx/html;
37+
index index.html;
38+
39+
add_header X-Content-Type-Options "nosniff" always;
40+
add_header X-Frame-Options "DENY" always;
41+
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
42+
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
43+
44+
autoindex off;
45+
46+
location ~ /\. {
47+
deny all;
48+
return 404;
49+
}
50+
51+
location / {
52+
try_files $uri $uri/ $uri/index.html =404;
53+
}
54+
55+
error_page 404 /docs/404.html;
56+
}
57+
}

0 commit comments

Comments
 (0)