🐳 Working Inside Docker Containers
The Magic Treehouse Analogy 🏠
Imagine your Docker container is a magic treehouse in your backyard. You built it, it’s running, but now you want to:
- Climb inside and play
- Bring toys up to the treehouse
- Take things back down
- See what changed since you built it
Let’s learn how to do all of this with Docker!
🎯 What You’ll Learn
- Using
docker exec- Sending commands into your treehouse - Attaching to containers - Climbing inside
- Detaching from containers - Climbing back out (without breaking anything!)
- Container shell access - Getting a full playground inside
- Copying files to containers - Bringing toys up
- Copying files from containers - Taking things back down
- Container diff - Seeing what changed
1. Using docker exec 🎮
What is it?
docker exec is like having a magic wand that lets you cast spells (run commands) inside your treehouse without climbing up!
Simple Example
# Say hello from inside the container
docker exec my_container echo "Hello!"
This runs echo "Hello!" inside my_container.
Real-Life Use
# Check what files are in the container
docker exec my_container ls /app
# Install something inside the container
docker exec my_container apt-get update
🧠 Key Flags
| Flag | What it does |
|---|---|
-i |
Interactive (keep input open) |
-t |
Give me a terminal |
-u |
Run as a specific user |
# Interactive terminal session
docker exec -it my_container bash
2. Attaching to Containers 🧗
What is it?
Attaching is like climbing into your treehouse and seeing exactly what’s happening inside. You connect to the main process running in the container.
Simple Example
docker attach my_container
Now you see what the container is doing!
⚠️ Important!
When you attach, you connect to the main process. If you press Ctrl+C, you might stop the whole container!
graph TD A[Your Terminal] -->|docker attach| B[Container's Main Process] B --> C[You see all output] B --> D[Your input goes to main process]
When to use it?
- Watching logs in real-time
- Interacting with the main app
- Debugging what’s happening
3. Detaching from Containers 🪂
The Problem
You attached to your container. Now how do you leave without stopping it?
The Magic Escape Sequence! ✨
Ctrl + P, then Ctrl + Q
Press these keys one after another, and you’ll safely float back down from your treehouse while it keeps running!
Simple Example
# Attach to container
docker attach my_container
# ... do your work ...
# Detach safely (press these keys):
# Ctrl+P, Ctrl+Q
You’ll see: read escape sequence
Your container keeps running! 🎉
🚫 What NOT to do
- Don’t press
Ctrl+C(this stops the container) - Don’t close the terminal (container might stop)
4. Container Shell Access 🐚
What is it?
Getting a shell means you get a full command line inside the container. It’s like having your own room in the treehouse where you can do anything!
Simple Example
# Get a bash shell
docker exec -it my_container bash
# Or if bash isn't available, try sh
docker exec -it my_container sh
Now you’re inside! Your prompt changes:
root@abc123:/#
What can you do inside?
# Look around
ls -la
# Check running processes
ps aux
# Read files
cat /etc/hostname
# Exit when done
exit
🎯 Common Shells
| Shell | Command |
|---|---|
| Bash | docker exec -it container bash |
| Sh | docker exec -it container sh |
| Zsh | docker exec -it container zsh |
graph TD A[You] -->|docker exec -it bash| B[Container Shell] B --> C[Run any command] C --> D[ls, cat, ps, etc.] D -->|exit| A
5. Copying Files TO Containers 📦➡️🏠
What is it?
Just like carrying toys up to your treehouse! You can copy files from your computer INTO the container.
The Magic Command
docker cp source_file container:destination
Simple Example
# Copy a file
docker cp myfile.txt my_container:/app/
# Copy a folder
docker cp ./myfolder my_container:/data/
Real-Life Use
# Copy a config file into container
docker cp nginx.conf webserver:/etc/nginx/
# Copy code into a running container
docker cp ./src my_app:/var/www/html/
✨ Key Points
- The container must be running
- Use full paths for clarity
- Folders copy recursively
6. Copying Files FROM Containers 🏠➡️📦
What is it?
Taking things back from your treehouse! Copy files from the container to your computer.
The Magic Command
docker cp container:source_file destination
Simple Example
# Copy a log file out
docker cp my_container:/var/log/app.log ./logs/
# Copy a whole folder out
docker cp my_container:/app/data ./backup/
Real-Life Use
# Get logs for debugging
docker cp webserver:/var/log/nginx/ ./nginx-logs/
# Backup database files
docker cp db_container:/var/lib/mysql ./db-backup/
graph LR A[Your Computer] -->|docker cp file container:/path| B[Container] B -->|docker cp container:/path file| A
🎯 Remember!
# TO container: your file first
docker cp myfile.txt container:/path/
# FROM container: container path first
docker cp container:/path/file ./local/
7. Container Diff 🔍
What is it?
docker diff shows you what changed inside the container since it started. It’s like a detective finding clues!
The Magic Command
docker diff my_container
What the symbols mean
| Symbol | Meaning | Example |
|---|---|---|
A |
Added | New file was created |
C |
Changed | File or folder was modified |
D |
Deleted | Something was removed |
Simple Example
$ docker diff my_container
A /app/newfile.txt
C /var/log
C /var/log/app.log
D /tmp/oldfile.txt
This tells you:
- 📄
newfile.txtwas added - 📝
app.logwas changed - 🗑️
oldfile.txtwas deleted
Why is this useful?
- Debugging: What did my app create?
- Security: Did something unexpected change?
- Learning: Understanding what containers do
graph TD A[Original Container Image] --> B[Running Container] B --> C{What Changed?} C -->|A| D[Added Files] C -->|C| E[Changed Files] C -->|D| F[Deleted Files]
🎯 Quick Reference
| Task | Command |
|---|---|
| Run command inside | docker exec container command |
| Get shell access | docker exec -it container bash |
| Attach to container | docker attach container |
| Detach safely | Ctrl+P, Ctrl+Q |
| Copy file in | docker cp file container:/path |
| Copy file out | docker cp container:/path file |
| See what changed | docker diff container |
🚀 You Did It!
Now you know how to:
- ✅ Send commands into containers
- ✅ Climb in and out safely
- ✅ Get full shell access
- ✅ Move files in both directions
- ✅ See what changed
You’re ready to work with Docker containers like a pro! 🎉
💡 Pro Tips
- Always use
-ittogether when you want an interactive terminal - Remember the escape sequence:
Ctrl+P, Ctrl+Qto detach safely - Use
docker diffbefore committing changes to see what you’re saving - Copy files while container runs - no need to stop it!