對Docker常用命令的整理
阿新 • • 發佈:2019-02-14
檢視docker版本資訊
#docker version
#docker -v
#docker info
image映象操作命令
#docker search image_name //檢索image
#docker pull image_name //下載映象
#docker images //列出本地映象 -a, --all=false Show all images; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs
//刪除一個或者多個映象; -f, --force=false Force; --no-prune=false Do not delete untagged parents
#docker rmi image_name
//顯示一個映象的歷史; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs
#docker history image_name
容器操作
# 在容器中執行"echo"命令,輸出"hello word"
$docker run image_name echo "hello word"
# 互動式進入容器中
$docker run -i -t image_name /bin/bash
# 後臺啟動映象 並更改映象名字
$docker run -d --name myImage centos
# 在容器中安裝新的程式
$docker run image_name yum install -y app_name
# 列出當前所有正在執行的container
$docker ps
# 列出所有的container
$docker ps -a
# 列出最近一次啟動的container
$docker ps -l
# 儲存對容器的修改; -a, --author="" Author; -m, --message="" Commit message
$docker commit ID new_image_name
# 刪除所有容器
$docker rm `docker ps -a -q`
# 刪除單個容器; -f, --force=false; -l, --link=false Remove the specified link and not the underlying container; -v, --volumes=false Remove the volumes associated to the container
$docker rm Name/ID
# 停止、啟動、殺死一個容器
$docker stop Name/ID
$docker start Name/ID
$docker kill Name/ID
# 從一個容器中取日誌; -f, --follow=false Follow log output; -t, --timestamps=false Show timestamps
$docker logs Name/ID
# 列出一個容器裡面被改變的檔案或者目錄,list列表會顯示出三種事件,A 增加的,D 刪除的,C 被改變的
$docker diff Name/ID
# 顯示一個執行的容器裡面的程序資訊
$docker top Name/ID
# 從容器裡面拷貝檔案/目錄到本地一個路徑
$docker cp Name:/container_path to_path
$docker cp ID:/container_path to_path
# 重啟一個正在執行的容器; -t, --time=10 Number of seconds to try to stop for before killing the container, Default=10
$docker restart Name/ID
# 附加到一個執行的容器上面; --no-stdin=false Do not attach stdin; --sig-proxy=true Proxify all received signal to the process
$docker attach ID
#訪問另一個容器的名稱空間 進入另一個容器
#安裝Linux工具包
$ yum install -y util-linux
#獲取容器的Pid
$docker inspect --format "{{.State.Pid}}" containerName
#進入容器
$ nsenter --target Pid --mount --uts --ipc --net --pid
#容器網路配置
#隨機生成container到host埠對映
$docker run -d -P --name myNginx nginx
#指定特定埠 將container 80到host91埠的對映
$docker run -d -p 91:80 --name myNginx imageName
# -p ip: hostPort:containerPosrt
$docker ps -l
docker資料管理
# -v 繫結掛載一個數據卷 -h 給容器指定一個主機名
$docker run -it --name volume-test1 -h nginx -v /data/ imageName
#或著手動設定對映
$docker run -it --name volume-test1 -h nginx -v /opt:/opt imageName
#掛載另一容器, 另一容器volume-test2(即使容器已經停掉)來做volume-test1的專門的儲存
$docker run -it --name volume-test1 -h nginx --volumes-from volume-test2 imageName
#顯示資料捲到host主機的對映關係
$docker inspect -f {{.Volumes}} volume-test1