For our Blog Visitor only Get Additional 3 Month Free + 10% OFF on TriAnnual Plan YSBLOG10
Grab the Deal

How to Create Docker on Linux Server in 2026? – (Step by Step Guide)

To create Docker on a Linux server, add Docker’s official repository, install Docker Engine and the Compose plugin, enable and start the docker service, then add your user to the docker group and verify with hello-world.

Finally, secure the daemon, set up persistent storage, and deploy your first container or Compose stack. Wondering how to create Docker on Linux server from scratch?

This guide walks you through installing Docker Engine, enabling it on boot, configuring permissions, and launching your first container and Docker Compose stack. I’ll use simple steps that work on Ubuntu, Debian, RHEL/CentOS, AlmaLinux/Rocky, and Fedora, with security, troubleshooting, and expert tips for production.

What You’ll Need (Prerequisites)

  • 64-bit Linux server (Ubuntu 22.04/24.04, Debian 12, RHEL/CentOS 8/9, AlmaLinux/Rocky 8/9, Fedora 39+).
  • Root or sudo privileges.
  • Network access to download.docker.com and Docker Hub (hub.docker.com).
  • Kernel with cgroups v1/v2 (modern kernels are fine).
  • Optional: A domain and open firewall ports for your app (e.g., 80/443 for web).

Choosing a Linux Distribution (At a Glance)

If you’re just starting, Ubuntu LTS or Debian stable is the easiest path. RHEL-compatible distros (Alma/Rocky) are great for enterprise. Fedora has the newest packages but changes quickly. Here’s a quick comparison:

Distro       | Pros                                    | Considerations
-------------|-----------------------------------------|-------------------------------
Ubuntu LTS   | Most tutorials, easy repo setup          | Slightly faster package churn
Debian       | Very stable, predictable                 | Older defaults; add newer repos if needed
Alma/Rocky   | Enterprise stability, RHEL-compatible    | DNF repo steps differ; SELinux contexts matter
RHEL         | Official support options                 | Subscription/repo access needed
Fedora       | Latest features, fast updates            | Short lifecycle; frequent changes

Ubuntu (20.04/22.04/24.04) and Debian (11/12)

Use Docker’s official repository to get the latest stable Engine, Buildx, and Compose plugin.

# 1) Remove old editions (safe if not installed)
sudo apt-get remove -y docker docker-engine docker.io containerd runc

# 2) Prep dependencies
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg

# 3) Add Docker’s GPG key and repo
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# For Ubuntu:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# For Debian, use 'debian' in the URL and the codename:
# echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
# https://download.docker.com/linux/debian $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
# sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# 4) Install Docker Engine and plugins
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# 5) Enable and start Docker
sudo systemctl enable --now docker

RHEL/CentOS 8/9, AlmaLinux, Rocky Linux

On RHEL-like systems, use the Docker CE repo, then install and start the service.

# 1) Remove any old packages
sudo dnf remove -y docker docker-client docker-client-latest docker-common \
  docker-latest docker-latest-logrotate docker-logrotate docker-engine

# 2) Add the Docker CE repo
sudo dnf -y install dnf-plugins-core
sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

# 3) Install Docker Engine and plugins
sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# 4) Enable and start Docker
sudo systemctl enable --now docker

# If SELinux blocks container networking, ensure container-selinux is installed
# sudo dnf install -y container-selinux

Fedora

sudo dnf -y install dnf-plugins-core
sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo systemctl enable --now docker

Post‑Install Essentials (Do This Next)

  • Add your user to the docker group so you can run Docker without sudo.
  • Verify with hello-world and check versions.
  • Install the Compose plugin (already included above) and test it.
  • Enable Docker at boot and confirm container runtime.
# Allow your user to run Docker without sudo
sudo usermod -aG docker $USER
newgrp docker

# Verify Engine and Compose
docker --version
docker compose version

# Hello World test image
docker run --rm hello-world

# Check service status
systemctl status docker

For production, consider rootless mode for least privilege, or run systemd units with constrained capabilities. Always keep your kernel and Docker packages updated.

Create and Run Your First Container

Option A: Quick NGINX Web Server with docker run

This launches a web server on port 80 with a persistent HTML directory.

# Create a content directory
mkdir -p ~/webroot
echo "Hello from Docker on Linux!" > ~/webroot/index.html

# Run NGINX, map port 80, and mount content
docker run -d --name web \
  -p 80:80 \
  -v ~/webroot:/usr/share/nginx/html:ro \
  --restart unless-stopped \
  nginx:stable

# Test locally
curl -I http://localhost

Create a compose file for NGINX serving a local folder, easy to extend later.

mkdir -p ~/webstack && cd ~/webstack
mkdir -p html
echo "<h1>Compose + NGINX</h1>" > html/index.html

cat > docker-compose.yml <<'YAML'
services:
  web:
    image: nginx:stable
    ports:
      - "80:80"
    volumes:
      - ./html:/usr/share/nginx/html:ro
    restart: unless-stopped
