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

  1. Download Docker Desktop for Windows
  2. Run the installer
  3. When prompted, ensure Use WSL 2 is selected
  4. Restart your computer
  5. 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

  1. Download Docker Desktop for Mac
    • Apple Silicon (M1/M2/M3): Download the Apple Silicon version
    • Intel Mac: Download the Intel version
  2. Open the .dmg file and drag Docker to Applications
  3. Launch Docker from Applications
  4. Grant permissions when prompted

Linux

Docker runs natively on Linux. Install via your package manager:

Ubuntu/Debian:

hljs bash
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:

hljs bash
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):

hljs bash
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:

hljs bash
docker run -it ubuntu bash

Let's break this down:

  • docker run - Start a new container
  • -it - Interactive mode with a terminal
  • ubuntu - Use the Ubuntu image
  • bash - 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:

hljs bash
# 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:

hljs bash
# 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:

hljs bash
exit

To come back later:

hljs bash
# 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:

hljs bash
# 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

TaskCommand
Start new Ubuntu shelldocker run -it ubuntu bash
Start named containerdocker start linux-learning
Attach to containerdocker attach linux-learning
Exit (and stop)exit or Ctrl+D
Detach (keep running)Ctrl+P, Ctrl+Q
List containersdocker ps -a
Remove containerdocker rm linux-learning
Remove all stoppeddocker container prune

Mounting Your Files

Want to access files from your computer inside the container?

hljs bash
# 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:

hljs bash
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:

hljs bash
docker run -it --user 1000:1000 ubuntu bash

Keep your container running in background:

hljs bash
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 docker commands
  • Permission denied on Linux: Make sure you ran sudo usermod -aG docker $USER and 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

hljs bash
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:

hljs bash
# 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!

Knowledge Check

What does the -it flag do in 'docker run -it ubuntu bash'?