Setting Up Linux with Docker
Docker is the fastest way to get a Linux command line on any operating system. One command and you're in. No VMs, no dual booting, no complexity.
Why Docker?
- Fast: Start a Linux shell in seconds
- Lightweight: Uses way less resources than a VM
- Clean: Delete the container when you're done, nothing left behind
- Professional: Docker is a skill you'll use in your career anyway
Step 1: Install Docker
Windows
- Download Docker Desktop for Windows
- Run the installer
- When prompted, ensure Use WSL 2 is selected
- Restart your computer
- Launch Docker Desktop and complete setup
Windows Home Users
Docker Desktop now works on Windows Home. Make sure WSL2 is enabled (see WSL2 setup guide).
Mac
- Download Docker Desktop for Mac
- Apple Silicon (M1/M2/M3): Download the Apple Silicon version
- Intel Mac: Download the Intel version
- Open the .dmg file and drag Docker to Applications
- Launch Docker from Applications
- Grant permissions when prompted
Linux
Docker runs natively on Linux. Install via your package manager:
Ubuntu/Debian:
sudo apt update
sudo apt install docker.io -y
sudo usermod -aG docker $USER
Log out and back in for group changes to take effect.
Fedora:
sudo dnf install docker -y
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker $USER
Step 2: Verify Docker Works
Open your terminal (PowerShell, Terminal, or your Linux terminal):
docker --version
You should see something like: Docker version 24.x.x
Step 3: Start a Linux Container
Here's the magic - one command to get a Linux shell:
docker run -it ubuntu bash
Let's break this down:
docker run- Start a new container-it- Interactive mode with a terminalubuntu- Use the Ubuntu imagebash- Run bash shell
First time? Docker will download Ubuntu (~30MB). Then you're in a Linux shell:
root@a1b2c3d4e5f6:/#
That's it. You're in Linux.
Step 4: Set Up Your Environment
You're now root (administrator) inside a fresh Ubuntu container:
# Update package lists
apt update
# Install essential tools
apt install -y sudo curl wget git vim nano htop tree
# Check where you are
pwd
# List files
ls -la
Creating a Persistent Container
The basic docker run creates a throwaway container. When you exit, your changes are gone.
For learning, let's create a named container you can reuse:
# Create a named container
docker run -it --name linux-learning ubuntu bash
# Inside the container, set it up
apt update && apt install -y sudo curl wget git vim nano htop
When you're done:
exit
To come back later:
# Start the container
docker start linux-learning
# Attach to it
docker attach linux-learning
Your changes persist in this named container.
Creating a User (Optional)
Running as root all the time isn't realistic. Create a regular user:
# Inside your container
useradd -m -s /bin/bash learner
echo "learner:password" | chpasswd
usermod -aG sudo learner
# Switch to the new user
su - learner
Now you're operating as a normal user, just like you would on a real system.
Quick Reference Commands
| Task | Command |
|---|---|
| Start new Ubuntu shell | docker run -it ubuntu bash |
| Start named container | docker start linux-learning |
| Attach to container | docker attach linux-learning |
| Exit (and stop) | exit or Ctrl+D |
| Detach (keep running) | Ctrl+P, Ctrl+Q |
| List containers | docker ps -a |
| Remove container | docker rm linux-learning |
| Remove all stopped | docker container prune |
Mounting Your Files
Want to access files from your computer inside the container?
# Windows (PowerShell)
docker run -it -v ${PWD}:/workspace ubuntu bash
# Mac/Linux
docker run -it -v $(pwd):/workspace ubuntu bash
Your current directory is now available at /workspace inside the container.
Pro Tips
Use Alpine for a smaller image:
docker run -it alpine sh
Alpine is only 5MB but uses sh instead of bash and apk instead of apt.
Run as a specific user:
docker run -it --user 1000:1000 ubuntu bash
Keep your container running in background:
docker run -d -it --name linux-bg ubuntu bash
docker exec -it linux-bg bash # Connect to it anytime
Troubleshooting
Common Mistakes
- Docker Desktop not running: You need Docker Desktop running in the background before using
dockercommands - Permission denied on Linux: Make sure you ran
sudo usermod -aG docker $USERand logged out/in - Changes lost after exit: Use a named container (
--name) to persist changes, or mount volumes - Using Windows CMD: PowerShell works better than CMD for Docker commands on Windows
"Cannot connect to Docker daemon" Docker Desktop isn't running. Start it from your applications, wait for it to initialize (whale icon stops animating).
"Permission denied" on Linux
sudo usermod -aG docker $USER
# Then log out and back in completely
Container immediately exits
You need -it for interactive shells. Without it, the container runs and exits immediately.
Cleaning Up
Docker images and containers can use disk space. Clean up periodically:
# Remove stopped containers
docker container prune
# Remove unused images
docker image prune
# Nuclear option - remove everything
docker system prune -a
You're Ready!
You now have a Linux environment that starts in seconds. Perfect for learning, experimenting, and following along with this course.
Head back to the course introduction and start learning - your Linux shell is just docker run -it ubuntu bash away!
What does the -it flag do in 'docker run -it ubuntu bash'?