Docker Basics

Essential Docker system commands for beginners

Understanding Container Data Storage

Containers Are Temporary and Don't Save Data by Default

What Does "Ephemeral" Mean?

Ephemeral means something that lasts for a very short time. Think of it like a sandcastle on the beach - it's there when you build it, but when the tide comes in, it disappears.

In Docker, containers are ephemeral - they're designed to be temporary. When you delete a container, everything inside it is gone forever.

Two Types of Data in Docker:

Temporary Data

This is data that doesn't need to be saved long-term.

  • Stored directly inside the container
  • Easy to use - just save files like normal
  • Disappears when the container is deleted
  • Good for: Cache files, temporary logs, session data

Permanent Data

This is important data that needs to be saved.

  • Stored outside the container using Volumes
  • Volumes are like special folders that survive container deletion
  • Data stays safe even if containers are destroyed
  • Good for: Databases, user files, application data

Real-World Example:

Think of a container like a hotel room:

  • Temporary data = The towels and toiletries in the room (they get replaced when you leave)
  • Permanent data = Your luggage that you store at the front desk (it stays safe even after you check out)

Data Management Examples

Temporary Data (Inside Container)

# This data disappears when container stops
docker run -it ubuntu bash

# Inside the container:
echo "This will be lost" > /tmp/temp-file.txt
cat /tmp/temp-file.txt
# Output: This will be lost

# But if you stop and restart the container,
# the file is gone forever!

Permanent Data (Using Volumes)

# Create a volume for permanent storage
docker volume create my-data

# Use the volume when running a container
docker run -v my-data:/app/data ubuntu bash

# Inside the container:
echo "This will be saved" > /app/data/important-file.txt

# Even if you delete the container,
# the data in 'my-data' volume remains safe

Best Practices for Docker Data

Do This:

  • Use volumes for database files
  • Store configuration files in volumes
  • Keep user uploads in persistent storage
  • Use environment variables for settings

Avoid This:

  • Storing important data directly in containers
  • Keeping sensitive information in container images
  • Assuming data will survive container restarts
  • Using containers as permanent storage solutions

Remember:

Containers are like disposable cameras - great for temporary use, but not for keeping your precious photos safe long-term. Use volumes for anything important!