متقدم
البحث فيأدلة التثبيت
طريقة البحث
  • الصق رسالة الخطأ بين علامتي تنصيص، مثل "permission denied"، لتجد جدول الحل.
  • اختر مجالًا وجزءًا من الدليل (التثبيت، أول الإعدادات، الاستخدام اليومي، الأمان، الحلول) لتصل مباشرة إلى الخطوة الصحيحة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الدليل 1Docker Engine · Containers

Install and set up Docker Engine

Docker runs apps in containers: small, isolated packages with the app and everything it needs. It is the first tool on almost every DevOps server and laptop. This guide installs Docker Engine from the official Docker repository on RHEL and Ubuntu, makes the first important settings (log limits, safe defaults), shows the daily commands and gives a security checklist and fixes for common problems.

  • 11 الدروس
  • 14 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

1.1

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.

ItemValue
Packagesdocker-ce, docker-ce-cli, containerd.io, docker-buildx-plugin, docker-compose-plugin
Servicedocker.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 portnone by default (never open the API on TCP 2375)
Logs of the servicejournalctl -u docker
Logs of a containerdocker 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 sudo and internet access to download.docker.com.

ملاحظة

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.

1.2

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

Start the service now and at every boot, then test it:

systemctl enable --now docker
docker run --rm hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.

1.3

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 debian instead of ubuntu in the two URLs.
  • On Ubuntu and Debian the service starts and is enabled by the package. Check with systemctl is-enabled docker.

انتبه

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.

1.4

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     NAMES

انتبه

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.

1.5

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.

/etc/docker/daemon.json
{
  "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 } ]
}
SettingWhy
log-opts max-size / max-fileEach container keeps at most 3 log files of 10 MB.
live-restoreContainers keep running when you restart or update the Docker service.
no-new-privilegesA process in a container can not get more rights with setuid programs.
userland-proxy: falsePublished ports use iptables/nftables rules, faster and fewer processes.
default-address-poolsDocker 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=true

تذكّر

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.

1.6

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.

docker run -d --name web -p 127.0.0.1:8080:80 nginx
curl -I http://127.0.0.1:8080
HTTP/1.1 200 OK

انتبه

  • 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:80 when 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.

1.7

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 df

Most apps have more than one container (app + database + cache). Describe them in one compose.yaml file and start them together:

compose.yaml
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.txt
docker compose up -d
docker compose ps
docker compose logs -f db
docker compose down

Real uses at work:

الأوامر في هذا الدرس

1.8

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 docker group; rootless mode on shared hosts.
  • Use small official images with a fixed tag (nginx:1.27), not latest. Scan them: trivy image nginx:1.27.
  • Run apps as a non-root user (USER app in 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 ENV lines; use Compose secrets or a secret manager.
  • Bind ports to 127.0.0.1 when a proxy is in front.
  • Keep SELinux on (RHEL). Use :Z on bind mounts: -v ./data:/data:Z.
  • Update Docker and rebuild images often (new base image = new security fixes).

1.9

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/docker

في العمل

docker system prune does not delete volumes. Add --volumes only when you are sure no database data lives in an unnamed volume.

1.10

Fix common problems

ProblemCauseFix
permission denied … /var/run/docker.sockYour 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 daemonThe service is stopped or failed.systemctl start docker, then journalctl -u docker -n 50.
Service fails after a config changeBad JSON in daemon.json.python3 -m json.tool /etc/docker/daemon.json, fix, restart.
port is already allocatedAnother container or program uses the host port.ss -ltnp to find it; use another host port.
no space left on deviceOld images, build cache or big logs.docker system df, docker system prune, set log limits.
dnf: conflicts with podman or runcRHEL container tools are installed.dnf remove podman runc buildah, then install again.
Containers have no internet / DNSFirewall 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).

1.11

Next steps

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.

كل الأوامر في هذا الفصل