Docker Tutorial

Docker Tutorial

Table of Contents

🐳 Learning About Docker - Your Container Adventure Begins!

πŸŽ₯ Playlist by Learn Linux TV

πŸ€” What is Docker:

Docker Architecture

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

  1. 🏷️ Always use meaningful names and tags for your images
  2. 🧹 Clean up regularly - unused containers and images can pile up!
  3. πŸ“š Read the docs - Docker has excellent documentation
  4. πŸ”„ Practice, practice, practice - The more you use Docker, the more magical it becomes!
  5. πŸ›‘οΈ 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! 🐳✨

Tags :