Container Execution and Config

Loading concept...

Docker Container Execution & Config 🐳

The Shipping Container Analogy

Imagine you’re running a magical shipping company. Each container is like a small box that carries everything needed for a specific job—tools, supplies, and instructions. Your job? Learn how to launch these containers, name them, connect them to the outside world, and give them the resources they need!


🚀 Running Containers

What Does “Running a Container” Mean?

Think of it like this: You have a recipe (Docker image), and now you want to cook the dish (run the container).

docker run nginx

This command:

  1. Finds the nginx image (the recipe)
  2. Creates a new container from it
  3. Starts it running!

The Basic Command

docker run [OPTIONS] IMAGE [COMMAND]

Real Example:

docker run hello-world

This runs a tiny container that says “Hello from Docker!” and exits.


🏷️ Container Naming

Why Name Your Containers?

Imagine having 100 boxes in a warehouse with no labels. Chaos! Docker gives containers random names like quirky_einstein, but you can choose your own.

docker run --name my-web-server nginx

Now instead of remembering a7f3b2c1d4e5, you just say my-web-server!

Rules for Names

  • Letters, numbers, underscores, hyphens allowed
  • Must be unique (no two containers with same name)
  • Makes your life much easier
# Stop by name
docker stop my-web-server

# View logs by name
docker logs my-web-server

🎭 Detached vs Interactive Modes

Two Ways to Run a Container

Interactive Mode (-it): Like having a live phone call

docker run -it ubuntu bash
  • -i = Keep input open (listen to you)
  • -t = Give you a terminal (so you can type)
  • You’re INSIDE the container, typing commands!

Detached Mode (-d): Like sending a postcard

docker run -d nginx
  • Container runs in the background
  • You get your terminal back immediately
  • Perfect for servers that run 24/7

When to Use Each?

Mode Use Case
-it Debugging, exploring, testing
-d Web servers, databases, services

🌉 Container Port Mapping

The Bridge Between Worlds

Your container is like a house on a private island. Port mapping builds a bridge so visitors (network traffic) can reach it!

docker run -d -p 8080:80 nginx

This means:

  • 8080 = Door on YOUR computer
  • 80 = Door inside the container
  • Traffic to localhost:8080 goes to container’s port 80
graph TD A[Your Browser] -->|localhost:8080| B[Docker Host] B -->|Port Mapping| C[Container :80] C --> D[Nginx Running]

Multiple Port Mappings

docker run -d \
  -p 8080:80 \
  -p 8443:443 \
  nginx

Now both HTTP (8080) and HTTPS (8443) work!


🌿 Environment Variables

Passing Secrets & Settings

Environment variables are like sticky notes you attach to your container with instructions.

docker run -e DATABASE_URL="mysql://db:3306" \
           -e APP_MODE="production" \
           my-app

Why Use Them?

  • Passwords (don’t hardcode!)
  • Configuration (dev vs production)
  • API keys and secrets

Using an Env File

Too many variables? Put them in a file!

# .env file
DATABASE_URL=mysql://db:3306
APP_MODE=production
SECRET_KEY=super-secret-123
docker run --env-file .env my-app

⚖️ Environment Variable Precedence

Who Wins When Values Conflict?

Imagine three people giving you instructions. Who do you listen to?

Priority Order (Highest to Lowest):

  1. Command line -e (Boss says directly)
  2. --env-file (Written instructions)
  3. Dockerfile ENV (Default instructions)
# Dockerfile says: APP_MODE=development
# .env says: APP_MODE=staging
# Command says: -e APP_MODE=production

# Result: production WINS! 🏆
graph TD A["-e flag"] -->|Highest Priority| D[Final Value] B["--env-file"] -->|Medium Priority| D C["Dockerfile ENV"] -->|Lowest Priority| D

🔄 Container Restart Policies

What Happens When Things Crash?

Containers can stop unexpectedly. Restart policies tell Docker: “What should I do next?”

Policy Meaning
no Don’t restart (default)
always Always restart, even after reboot
unless-stopped Restart unless YOU stopped it
on-failure Only restart if it crashed

Examples

# Always keep running (perfect for servers)
docker run -d --restart=always nginx

# Restart on crashes, max 5 attempts
docker run -d --restart=on-failure:5 my-app

# Restart unless manually stopped
docker run -d --restart=unless-stopped redis

When to Use Each?

  • Production servers: always or unless-stopped
  • Development: no (you want to see crashes)
  • Flaky apps: on-failure:3 (limit retries)

📊 Container Resource Limits

Don’t Let Containers Be Greedy!

Without limits, one container could eat all your computer’s memory and CPU. That’s bad!

Memory Limits

# Max 512 megabytes of RAM
docker run -m 512m nginx

# Max 2 gigabytes
docker run --memory=2g my-database

If the container tries to use more, Docker stops it (or kills it).

CPU Limits

# Use only 1.5 CPU cores max
docker run --cpus=1.5 my-app

# Use 50% of one CPU core
docker run --cpus=0.5 lightweight-app

Combined Example

docker run -d \
  --name production-api \
  --memory=1g \
  --cpus=2 \
  --restart=always \
  my-api:latest

This container:

  • Gets max 1GB RAM
  • Gets max 2 CPU cores
  • Always restarts if it stops

🎯 Putting It All Together

Here’s a real-world example combining everything:

docker run -d \
  --name my-web-app \
  -p 3000:3000 \
  -e NODE_ENV=production \
  -e DATABASE_URL=postgres://db:5432 \
  --restart=unless-stopped \
  --memory=512m \
  --cpus=1 \
  my-node-app:v2

What this does:

  1. ✅ Runs in background (-d)
  2. ✅ Named my-web-app
  3. ✅ Maps port 3000
  4. ✅ Sets environment variables
  5. ✅ Restarts automatically
  6. ✅ Limited to 512MB RAM and 1 CPU

🌟 Key Takeaways

Concept Command Flag Example
Run container docker run docker run nginx
Name it --name --name my-app
Background -d -d
Interactive -it -it ubuntu bash
Port mapping -p -p 8080:80
Env variable -e -e KEY=value
Env file --env-file --env-file .env
Restart --restart --restart=always
Memory limit -m -m 512m
CPU limit --cpus --cpus=1.5

You’re now ready to launch, configure, and control Docker containers like a pro! 🚀

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.