DockerDocker Setup

Docker Installation on Ubuntu(Simple)

Before you start

  • 2 min read
  • 22 to run in terminal
  • 2 file contents

Boxes marked Run in terminal are what you type; plain text explains why.

When installing Docker on Ubuntu, there are multiple options and configurations to consider both during and after installation. Here’s a comprehensive guide:


During Installation#

1. Install from the Official Ubuntu Repository (Basic)#

Run in terminal
sudo apt update
sudo apt install docker.io -y
  • Pros: Simple, stable.
  • Cons: Might not have the latest version.
Run in terminal
sudo apt update
sudo apt install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo tee /etc/apt/keyrings/docker.asc > /dev/null
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
  • Pros: Latest stable version, full feature set.
  • Cons: Slightly longer setup.

3. Install from a DEB Package (Offline Installation)#

Run in terminal
wget https://download.docker.com/linux/ubuntu/dists/$(lsb_release -cs)/pool/stable/amd64/docker-ce_XX.YY.ZZ~3-0~ubuntu_amd64.deb
sudo dpkg -i docker-ce_XX.YY.ZZ~3-0~ubuntu_amd64.deb
  • Pros: Useful for air-gapped environments.
  • Cons: Manual updates required.
Run in terminal
sudo snap install docker
  • Pros: Easy installation and updates.
  • Cons: Can have permission issues with volume mounts.

After Installation#

1. Verify Installation#

Run in terminal
docker --version
docker info

2. Start and Enable Docker Service#

Run in terminal
sudo systemctl start docker
sudo systemctl enable docker

3. Run a Test Container#

Run in terminal
sudo docker run hello-world

4. Allow Non-root Users to Run Docker#

Run in terminal
sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker
  • Pros: Run Docker without sudo.
  • Cons: May have security risks.

5. Enable Docker to Start on Boot#

Run in terminal
sudo systemctl enable docker

6. Configure Docker Daemon Settings#

  • Edit the configuration file:
Run in terminal
sudo nano /etc/docker/daemon.json
  • Example configuration:
File content
{
  "log-driver": "json-file",
  "log-opts": { "max-size": "10m", "max-file": "3" },
  "storage-driver": "overlay2",
  "insecure-registries": ["myregistry.local:5000"]
}
  • Restart Docker:
Run in terminal
sudo systemctl restart docker

7. Check Logs#

Run in terminal
sudo journalctl -u docker --no-pager

8. Install Docker Compose#

Run in terminal
sudo apt install docker-compose-plugin
docker compose version

9. Set Up a Private Docker Registry#

Run in terminal
docker run -d -p 5000:5000 --restart=always --name registry registry:2

10. Remove Docker (if needed)#

Run in terminal
sudo apt remove docker-ce docker-ce-cli containerd.io -y
sudo rm -rf /var/lib/docker /etc/docker

Optional Configurations#

- Use Different Storage Drivers#

Run in terminal
docker info | grep "Storage Driver"
  • Change driver (Example: overlay2):
Run in terminal
sudo systemctl stop docker
sudo rm -rf /var/lib/docker
sudo nano /etc/docker/daemon.json
# Add:
{
  "storage-driver": "overlay2"
}
Run in terminal
sudo systemctl restart docker

- Enable IPv6#

Run in terminal
sudo nano /etc/docker/daemon.json
File content
{
  "ipv6": true,
  "fixed-cidr-v6": "2001:db8:1::/64"
}
Run in terminal
sudo systemctl restart docker

- Configure Remote API#

Run in terminal
sudo nano /lib/systemd/system/docker.service
# Modify ExecStart:
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
Run in terminal
sudo systemctl daemon-reload
sudo systemctl restart docker

These steps cover all major aspects of Docker installation and configuration on Ubuntu.🚀