《docker基礎指令 Cheat Sheet DAY 1 – 學習筆記》

Telegram share !

在軟體開發領域,Docker 已經成為一個極為流行的工具,它能夠幫助開發人員更輕鬆地打包、交付和運行應用程序。雖然許多人可能對 Docker 的概念有所聞,但對於如何開始使用它可能還感到陌生。本教學將帶領你進入 Docker 的世界,介紹基本的 Docker 指令和操作,讓你快速上手並開始使用 Docker 打造你的開發環境。

安裝前準備

  1. Windows
    • 請確保你的 Windows 版本支援 Hyper-V,這是 Docker 在 Windows 上運行所需的虛擬化平台。
    • 在安裝 Docker 前,請確保已啟用並安裝 Hyper-V。
    • 在 Windows 10 以上,請使用 Docker Desktop for Windows 進行安裝,這是官方支援的工具,可簡化安裝和管理 Docker 的流程。
  2. Linux
    • 在 Linux 上安裝 Docker 較為簡單,但請注意你的 Linux 發行版是否被支援。
    • 請確保你具有足夠的權限來安裝和執行 Docker。
    • 可能需要手動安裝 Docker Engine 和 Docker Compose,具體取決於你的發行版。
  3. macOS
    • 在 macOS 上安裝 Docker 需要注意你的系統是否支援 Docker Desktop for Mac。
    • 請確保你的 macOS 版本符合 Docker Desktop 的最低要求。
    • 安裝過程中可能需要開啟系統的虛擬化支援。

※完成以上對應平台檢查後即可到官網點選下載=>官網

驗證 Docker版本

docker --version

基礎指令

run: 啟動一個容器

docker run <image_name>

run: 啟動一個容器,命令退出後容器也會持續在背景執行 ★

docker run <image_name> -d

指令測試

C:\Users\rdpapa>docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:a26bff933ddc26d5cdf7faa98b4ae1e3ec20c4985e6f87ac0973052224d24302
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 desktop應用程式上,

ps – 查看目前正在運行的容器列表

ps – 列出正在運行的容器 ★

docker ps

ps -a – 列出所有容器(包括已停止的) ★

docker ps -a

stop – 停止一個運行中的容器(但不會移除) ★

docker stop <container_id>

restart – 重啟container並且保留他原本的狀態(資料都會保留類似vm的重開機)

docker restart <container_id>

rm – 刪除一個容器 ★

※Tip:

  • 在使用 docker rm 之前,請務必確定您要刪除哪個容器。唯一識別容器 ID 或名稱。刪除錯誤的容器可能導致數據丟失或服務中斷。
  • 備份重要數據,定期備份您的重要數據,尤其是如果它存儲在 Docker 容器中
docker rm <container_id>

rm –force – 強制刪除正在運行的容器

docker rm <container_id> --force

images – 列出本地所有的映像 ★

docker images

or

docker image ls

image ls -f dangling=true – 列出所有虛擬映像檔

#虛擬映像檔是指未存儲在任何資源庫中且未指定任何標記的映像檔。
docker image ls -f dangling=true

rmi – 刪除一個映像 ★

docker rmi <image_id>

or

docker image rm <image_id>

prune – 移除所有虛擬映像檔

docker image prune

search – 從 Docker Hub上搜尋其他人做好的Image ★

search – 從 Docker Hub搜尋 Docker Image

#如果你只是要搜尋ubuntu相關套件非官方也行可以用以下指令
#docker search ubuntu 


C:\Users\rdpapa> docker search ubuntu
NAME                             DESCRIPTION                                      STARS     OFFICIAL
ubuntu                           Ubuntu is a Debian-based Linux operating sys…   17003     [OK]
websphere-liberty                WebSphere Liberty multi-architecture images …   298       [OK]
open-liberty                     Open Liberty multi-architecture images based…   64        [OK]
neurodebian                      NeuroDebian provides neuroscience research s…   107       [OK]
ubuntu-debootstrap               DEPRECATED; use "ubuntu" instead                 52        [OK]
ubuntu-upstart                   DEPRECATED, as is Upstart (find other proces…   115       [OK]
ubuntu/nginx                     Nginx, a high-performance reverse proxy & we…   112
ubuntu/squid                     Squid is a caching proxy for the Web. Long-t…   89
ubuntu/cortex                    Cortex provides storage for Prometheus. Long…   4
ubuntu/prometheus                Prometheus is a systems and service monitori…   60

search -f is-official=true 從 Docker Hub搜尋 Ubuntu 官方的 Docker Image

#docker search ubuntu -f is-official=true

C:\Users\rdpapa> docker search ubuntu -f is-official=true
NAME                 DESCRIPTION                                      STARS     OFFICIAL
ubuntu               Ubuntu is a Debian-based Linux operating sys…   17003     [OK]
websphere-liberty    WebSphere Liberty multi-architecture images …   298       [OK]
open-liberty         Open Liberty multi-architecture images based…   64        [OK]
ubuntu-debootstrap   DEPRECATED; use "ubuntu" instead                 52        [OK]
neurodebian          NeuroDebian provides neuroscience research s…   107       [OK]
ubuntu-upstart       DEPRECATED, as is Upstart (find other proces…   115       [OK]

pull – 將docker hub 上的image pull 下來治本地端

C:\Users\rdpapa>docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
3c645031de29: Pull complete
Digest: sha256:1b8d8ff4777f36f19bfe73ee4df61e3a0b789caeff29caa019539ec7c9a57f95
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

What's Next?
  View a summary of image vulnerabilities and recommendations → docker scout quickview ubuntu

exec – 進入 Docker Container 的 shell 裡面 ★

用於在運行容器中運行命令。它允許您以交互方式訪問容器的 shell 或運行特定命令,而無需啟動新的容器。

進入容器的 shell:

C:\Users\rdpapa> docker exec -it my_container /bin/bash 

root@d161f9a1bf8f:/# 
#注意如果離開 bash:使用ctrl+p後再ctrl+q


# 如果想要再次進入 bash
C:\Users\rdpapa>docker exec -it d161f9a1bf8f /bin/bash

執行容器內的linux指令:

C:\Users\rdpapa> docker exec my_container ls /var/log 

以指定用戶的身份運行命令:

C:\Users\rdpapa> docker exec -u root my_container /usr/bin/mysql -u root -ppassword database_name

logs 顯示指定的容器內的系統log:

C:\Users\rdpapa> docker log <container_id>

inspect顯示指定的容器或映像檔的詳細資訊:

C:\Users\rdpapa> docker inspect <container_id> or <image_id>

※打 ★ 的為常用指令

Reference