Skillversitas

Docker Tmpfs Mounts

A tmpfs mount stores data in host memory, not on disk.

Syntax

# Mount tmpfs
docker run -d --tmpfs /app/temp myapp

# With options
docker run -d --tmpfs /app/temp:rw,size=100m,noexec myapp

Tmpfs Options

# Read-write
docker run -d --tmpfs /app/temp:rw myapp

# Size limit
docker run -d --tmpfs /app/temp:size=100m myapp

# No execute
docker run -d --tmpfs /app/temp:noexec myapp

# No suid
docker run -d --tmpfs /app/temp:nosuid myapp

# Combined
docker run -d --tmpfs /app/temp:rw,size=100m,noexec,nosuid myapp

Use Case: Temp Files

# Store temporary files in memory
docker run -d \
  --name app \
  --tmpfs /app/temp:rw,size=50m \
  --tmpfs /app/cache:rw,size=100m \
  myapp

Use Case: Application

version: '3.8'

services:
  api:
    build: ./backend
    tmpfs:
      - /app/temp:rw,size=50m
      - /app/cache:rw,size=100m

  worker:
    build: ./worker
    tmpfs:
      - /tmp:rw,size=200m

Storage Comparison

Type Persistent Performance Use Case
Volume Yes High Permanent data
Bind Mount Yes Depends on host Development
Tmpfs No Very high Temp files, cache

Everyday Use Case

Case: The application requires temp files.

# Run with tmpfs
docker run -d \
  --name app \
  --tmpfs /app/temp:rw,size=100m \
  myapp

# Data in /app/temp is stored in memory
# It is lost when the container restarts

Best Practices

  1. Use for temp files: Not for important data
  2. Set a size limit: Avoid excessive memory usage
  3. Use noexec: For security
  4. Consider alternatives: A volume or bind mount may be better

Troubleshooting

Tmpfs not available

# Make sure you are using Docker 17.06+
docker --version

# Check the mount
docker inspect container_name | grep Tmpfs

Conclusion

Tmpfs mounts are useful for temporary files that require high performance. Use them for cache or temp files, not for important data.