EN · $ USD
VDS and Linux Server Management

How to Install Docker on Ubuntu and Run Your First Container

7 min read Updated: Sunucu Atlas technical team

In short

To install Docker on Ubuntu, add Docker's official apt repository with its GPG key and install the docker-ce, docker-ce-cli, containerd.io, docker-buildx-plugin and docker-compose-plugin packages. After testing with sudo docker run hello-world, start your first container with docker run -d -p 8080:80 nginx:alpine, use volumes for persistent data and a custom bridge network for service-to-service communication.

In this guide
  1. What Are Docker, Images and Containers?
  2. 1. Remove Old or Conflicting Packages
  3. 2. Add Docker's Official Repository
  4. 3. Install Docker Engine and Compose
  5. 4. Your First Container: Nginx Web Server
  6. 5. Volumes: Making Data Persistent
  7. 6. Networking Basics
  8. 7. Multi-Container Apps with Docker Compose
  9. 8. Security Recommendations for Production
  10. Which Server Should You Choose for Docker?
  11. Next Steps

The right way to install Docker on Ubuntu is to add Docker's official apt repository and install the docker-ce, docker-ce-cli, containerd.io, docker-buildx-plugin and docker-compose-plugin packages. Installation takes a few minutes; then you test it with sudo docker run hello-world and start your first application container with docker run -d -p.

This guide walks you step by step through installing Docker Engine and Docker Compose from the official repository, running your first container, volume and network basics, and the security settings that are often skipped in production. The commands apply to Ubuntu 22.04 and 24.04.

What Are Docker, Images and Containers?

  • Docker Engine: The background service (dockerd) that builds and runs containers.
  • Image: A read-only package of an application and its dependencies. Examples: nginx:alpine, postgres:16.
  • Container: A running, isolated instance of an image. You can start as many containers from the same image as you like.
  • Volume: A data area that persists even if the container is deleted.
  • Docker Compose: A tool that defines multiple containers in a single YAML file and manages them together.

Unlike a virtual machine, Docker doesn't run a separate kernel; it shares the host's Linux kernel. That makes it lightweight, and because it needs a full kernel, it runs smoothly on a KVM-based VDS.

Container or virtual machine?

FeatureDocker containerVirtual machine (KVM)
KernelShared with the hostEach VM has its own kernel
Startup timeUsually secondsAs long as an OS boot
Resource overheadLow, application processes onlyFull operating system overhead
IsolationProcess levelHardware virtualization level
Typical useApplication packaging, microservices, CISeparate operating system, strong isolation

In practice the two aren't competitors: the most common setup is running Docker inside a KVM-based VDS. The VDS gives you an isolated operating system, and Docker keeps your applications organized and portable inside it.

1. Remove Old or Conflicting Packages

If unofficial packages such as docker.io from the Ubuntu repository are installed, they conflict with the official release. Remove them first (if they aren't installed, the command just prints a warning):

for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do
  sudo apt-get remove -y $pkg
done

2. Add Docker's Official Repository

sudo apt-get update
sudo apt-get install -y 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

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

This step uses the GPG key to verify that packages are signed by Docker. The curl ... | sh convenience scripts you find online are fine for test environments; on a production server, the repository method is more transparent and easier to keep updated.

3. Install Docker Engine and Compose

sudo apt-get install -y docker-ce docker-ce-cli containerd.io \
  docker-buildx-plugin docker-compose-plugin

sudo systemctl enable --now docker
sudo docker run hello-world
docker compose version

If you see the "Hello from Docker!" message, the installation is complete. Compose is no longer a separate program but a Docker CLI plugin; the command is written docker compose (with a space), not docker-compose.

Running without sudo (be careful)

sudo usermod -aG docker $USER
newgrp docker

Important: Every user in the docker group effectively has root privileges, because they can mount the host file system into a container and modify any file. Grant this group only to the server's administrator.

4. Your First Container: Nginx Web Server

docker run -d --name ilk-web -p 8080:80 --restart unless-stopped nginx:alpine
docker ps
curl http://localhost:8080

What the parameters mean:

  • -d: Runs the container in the background.
  • --name ilk-web: Gives the container a memorable name.
  • -p 8080:80: Maps the host's port 8080 to the container's port 80.
  • --restart unless-stopped: Starts the container automatically when the server reboots.

Everyday management commands:

TaskCommand
Running containersdocker ps
All containersdocker ps -a
Follow logsdocker logs -f ilk-web
Open a shell insidedocker exec -it ilk-web sh
Stop / removedocker stop ilk-web / docker rm ilk-web
Resource usagedocker stats

5. Volumes: Making Data Persistent

Data written inside a container is lost when the container is deleted. Always use volumes for databases, uploaded files and configuration. There are two methods:

  • Named volume: Managed by Docker under /var/lib/docker/volumes/. Recommended for databases.
  • Bind mount: Mounts a specific folder on the host into the container. Handy for editing code or config files.
# Named volume
docker volume create pgdata
docker run -d --name db -e POSTGRES_PASSWORD=GucluParola \
  -v pgdata:/var/lib/postgresql/data postgres:16

# Bind mount (read-only)
docker run -d --name site -p 8081:80 \
  -v /srv/site:/usr/share/nginx/html:ro nginx:alpine

6. Networking Basics

On the default bridge network, containers can't find each other by name. On a bridge network you create yourself, Docker provides internal DNS and containers reach each other by name:

docker network create uygulama-agi
docker run -d --name redis --network uygulama-agi redis:7-alpine
docker run --rm --network uygulama-agi redis:7-alpine redis-cli -h redis ping

If the last command returns PONG, the two containers can see each other on the same network. You don't need to publish ports with -p for services like databases; the application container reaches them over the internal network.

7. Multi-Container Apps with Docker Compose

In real projects, instead of starting containers one by one, you write a compose.yaml file:

services:
  web:
    image: nginx:alpine
    ports:
      - "127.0.0.1:8080:80"
    volumes:
      - ./html:/usr/share/nginx/html:ro
    depends_on:
      - db
    restart: unless-stopped

  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - pgdata:/var/lib/postgresql/data
    restart: unless-stopped

volumes:
  pgdata:

Put the password in a .env file in the same folder as DB_PASSWORD=... and don't commit that file to Git. Then run:

docker compose up -d
docker compose ps
docker compose logs -f web
docker compose pull && docker compose up -d   # update

8. Security Recommendations for Production

Docker ports bypass UFW

This is the most common mistake. Docker writes iptables rules directly for the ports it publishes, and those rules are processed before UFW's rules. So even if you ran ufw deny 8080, a port opened with -p 8080:80 is reachable from the internet. The fix: bind every port that shouldn't be public to localhost only (127.0.0.1:8080:80) and route traffic through a reverse proxy in front (Nginx, Caddy or Traefik). For UFW configuration, see our guide on setting up a Linux firewall with UFW.

Limit log size

The default json-file log driver lets logs grow without limit and can fill up your disk. Create the /etc/docker/daemon.json file:

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}

