Docker 常用指令 #
1. Docker Service #
- 查看 Docker 版本資訊 #
bash
docker version- 查看 Docker 目前狀態 #
bash
service docker status- 啟動 Docker Service #
bash
service docker start- 重啟 Docker Service #
bash
service docker restart- 停止 Docker Service #
bash
service docker stop2. Image #
- 拉取 image #
bash
docker pull [image name]:[image tag]從 Docker Hub 下載 nginx 和 ubuntu 的 image
bash
docker pull nginx:latest
docker pull ubuntu:20.04- 查看已下載的 image #
bash
docker images- 搜尋 image #
bash
docker search [keyword]搜尋與 cassandra 和 python 相關的 image
bash
docker search cassandra
docker search python- 查看 image 詳細資訊 #
bash
docker inspect [image name]查看 nginx 和 ubuntu image 的詳細資訊
bash
docker inspect nginx
docker inspect ubuntu:20.04- 建立 image(使用 Dockerfile) #
bash
docker build -t [repository:tag] .使用當前目錄的 Dockerfile 建立自定義 image
bash
docker build -t myapp:v1.0 .
docker build -t easonimg/pythonapp3 .- 建立 image(不使用快取) #
bash
docker build --no-cache -t [repository:tag] .強制重新建立 image,不使用快取
bash
docker build --no-cache -t myapp:v1.0 .- 為 image 新增標籤 #
bash
docker tag [old tag] [new tag]為現有的 image 建立新的標籤
bash
docker tag easonimg/pythonapp3 easonimg/pythonapp3_new- 刪除 image #
bash
docker rmi [image id]刪除不需要的 image
bash
docker rmi nginx
docker rmi ubuntu:20.04- 儲存 image #
bash
docker save -o [filename] [image name]將 image 儲存為檔案,方便備份或傳輸
bash
docker save -o imgFlask tiangolo/nginx_flask- 載入 image #
bash
docker load -i [filename]從檔案載入 image
bash
docker load -i imgFlask- 上傳 image 至倉庫 #
bash
docker push [repository:tag]將自建的 image 上傳到 Docker Hub
bash
docker push easonimg/docker.uwsgi-nginx-flask-emperor3. Container #
- 創建並運行 Container #
bash
docker run [OPTIONS] [image name]:[image tag] 建立並運行簡單的 hello-world Container 和 nginx web server
bash
docker run hello-world
docker run -d -p 8080:80 --name mynginx nginx- 查看 Container 狀態 #
bash
docker ps -a- 查看運行中的 Container #
bash
docker ps- 啟動 Container #
bash
docker start [container id] 啟動已停止的 Container
bash
docker start mynginx- 停止 Container #
bash
docker stop [container id] 停止正在運行的 Container
bash
docker stop mynginx- 進入運行中的 Container #
bash
docker exec -it [container id] /bin/bash 進入 Container 內部進行操作
bash
docker exec -it mynginx /bin/bash- 查看 Container 日誌 #
bash
docker logs [container id] 查看 Container 的輸出日誌
bash
docker logs mynginx- 查看 Container 詳細資訊 #
bash
docker inspect [container id] 查看 Container 的詳細配置資訊
bash
docker inspect mynginx- 刪除 Container #
bash
docker rm [container id] 刪除已停止的 Container
bash
docker rm mynginx- 匯出 Container #
bash
docker export -o [filename] [container id] 將 Container 匯出為檔案
bash
docker export -o mycontainer.tar mynginx- 匯入 Container 為 image #
bash
cat [filename] | docker import - [repository:tag] 將匯出的 Container 檔案重新匯入為 image
bash
cat mycontainer.tar | docker import - easonimg/mycontainer- 複製檔案(Host → Container) #
bash
docker cp [host file] [container id]:[container path] 將主機的檔案複製到 Container 內部
bash
docker cp config.yaml mynginx:/etc/config.yaml- 複製檔案(Container → Host) #
bash
docker cp [container id]:[container path] [host file] 將 Container 內的檔案複製到主機
bash
docker cp mynginx:/etc/nginx/nginx.conf nginx.conf- 建立 Container 並設定 Port 映射 #
bash
docker run -d -p [host port]:[container port] --name [container name] [image] 建立 web server Container 並映射端口
bash
docker run -d -p 8080:80 --name mynginx nginx
docker run -d -p 8080:80 -p 8181:81 --name myapp tiangolo/nginx_flask- 建立 Container 並掛載目錄 #
bash
docker run -d --name [container name] -v [host path]:[container path] [image] 建立 Container 並掛載主機目錄,實現資料持久化
bash
docker run -d --name myapp -v /home/app:/app tiangolo/nginx_flask- 建立 Container 並設定時區 #
bash
docker run -d -p [host port]:[container port] --name [container name] -e "TZ=Asia/Taipei" [image] 建立 Container 並設定台灣時區
bash
docker run -d -p 8080:80 --name myapp -e "TZ=Asia/Taipei" easonimg/docker.nginx-flask-python-pandas- 建立 Container 並使用 Host 網路 #
bash
docker run -d --net=host --restart=always [image] 建立 Container 並使用主機網路模式
bash
docker run -d --net=host --restart=always easonimg/imgID- 設定 Container 資源限制 #
bash
docker run --cpus="[cpu limit]" -m [memory limit] [image] 限制 Container 的 CPU 和記憶體使用量
bash
docker run --cpus="1.5" -m 512m nginx4. Volume #
- 創建 Volume #
bash
docker volume create [volume name] 建立資料持久化的 Volume
bash
docker volume create mydata
docker volume create postgres_data- 查看所有 Volume #
bash
docker volume ls- 查看 Volume 詳細資訊 #
bash
docker volume inspect [volume name] 查看 Volume 的詳細配置資訊
bash
docker volume inspect mydata- 將 Volume 掛載到 Container #
bash
docker run -v [volume name]:[container path] [image] 將 Volume 掛載到 Container 內部,實現資料持久化
bash
docker run -v mydata:/app/data nginx
docker run -v postgres_data:/var/lib/postgresql/data postgres- 刪除 Volume #
bash
docker volume rm [volume name] 刪除不需要的 Volume
bash
docker volume rm mydata- 建立 NFS Volume #
bash
docker volume create --driver local \
--opt type=nfs \
--opt o=nfsvers=3,addr=[nfs server ip],rw \
--opt device=:/[nfs folder] \
[volume name] 建立連接到 NFS 伺服器的 Volume
bash
docker volume create --driver local \
--opt type=nfs \
--opt o=nfsvers=3,addr=192.168.0.55,rw \
--opt device=:/nfsfolder \
volume_nfs