trivy は、コンテナイメージの脆弱性診断を行うツールです。trivy を使用することで、コンテナイメージに含まれる脆弱性を検出し、セキュリティを向上させることができます。
$ mkdir test
$ vim test/Dockerfile
FROM public.ecr.aws/nginx/nginx:1.25-alpine-slim
EXPOSE 80/tcp
$ docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v $(pwd)/test:/workdir aquasec/trivy config --ignorefile .trivy/.trivyignore --severity HIGH,CRITICAL .
2024-03-20T13:16:35.209Z INFO Misconfiguration scanning is enabled
2024-03-20T13:16:35.210Z INFO Need to update the built-in policies
2024-03-20T13:16:35.211Z INFO Downloading the built-in policies...
46.13 KiB / 46.13 KiB [-------------------------------------------------------------------------------------------------------------------------------------------------] 100.00% ? p/s 100ms
2024-03-20T13:16:36.947Z INFO Detected config files: 1
Dockerfile (dockerfile)
Tests: 20 (SUCCESSES: 19, FAILURES: 1, EXCEPTIONS: 0)
Failures: 1 (HIGH: 1, CRITICAL: 0)
HIGH: Specify at least 1 USER command in Dockerfile with non-root user as argument
═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Running containers with 'root' user can lead to a container escape situation. It is a best practice to run containers as non-root users, which can be done by adding a 'USER' statement to the Dockerfile.
See https://avd.aquasec.com/misconfig/ds002
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
$ vim test/Dockerfile
FROM public.ecr.aws/nginx/nginx:1.25-alpine-slim
RUN apk add --no-cache shadow && \
useradd -u 9000 test
USER test
# 再確認
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v $(pwd)/test:/workdir aquasec/trivy config --ignorefile .trivy/.trivyignore --severity HIGH,CRITICAL .
2024-03-20T13:28:55.680Z INFO Misconfiguration scanning is enabled
2024-03-20T13:28:55.681Z INFO Need to update the built-in policies
2024-03-20T13:28:55.682Z INFO Downloading the built-in policies...
46.13 KiB / 46.13 KiB [-------------------------------------------------------------------------------------------------------------------------------------------------] 100.00% ? p/s 100ms
2024-03-20T13:28:57.980Z INFO Detected config files: 1
$ cd test && docker build -f Dockerfile -t test-nginx-01:latest .
ubuntu@test-vm-01:~$ docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v $(pwd)/test:/workdir aquasec/trivy image test-nginx-01:latest
2024-03-20T13:39:44.909Z INFO Need to update DB
2024-03-20T13:39:44.909Z INFO DB Repository: ghcr.io/aquasecurity/trivy-db:2
2024-03-20T13:39:44.909Z INFO Downloading DB...
1.21 MiB / 44.46 MiB [->_____________________________________________________________] 2.71% ? p/s ?2.38 MiB / 44.46 MiB [--->___________________________________________________________] 5.36% ? p/s ?3.81 MiB / 44.46 MiB [----->_________________________________________________________] 8.58% ? p/s ?4.92 MiB / 44.46 MiB [----->____________________________________________] 11.06% 6.17 MiB p/s ETA 6s6.23 MiB / 44.46 MiB [------->__________________________________________] 14.00% 6.17 MiB p/s ETA 6s7.35 MiB / 44.46 MiB [-------->_________________________________________] 16.54% 6.17 MiB p/s ETA 6s9.36 MiB / 44.46 MiB [---------->_______________________________________] 21.05% 6.25 MiB p/s ETA 5s11.68 MiB / 44.46 MiB [------------>____________________________________] 26.28% 6.25 MiB p/s ETA 5s14.02 MiB / 44.46 MiB [--------------->_________________________________] 31.52% 6.25 MiB p/s ETA 4s16.96 MiB / 44.46 MiB [------------------>______________________________] 38.14% 6.67 MiB p/s ETA 4s20.00 MiB / 44.46 MiB [---------------------->__________________________] 44.98% 6.67 MiB p/s ETA 3s22.54 MiB / 44.46 MiB [------------------------>________________________] 50.69% 6.67 MiB p/s ETA 3s25.50 MiB / 44.46 MiB [---------------------------->____________________] 57.36% 7.16 MiB p/s ETA 2s28.53 MiB / 44.46 MiB [------------------------------->_________________] 64.18% 7.16 MiB p/s ETA 2s30.86 MiB / 44.46 MiB [---------------------------------->______________] 69.41% 7.16 MiB p/s ETA 1s33.75 MiB / 44.46 MiB [------------------------------------->___________] 75.91% 7.58 MiB p/s ETA 1s36.58 MiB / 44.46 MiB [---------------------------------------->________] 82.27% 7.58 MiB p/s ETA 1s39.46 MiB / 44.46 MiB [------------------------------------------->_____] 88.77% 7.58 MiB p/s ETA 0s42.50 MiB / 44.46 MiB [---------------------------------------------->__] 95.60% 8.03 MiB p/s ETA 0s44.46 MiB / 44.46 MiB [----------------------------------------------->] 100.00% 8.03 MiB p/s ETA 0s44.46 MiB / 44.46 MiB [----------------------------------------------->] 100.00% 8.03 MiB p/s ETA 0s44.46 MiB / 44.46 MiB [----------------------------------------------->] 100.00% 7.72 MiB p/s ETA 0s44.46 MiB / 44.46 MiB [----------------------------------------------->] 100.00% 7.72 MiB p/s ETA 0s44.46 MiB / 44.46 MiB [----------------------------------------------->] 100.00% 7.72 MiB p/s ETA 0s44.46 MiB / 44.46 MiB [----------------------------------------------->] 100.00% 7.22 MiB p/s ETA 0s44.46 MiB / 44.46 MiB [--------------------------------------------------] 100.00% 9.01 MiB p/s 5.1s2024-03-20T13:39:51.416Z INFO Vulnerability scanning is enabled
2024-03-20T13:39:51.416Z INFO Secret scanning is enabled
2024-03-20T13:39:51.416Z INFO If your scanning is slow, please try '--scanners vuln' to disable secret scanning
2024-03-20T13:39:51.416Z INFO Please see also https://aquasecurity.github.io/trivy/v0.50/docs/scanner/secret/#recommendation for faster secret detection
2024-03-20T13:39:51.923Z INFO Detected OS: alpine
2024-03-20T13:39:51.924Z INFO Detecting Alpine vulnerabilities...
2024-03-20T13:39:51.925Z INFO Number of language-specific files: 0
test-nginx-01:latest (alpine 3.18.6)
====================================
Total: 0 (UNKNOWN: 0, LOW: 0, MEDIUM: 0, HIGH: 0, CRITICAL: 0)
ubuntu@test-vm-01:~$ cat docker-compose.yml
version: "3"
services:
trivy-scan:
image: bitnami/trivy:latest
platform: linux/arm64
volumes:
- ./test:/workdir
working_dir: "/workdir"
entrypoint: sh -c 'trivy config --ignorefile .trivy/.trivyignore --severity HIGH,CRITICAL .'