After sudo systemctl restart docker, this applies to newly created containers; you need to recreate existing ones.

Other basic rules

  • Use a fixed tag instead of latest for images (such as postgres:16); this prevents unexpected version jumps.
  • Only pull images from official or trusted publishers.
  • Don't bake passwords into images; use a .env file or Docker secrets.
  • Clean up unused images regularly: docker image prune -a.
  • Don't mount the Docker socket (/var/run/docker.sock) into containers unless you really need to.

For more detail, see Docker's official Ubuntu installation guide.

Which Server Should You Choose for Docker?

Docker itself uses few resources; what drives your needs is the containers you run. As a rough guide: 2-4 GB RAM for a few lightweight services, 6-8 GB for Compose projects with a database + application + cache, and extra CPU and disk headroom if you also build images on the server. For CPU-heavy work such as builds and compression, the processor generation makes a direct difference.

Sunucu Atlas's AMD Ryzen 9 9950X VDS plans, with DDR5 RAM, NVMe storage, KVM virtualization and root access, are a strong foundation if you want to install and manage Docker yourself. If you plan to grow your Compose projects, the SA-R9-9950X-3 plan in this series, with 4 vCPU and 6 GB DDR5 RAM, is a comfortable starting point.

If you'd rather deploy straight from a GitHub repository than deal with Docker installation, reverse proxies and SSL, the AI Hosting plans with the Coolify panel preinstalled may be a better fit: deploying with GitHub and Docker, automatic SSL and one-click databases are all managed from the panel. We cover this approach in detail in our What is Coolify guide.

Next Steps

Once your first container is running, convert one of your existing projects into a compose.yaml file, bind ports to 127.0.0.1, set the log limit and plan backups for your volumes. If you want full control, start with an Istanbul-based Ryzen 9 9950X VDS; if you'd rather deploy through a ready-made panel, start with AI Hosting.

Frequently asked questions

What is the difference between the docker.io package in the Ubuntu repository and docker-ce?

docker.io is packaged by Ubuntu and is usually an older version. docker-ce comes from Docker's official repository and provides the latest release together with the Compose plugin.

Are docker-compose and docker compose the same thing?

In current installs, Compose is a Docker CLI plugin and runs as docker compose (with a space). The old hyphenated docker-compose command was a separate program and isn't needed on new installs.

Why does Docker ignore UFW rules?

Docker adds iptables rules for published ports that are processed before UFW's. Bind ports that shouldn't be public to localhost only, using the form 127.0.0.1:port:port.

Is it safe to add a user to the docker group?

A user in the docker group effectively has root privileges. Grant this group only to trusted users who administer the server.

Does Docker work on shared hosting?

No. Docker requires root access and full kernel features, so you need a VDS with KVM virtualization or a physical server.

For this guide

Recommended plans

If you want to manage Docker yourself with root access, a Ryzen 9 9950X VDS with DDR5 RAM and NVMe storage fits the bill; if you'd rather deploy from GitHub without the setup work, AI Hosting with the Coolify panel preinstalled is the better match.

SA-R9-9950X-1

RYZEN9 9950X
Recommended

2 GB DDR5 RAM

$7.86/mo

+VAT

  • Processor2 vCPU
  • ModelAMD Ryzen 9 9950X
  • Disk40 GB NVMe
  • LocationIstanbul
  • Port: 1G Port
  • Traffic: Fair use
  • Virtualization: KVM
  • Multi-layer DDoS protection

SA-R9-9950X-2

RYZEN9 9950X

4 GB DDR5 RAM

$11.01/mo

+VAT

  • Processor2 vCPU
  • ModelAMD Ryzen 9 9950X
  • Disk50 GB NVMe
  • LocationIstanbul
  • Port: 1G Port
  • Traffic: Fair use
  • Virtualization: KVM
  • Multi-layer DDoS protection

SA-R9-9950X-3

RYZEN9 9950X

6 GB DDR5 RAM

$15.73/mo

+VAT

  • Processor4 vCPU
  • ModelAMD Ryzen 9 9950X
  • Disk60 GB NVMe
  • LocationIstanbul
  • Port: 1G Port
  • Traffic: Fair use
  • Virtualization: KVM
  • Multi-layer DDoS protection

Ryzen 9 9950X VDS — tüm paketler AI Hosting — tüm paketler

Need help with setup?

Send questions about your service in a support ticket, and our technical team will walk you through it step by step.