YAML

# Start the stack
docker compose up -d
docker compose ps

Build Your Own Image with a Dockerfile

Package your application with a Dockerfile. Here’s a tiny static-site example using NGINX:

mkdir -p ~/myimage && cd ~/myimage
cat > Dockerfile <<'DOCKER'
FROM nginx:stable
COPY ./html /usr/share/nginx/html
DOCKER

mkdir -p html
echo "<h2>Custom image built on Docker</h2>" > html/index.html

# Build and run
docker build -t mynginx:1.0 .
docker run -d --name custom-web -p 8080:80 mynginx:1.0
curl -I http://localhost:8080

For apps (Node.js, Python, Go), use multi-stage builds to keep images small, pin versions, and avoid shipping build tools in your final runtime image.

Security and Best Practices

  • Use least privilege: prefer non-root containers or rootless Docker when possible.
  • Pin image versions and verify sources (official images, signed artifacts). Consider Docker Content Trust/notation.
  • Keep the host patched: kernel, Docker Engine, containerd, and plugins.
  • Scan images for CVEs and rebuild regularly (e.g., weekly).
  • Restrict capabilities and add resource limits (CPU/memory) for noisy-neighbor control.
  • Network hygiene: only publish required ports; segment containers with user-defined networks.
  • Secrets: pass secrets via environment files or orchestrator secrets; avoid hardcoding in images.
  • Backups: persist data in named volumes or bind mounts and back them up.

Troubleshooting Common Issues

  • Permission denied when running docker: You likely didn’t re-login after adding your user to the docker group. Run newgrp docker or log out and back in.
  • Service won’t start: Check logs with journalctl -u docker and ensure /var/lib/docker has space and correct permissions.
  • cgroups errors: Ensure you’re on a modern kernel. Most distros use cgroup v2 and work out of the box with current Docker.
  • Firewall conflicts: Docker manages iptables rules. If using UFW/firewalld, allow needed ports (e.g., 80/443) and avoid manual rules that drop Docker’s bridge traffic.
  • DNS or pull failures: Verify outbound access to registry-1.docker.io and your nameserver config in /etc/resolv.conf.
# Useful diagnostics
docker version
docker info
docker ps -a
journalctl -u docker --no-pager --since "1 hour ago"

# Network visibility
ip addr show docker0
iptables -t nat -L -n

Upgrade, Uninstall, and Cleanup

  • Upgrade: Use your package manager regularly (apt upgrade/dnf upgrade). Restart containers to pick up new runtimes.
  • Uninstall: Remove packages, then optionally delete images, containers, and volumes.
# Ubuntu/Debian uninstall
sudo apt-get purge -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo rm -rf /var/lib/docker /var/lib/containerd

# RHEL/CentOS/Alma/Rocky uninstall
sudo dnf remove -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo rm -rf /var/lib/docker /var/lib/containerd

# Cleanup unused artifacts (safe prior to uninstall)
docker system prune -af
docker volume prune -f

Production Tips and Server Sizing

  • Start small: 2 vCPU, 4–8 GB RAM, and SSD storage are enough for basic stacks.
  • Separate persistent data on named volumes or dedicated disks for easier backup and recovery.
  • Enable monitoring (docker events, node exporter) and log shipping (JSON logs to a central system).
  • Plan for growth: overprovision CPU/IOPS if you host databases or build pipelines.

If you prefer a pre-hardened environment with expert help, YouStable provides optimized Linux servers and managed Docker setups. Our team can deploy, secure, and monitor your containers so you can focus on your applications—not the plumbing.

FAQ’s

Is Docker free to use on Linux servers?

Yes. Docker Engine is open source and free to use. For enterprises needing centralized management and premium support, Docker offers paid subscriptions, but the core Engine and CLI are free and widely used in production.

Should I use the Compose plugin or the old docker-compose?

Use the modern Docker Compose plugin (invoked as “docker compose”). It’s maintained by Docker, ships via the official repository, and receives updates alongside Docker Engine. The legacy docker-compose (Python) still works but is no longer the recommended path.

How do I run Docker without sudo?

Add your user to the docker group (sudo usermod -aG docker $USER) and re-login or run newgrp docker. For stronger isolation, consider rootless Docker, which runs the daemon and containers without root privileges.

What ports must I open for Docker containers?

Only open the ports you publish with -p in docker run or in Compose (e.g., 80/443 for web, 5432 for PostgreSQL). The Docker daemon itself should not be exposed publicly. If remote management is needed, tunnel over SSH or use a secure proxy.

Is Kubernetes required to use Docker?

No. You can run single-host or small multi-container applications using Docker and Docker Compose alone. Use Kubernetes when you need large-scale orchestration, self-healing, service discovery across clusters, and advanced deployment strategies.

Sanjeet Chauhan

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top