
Docker Tutorial
Table of Contents
π³ Learning About Docker - Your Container Adventure Begins!
π₯ Playlist by Learn Linux TV
π€ What is Docker:
Docker is like having magical boxes π¦ that can contain entire applications with all their dependencies, making them portable across any system! Think of it as shipping containers for software - write once, run anywhere! π
π Installing Docker: GNU/Linux Adventure
π₯ Step 1: Install Docker
$ sudo apt update && sudo apt upgrade
$ sudo apt install docker.io
π Step 2: System Health Check
$ systemctl status docker
Expected Output: β
β docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-09-01 07:13:06 IST; 2min 26s ago
TriggeredBy: β docker.socket
Docs: https://docs.docker.com
Main PID: 2468 (dockerd)
β οΈ If it says “disabled” in the Loaded section:
$ systemctl enable docker
This magical spell β¨ will enable Docker to run on startup automatically!
π§ͺ Step 3: Docker Smoke Test
$ sudo docker run hello-world
This command summons a friendly “Hello World” container to verify everything is working! π
π Step 4: Fixing Permission Issues
First, try this command:
$ docker images
π± Getting “Permission Denied” error? Don’t panic! Here’s the fix:
$ sudo usermod -aG docker yourusername && reboot
π‘ Pro tip: We’re rebooting because sometimes logging out and back in isn’t enough to refresh group memberships!
After reboot, test again:
$ docker images
π Success! You should now see the Docker images without permission errors!
π§ Key Concept: Images vs Containers
π Image = The blueprint/recipe for your container
π Container = The actual running instance of your image
Think of it like this: Image is like a house blueprint π, Container is the actual house π‘ built from that blueprint!
β‘ Essential Docker Commands Toolkit
π Searching for Images (Docker’s Treasure Hunt)
$ docker search image-name
Example adventure:
$ docker search nginx
$ docker search python
$ docker search ubuntu
π¦ Downloading Images (Collecting Treasures)
$ docker pull image-name
Popular downloads for your collection:
$ docker pull ubuntu:latest
$ docker pull nginx:alpine
$ docker pull python:3.9
πββοΈ Running Containers - Method 1 (Quick & Simple)
$ docker run image-name
β οΈ Important: If the image has nothing to do, Docker will stop it immediately! It’s like a movie that ends as soon as it starts! π¬
π Spying on Your Containers
See what’s currently running: π΅οΈββοΈ
$ docker ps
See EVERYTHING (including stopped containers): π
$ docker ps -a
π Running Containers - Method 2 (Interactive Adventure Mode)
$ docker run -it ubuntu /bin/bash
Flag Decoder Ring: π
-t
: Allocate a pseudo-tty (gives you a proper terminal)-i
: Keep STDIN open (lets you type commands)-d
: Detached mode (runs in background like a ninja π₯·)-p
: Port mapping (host:container) - like opening windows between worlds πͺ
This command teleports you INSIDE the Ubuntu container! π You can install software, create files, and explore - but remember, it’s like a magic sandbox that resets when you leave! ποΈ
π Reconnecting to Detached Containers
$ docker attach container-name-or-id
This is like using a teleportation spell to jump back into your running container! β¨
πΎ Keeping Your Container Alive (The Secret Escape Sequence)
When you’re inside a container and want to exit WITHOUT killing it:
CTRL + p + q
This is the magical escape sequence! π Your container keeps running in the background while you return to your host system.
π¨ Creating Your Own Images (Becoming a Docker Artist)
After you’ve customized a container (installed software, made changes), you can save it as a new image:
$ docker commit container-id new-container-name:tag-name
Example Magic Spell: β¨
$ docker commit abc123def456 my-custom-ubuntu:v1.0
This transforms your modified container into a reusable image - like taking a snapshot of your perfect setup! πΈ
π― Pro Tips for Docker Adventurers
- π·οΈ Always use meaningful names and tags for your images
- π§Ή Clean up regularly - unused containers and images can pile up!
- π Read the docs - Docker has excellent documentation
- π Practice, practice, practice - The more you use Docker, the more magical it becomes!
- π‘οΈ Security first - Never run containers as root in production unless absolutely necessary
π Congratulations, Docker Explorer!
You’ve just embarked on an amazing journey into the world of containerization! π Docker will revolutionize how you think about software deployment and development. Keep experimenting, keep learning, and most importantly - have fun with your new containerized superpowers! π¦ΈββοΈ
Docker Compose:
Adding Repository to OS.
i will do it for PopOS, ubuntu based.
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
Install the packages:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Happy Dockering! π³β¨