# [學習紀錄] Docker 學習 - 常用操作指令 ###### tags: `Docker` 此篇記錄了在學習操作與了解 Docker 的觀念與指令。 ## 常用 Docker 指令 ### 查看資訊 * 顯示 Docker 的資訊 ``` $ docker info ``` * 顯示 Docker 的版本 ``` $ sudo docker version ``` * 搜尋 Docker Hub上的 Image ``` $ sudo docker search ubuntu -f is-official=true NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian-based Linux operating sys… 11601 [OK] websphere-liberty WebSphere Liberty multi-architecture images … 264 [OK] ubuntu-upstart Upstart is an event-based replacement for th… 109 [OK] neurodebian NeuroDebian provides neuroscience research s… 76 [OK] ubuntu-debootstrap debootstrap --variant=minbase --components=m… 44 [OK] open-liberty Open Liberty multi-architecture images based… 39 [OK] ``` * 取得一個指定版本的 Image 如果不指定 image 版本該版本則為最新版 ( latest ) ``` $ docker pull [Image 名稱]:[Image 版本] ``` * 顯示目前電腦上有的 Docker Images ``` $ sudo docker images ``` ### 建立 * 透過指定的Image啟動一個新的Container 使用下載的Docker Image來啟動Container,並可以在後面加上啟動參數(Port對應、終端機、掛載資料夾等) ``` $ sudo docker run [Image 名稱]:[Image 版本] [執行指令] # [常用執行指令 / OPTIONS]: # -i, --interactive (互動模式) # -t, --tty (配置一個終端機) # -d, --detach (在背景執行) # -p, --port (Port對應) # -v, --volume (掛載資料夾) ``` * 啟動一個最新版本的Ubuntu Container 可以透過`-d`參數來決定是否要將Container放到背景執行,如果輸入`-d`放到背景執行的話會回傳一個Container-ID作為識別這個已啟動Container的ID。 ``` # 前景執行 $ sudo docker run -it ubuntu bash # 背景執行 $ sudo docker run -d -it ubuntu bash ``` * 指定Host與Container之間的Port對應關係 ``` $ sudo docker run -p [Host Port:Container Port] ``` * 掛載Host資料夾到Container內 **路徑要使用絕對路徑,不能使用 `~` 或相對路徑作為掛載的路徑。** ``` $ sudo docker run -v /host/directory:/container/directory ``` ### 開始 * 啟動已停止的Container ``` $ sudo docker start [Container-ID或Container-Name] ``` ### 停止 * 停止已啟動的Container ``` $ sudo docker stop [Container-ID或Container-Name] ``` ### 刪除 * 刪除目前電腦上的 Docker Images ``` $ sudo docker rmi [Image-ID] # 強制刪除 $ sudo docker rmi -f [Image-ID] ``` * 刪除目前電腦上的 container ``` $ sudo docker rm [Container-ID] # 強制刪除 $ sudo docker rm -f [Container-ID] ``` ### 重新啟動 * 重新啟動的Container 如果要作為背景服務的話,透過 `--restart=always` 讓 Docker 自動幫容器重新啟動。 重新啟動包含下列情境 * Host 開機時自動啟動容器 (是的,預設開機時是不會啟動的喔) * 在容器中不小心輸入 exit 退出運行時自動重啟 * 容器運行中的程式當掉了,自動重啟 ``` $ sudo docker run --restart=always -d -it ubuntu ``` ### 狀態 * 查看目前所有正在執行的Container ``` $ sudo docker ps -a ``` ### 進入容器 * 進入已經啟動的Container ``` $ sudo docker exec -it [Container-ID或Container-Name] bash ``` ## 參考 * Docker指令 1. [【Day 3】 - Docker 基本指令操作](https://ithelp.ithome.com.tw/articles/10186431) 2. [Docker run reference](https://docs.docker.com/engine/reference/run/) 3. [使用 Docker - 背景服務](https://ithelp.ithome.com.tw/articles/10199991) 4. [Day4:用簡單的例子來說明如何使用 Docker 指令](https://ithelp.ithome.com.tw/articles/10190921) 5. [Day6:把 Docker Image Push 到 Docker Hub](https://ithelp.ithome.com.tw/articles/10191139) * 掛載資料夾 1. [How to mount a host directory in a Docker container](https://stackoverflow.com/questions/23439126/how-to-mount-a-host-directory-in-a-docker-container)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up