$ run as a normal user# run as root (or with sudo)
Know it before you install
Docker Engine is a background service (dockerd) plus a command-line tool (docker). You type docker …, the CLI talks to the service over a socket, and the service starts the containers with containerd and runc.
| Item | Value |
|---|---|
| Packages | docker-ce, docker-ce-cli, containerd.io, docker-buildx-plugin, docker-compose-plugin |
| Service | docker.service (and docker.socket) |
| Main config file | /etc/docker/daemon.json (you create it) |
| Data (images, volumes) | /var/lib/docker/ |
| Socket | /var/run/docker.sock — whoever can use it is root on the host |
| Network port | none by default (never open the API on TCP 2375) |
| Logs of the service | journalctl -u docker |
| Logs of a container | docker logs NAME |
What you need:
- A 64-bit server or VM: RHEL / Rocky / Alma 8–10, Fedora, Ubuntu 22.04 / 24.04 or Debian 12.
- 2 CPU, 2 GB RAM and 20 GB free disk are fine for labs. Images fill the disk fast in real work.
- A user with
sudoand internet access todownload.docker.com.
Note
Use the official Docker repository, not the old docker.io package of the distribution or the podman-docker package. Only the official repo gives you the newest Engine, Buildx and the docker compose plugin.
Install on RHEL, Rocky, Alma and Fedora
RHEL ships Podman by default. Remove the old or conflicting packages first, then add the Docker repository and install.
dnf -y remove docker docker-client docker-common docker-latest docker-engine podman runc
dnf -y install dnf-plugins-core
dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo
dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin- Rocky and AlmaLinux: use the CentOS repo:
https://download.docker.com/linux/centos/docker-ce.repo. - Fedora 41 and newer (dnf5):
dnf config-manager addrepo --from-repofile=https://download.docker.com/linux/fedora/docker-ce.repo. - dnf asks you to accept the Docker GPG key. Check that the fingerprint is
060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35.
Start the service now and at every boot, then test it:
Install on Ubuntu and Debian
Remove the old packages, add Docker's signing key and repository, then install.
apt-get remove -y docker.io docker-compose docker-doc podman-docker containerd runc
apt-get update
apt-get install -y ca-certificates curl
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
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" > /etc/apt/sources.list.d/docker.list
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
docker run --rm hello-world- Debian: write
debianinstead ofubuntuin the two URLs. - On Ubuntu and Debian the service starts and is enabled by the package. Check with
systemctl is-enabled docker.
Watch out
The script curl -fsSL https://get.docker.com | sh is fast, but it runs a script from the internet as root and installs the newest version without asking. Use it only in throw-away labs, never on production servers.
Run Docker without sudo (and what it costs)
By default only root can use the socket. To let your own user run docker, add it to the docker group and log in again.
usermod -aG docker alice
newgrp docker
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESWatch out
The docker group is the same as root on that host: a member can start a container that mounts / and change any file. Add only trusted admin users. On shared servers use sudo docker … or rootless mode (dockerd-rootless-setuptool.sh install), where the daemon runs as a normal user.
First configuration: the settings to make first
Make these settings on every new Docker host. Without log limits, one busy container can fill the whole disk.
{
"log-driver": "json-file",
"log-opts": { "max-size": "10m", "max-file": "3" },
"live-restore": true,
"no-new-privileges": true,
"userland-proxy": false,
"default-address-pools": [ { "base": "172.30.0.0/16", "size": 24 } ]
}| Setting | Why |
|---|---|
log-opts max-size / max-file | Each container keeps at most 3 log files of 10 MB. |
live-restore | Containers keep running when you restart or update the Docker service. |
no-new-privileges | A process in a container can not get more rights with setuid programs. |
userland-proxy: false | Published ports use iptables/nftables rules, faster and fewer processes. |
default-address-pools | Docker networks use 172.30.x.0/24. Change it when it overlaps your company or VPN network. |
data-root (optional) | Move images and volumes to a big disk, for example "data-root": "/data/docker". |
Check the file and apply it:
python3 -m json.tool /etc/docker/daemon.json
systemctl restart docker
docker info --format '{{.LoggingDriver}} live-restore={{.LiveRestoreEnabled}}'
json-file live-restore=trueRemember
A wrong daemon.json (one missing comma) stops the service from starting. Always check the JSON first, and read journalctl -u docker -n 30 when the restart fails.
Publish ports and the firewall
-p 8080:80 sends traffic from port 8080 of the host to port 80 in the container. Docker writes its own firewall rules for this.
Watch out
- A published port is open to the whole network, even when UFW or firewalld says the port is closed — Docker's rules come first. Write
-p 127.0.0.1:8080:80when only a local reverse proxy (nginx) needs the port. - On RHEL with firewalld, Docker uses its own zone
docker. Do not delete Docker's rules by hand; restart the service to create them again.
Daily use: the commands and the real uses
The 12 commands you use every day:
docker run -d --name web -p 127.0.0.1:8080:80 --restart unless-stopped nginx:1.27
docker ps -a
docker logs -f --tail 50 web
docker exec -it web sh
docker stop web && docker rm web
docker images
docker pull nginx:1.27
docker build -t myapp:1.0 .
docker volume ls
docker network ls
docker stats --no-stream
docker system dfMost apps have more than one container (app + database + cache). Describe them in one compose.yaml file and start them together:
services:
web:
image: nginx:1.27
ports: ["127.0.0.1:8080:80"]
restart: unless-stopped
db:
image: postgres:16
environment:
POSTGRES_PASSWORD_FILE: /run/secrets/db_pass
secrets: [db_pass]
volumes: [pgdata:/var/lib/postgresql/data]
volumes:
pgdata:
secrets:
db_pass:
file: ./db_pass.txtReal uses at work:
- Run apps and their databases on one server with Compose.
- Build the app image in CI (
docker build,docker pushto a registry). - Start a tool once without installing it:
docker run --rm -v "$PWD":/src aquasec/trivy fs /src. - Give every developer the same environment.
Make it safe
Security checklist:
- Never expose the API on TCP 2375. If you need remote access, use SSH:
DOCKER_HOST=ssh://admin@server docker ps. - Only trusted admins in the
dockergroup; rootless mode on shared hosts. - Use small official images with a fixed tag (
nginx:1.27), notlatest. Scan them:trivy image nginx:1.27. - Run apps as a non-root user (
USER appin the Dockerfile). - Drop rights:
--cap-drop ALL --read-only --security-opt no-new-privileges. - Limit resources:
--memory 512m --cpus 1. - Keep secrets out of images and out of
ENVlines; use Compose secrets or a secret manager. - Bind ports to
127.0.0.1when a proxy is in front. - Keep SELinux on (RHEL). Use
:Zon bind mounts:-v ./data:/data:Z. - Update Docker and rebuild images often (new base image = new security fixes).
Update, clean up and remove
dnf -y upgrade docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
apt-get update && apt-get install --only-upgrade -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
docker system prune
docker image prune -a --filter "until=168h"Remove Docker completely (this deletes all images, containers and volumes):
dnf -y remove docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
apt-get purge -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
rm -rf /var/lib/docker /var/lib/containerd /etc/dockerAt work
docker system prune does not delete volumes. Add --volumes only when you are sure no database data lives in an unnamed volume.
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
permission denied … /var/run/docker.sock | Your user is not in the docker group, or you did not log in again. | usermod -aG docker USER, then log out and in (or newgrp docker). |
Cannot connect to the Docker daemon | The service is stopped or failed. | systemctl start docker, then journalctl -u docker -n 50. |
| Service fails after a config change | Bad JSON in daemon.json. | python3 -m json.tool /etc/docker/daemon.json, fix, restart. |
port is already allocated | Another container or program uses the host port. | ss -ltnp to find it; use another host port. |
no space left on device | Old images, build cache or big logs. | docker system df, docker system prune, set log limits. |
dnf: conflicts with podman or runc | RHEL container tools are installed. | dnf remove podman runc buildah, then install again. |
| Containers have no internet / DNS | Firewall rules or a local DNS like 127.0.0.53. | Restart docker; set "dns": ["10.0.0.2", "1.1.1.1"] in daemon.json. |
Permission denied on a bind mount (RHEL) | SELinux label. | Mount with :Z (private) or :z (shared). |
Next steps
- Quick how-tos: Install Docker, Run a container and publish a port, See the logs of a container, Free disk space used by Docker, Write a small and safe Dockerfile, Run an app with Compose, Check disk space, Container limits, Container networking, Back up a volume, Multi-arch builds.
- Every
dockersub-command and option: Command page: docker. - Learn Docker step by step: Docker summary — Dockerfile, Compose, Security.
- Practise: Lab: web app, database and cache with Compose.
- Where Docker fits in real work: Containers and Kubernetes services.
- Scan your images: Command page: trivy. Rootless alternative: Command page: podman.
1Why do you set max-size and max-file in daemon.json?
Without them, container logs grow without limit and can fill the disk.
2You published -p 8080:80 and UFW blocks 8080. Is the port closed?
No. Docker's firewall rules come before UFW, so the port is open. Use -p 127.0.0.1:8080:80.
3Why is the docker group dangerous?
Its members can start a container that mounts the host's /, so they are root on the host.