For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/dockerfile-use-recommended-flags-with-useradd.md.
A documentation index is available at /llms.txt.
Creating users in Docker images without disabling login record creation causes utmp, wtmp, and lastlog entries to be written into image layers. This increases image size and can embed unnecessary host or user metadata in the image.
This rule scans dockerfile_containerRUN commands for useradd invocations and requires the command to include either the short -l flag (which may be combined with other short flags) or the long --no-log-init option to prevent login file initialization.
Resources that invoke useradd without -l or --no-log-init are flagged. Use one of the safe invocations below when creating users in Dockerfiles:
# using short flag (can be combined with other short flags)RUN useradd -l -r -s /sbin/nologin myuser
# using long optionRUN useradd --no-log-init -r -s /sbin/nologin myuser
Compliant Code Examples
FROMubuntu:22.04LABELmaintainer="platform@example.com"LABELdescription="Redis container with proper useradd flags"LABELversion="7.0.0"# Install Redis and dependenciesRUN apt-get update && apt-get install -y --no-install-recommends \
redis-server \
ca-certificates \
curl \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*# Create Redis directoriesRUN mkdir -p /var/lib/redis /var/log/redis /etc/redis &&\
chown -R redis:redis /var/lib/redis /var/log/redisWORKDIR/var/lib/redis# Negative case 1: useradd with -l flag (prevents large UID in lastlog, reduces disk usage)RUN useradd -l -u 123456 foobar --no-log-init# Negative case 2: useradd with -ul flag (prevents large UID in lastlog, reduces disk usage)RUN useradd -ul 123456 foobar# Negative case 2: useradd with --no-log-init flag (prevents large UID in lastlog, reduces disk usage)RUN useradd -u 123456 foobar --no-log-init# Copy Redis configurationCOPY redis.conf /etc/redis/redis.conf# Set proper permissions on configRUN chown redis:redis /etc/redis/redis.conf &&\
chmod 644 /etc/redis/redis.conf# Set environment variablesENVREDIS_PORT=6379\
REDIS_MAXMEMORY=256mb \
REDIS_MAXMEMORY_POLICY=allkeys-lru# Expose Redis portEXPOSE6379# Create volume for Redis dataVOLUME["/var/lib/redis"]# Health checkHEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3\
CMD redis-cli ping ||exit1# Switch to Redis userUSERredis# Start Redis serverCMD["redis-server","/etc/redis/redis.conf"]
Non-Compliant Code Examples
FROMubuntu:22.04LABELmaintainer="platform@example.com"LABELdescription="Database container without proper useradd flags"# Install PostgreSQL and dependenciesRUN apt-get update && apt-get install -y --no-install-recommends \
postgresql-14 \
postgresql-contrib-14 \
postgresql-client-14 \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*# Create data directoryRUN mkdir -p /var/lib/postgresql/data &&\
chown -R postgres:postgres /var/lib/postgresqlWORKDIR/var/lib/postgresql# Positive case: useradd without -l flag and --no-log-init flag (doesn't prevent large UID in lastlog)RUN useradd -u 123456 foobar# Configure PostgreSQLRUN mkdir -p /var/run/postgresql &&\
chown -R postgres:postgres /var/run/postgresql# Set environment variablesENVPOSTGRES_DB=myapp \
POSTGRES_USER=appuser \
PGDATA=/var/lib/postgresql/data# Expose PostgreSQL portEXPOSE5432# Volume for data persistenceVOLUME["/var/lib/postgresql/data"]USERpostgresCMD["postgres"]
1
2
rulesets:- Dockerfile # Rules to enforce .
Request a personalized demo
Get Started with Datadog
Ask AI
AI-generated responses may be inaccurate. Verify important info.