Dockerfiles that run yarn install must remove the Yarn package cache afterward, because leftover cache files increase image size and may retain build-time artifacts in image layers, expanding the attack surface.
This rule inspects Dockerfile RUN instructions (resource type dockerfile_container) and flags cases where a RUN executes yarn install without a corresponding yarn cache clean.
To remediate, chain the cleanup in the same RUN instruction (for example, RUN yarn install && yarn cache clean) so the cache is removed within the same layer. Running yarn cache clean in a later RUN may not eliminate cache files stored in earlier layers.
Secure example:
RUN yarn install --production && yarn cache clean --force
Compliant Code Examples
FROMnode:18-alpineASbuilderLABELmaintainer="frontend-team@example.com"LABELdescription="Vue.js application builder with proper cache cleanup"# Install system dependencies for node-gypRUN apk add --no-cache \
python3 \
make \
g++ \
gitWORKDIR/build# Set yarn configuration for optimal performanceRUN yarn config set network-timeout 300000# Copy package files for dependency installationCOPY package.json yarn.lock ./# Negative case 1: yarn install with cache clean (reduces image size)RUN yarn install \
&& yarn cache clean# Copy application source codeCOPY . .# Run lintingRUN yarn lint# Build the application for productionRUN yarn build# Production stageFROMnginx:1.25-alpineLABELmaintainer="frontend-team@example.com"LABELdescription="Production Vue.js application with Nginx"LABELversion="2.1.0"# Install curl for health checksRUN apk add --no-cache curl# Copy built application from builderCOPY --from=builder /build/dist /usr/share/nginx/html# Copy custom nginx configurationCOPY nginx.conf /etc/nginx/nginx.confCOPY default.conf /etc/nginx/conf.d/default.conf# Create cache directory with proper permissionsRUN mkdir -p /var/cache/nginx &&\
chown -R nginx:nginx /var/cache/nginx &&\
chmod -R 755 /var/cache/nginx# Negative case 2.1: yarn install with cache clean (reduces image size)RUN yarn install# Remove default nginx configRUN rm -f /etc/nginx/conf.d/default.conf.dpkg-dist# Negative case 2.2: yarn install with cache clean (reduces image size)RUN yarn cache clean# Set proper permissionsRUN chown -R nginx:nginx /usr/share/nginx/html# Expose HTTP portEXPOSE80# Health check for the applicationHEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3\
CMD curl -f http://localhost/health ||exit1# Switch to non-root userUSERnginx# Start nginxCMD["nginx","-g","daemon off;"]