如何在 Ubuntu 24.04 安裝 Docker 與 Docker Compose

先移除可能衝突的舊套件,這不會自動刪除 /var/lib/docker/ 裡面的既有 Docker 資料。

sudo apt remove -y docker.io docker-compose docker-compose-v2 docker-doc docker-buildx podman-docker containerd runc

接著安裝必要套件

sudo apt update
sudo apt install -y ca-certificates curl

建立 Docker GPG Key

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

加入 Docker 官方 Repository

sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/docker.asc
EOF

更新套件

sudo apt update

安裝 Docker + Compose

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

套件用途說明如下

  • docker-ce:Docker Engine
  • docker-ce-cli:Docker 指令
  • containerd.io:Container Runtime
  • docker-buildx-plugin:Docker Buildx
  • docker-compose-plugin:Docker Compose V2

確認 Docker 狀態

sudo systemctl status docker

如果沒有啟動

sudo systemctl start docker
sudo systemctl enable docker

測試 Docker

sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
4f55086f7dd0: Pull complete
d5e71e642bf5: Download complete
Digest: sha256:5e23090353324d887c48ad5e5c56d294eab81588df9605b07d1afe895f9cc8f8
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

確認 Docker Compose

docker compose version
Docker Compose version v5.5.1

把目前使用者加入 docker 群組,然後重新登入 SSH。

sudo usermod -aG docker $USER

如果不用 sudo 也可以執行,就完成了。

docker ps

測試 Docker Compose

mkdir -p ~/docker/test
cd ~/docker/test

建立 compose.yaml

nano compose.yaml
services:
nginx:
image: nginx:latest
container_name: test-nginx
ports:
- "8080:80"
restart: unless-stopped

啟動 Nginx Service

docker compose up -d
[+] up 12/12
✔ Image nginx:latest Pulled
✔ Network test_default Created
✔ Container test-nginx Started

查看 Docker Compose

docker compose ps
NAME         IMAGE          COMMAND                  SERVICE   CREATED          STATUS          PORTS
test-nginx nginx:latest "/docker-entrypoint.…" nginx 32 seconds ago Up 30 seconds 0.0.0.0:8080->80/tcp, [::]:8080->80/tcp

測試 Nginx 頁面

 curl http://localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, nginx is successfully installed and working.
Further configuration is required for the web server, reverse proxy,
API gateway, load balancer, content cache, or other features.</p>

<p>For online documentation and support please refer to
<a href="https://nginx.org/">nginx.org</a>.<br/>
To engage with the community please visit
<a href="https://community.nginx.org/">community.nginx.org</a>.<br/>
For enterprise grade support, professional services, additional
security features and capabilities please refer to
<a href="https://f5.com/nginx">f5.com/nginx</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

停止 Nginx Service

docker compose down
[+] down 2/2
✔ Container test-nginx Removed
✔ Network test_default Removed

今天的分享就到這邊,感謝收看。

參考文件

https://docs.docker.com/engine/install/ubuntu