Docker 常用命令整理(實(shí)用篇)
1. 查看docker信息(version、info)
# 查看docker版本 docker version # 顯示docker系統(tǒng)的信息 docker info
2. 對(duì)image的操作(search、pull、images、rmi、history)
# 檢索image docker search image_name # 下載image docker pull image_name #列出鏡像列表; -a, --all=false Show all images; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs docker images # 刪除一個(gè)或者多個(gè)鏡像; -f, --force=false Force; --no-prune=false Do not delete untagged parents docker rmi image_name # 顯示一個(gè)鏡像的歷史; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs docker his
3. 啟動(dòng)容器(run)
docker容器可以理解為在沙盒中運(yùn)行的進(jìn)程。這個(gè)沙盒包 含了該進(jìn)程運(yùn)行所必須的資源,包括文件系統(tǒng)、系統(tǒng)類庫(kù)、shell 環(huán)境等等。但這個(gè)沙盒默認(rèn)是不會(huì)運(yùn)行任何程序的。你需要在沙盒中運(yùn)行一個(gè)進(jìn)程來啟動(dòng)某一個(gè)容器。這個(gè)進(jìn)程是該容器的唯一進(jìn)程,所以當(dāng)該進(jìn)程結(jié)束的時(shí)候,容 器也會(huì)完全的停止。
# 在容器中運(yùn)行"echo"命令,輸出"hello word" docker run image_name echo "hello word" # 交互式進(jìn)入容器中 docker run -i -t image_name /bin/bash # 在容器中安裝新的程序 docker run image_name apt-get install -y app_name # 在一次進(jìn)剛才進(jìn)入的容器 docker exec -i -t [容器ID]
Note: 在執(zhí)行apt-get 命令的時(shí)候,要帶上-y參數(shù)。如果不指定-y參數(shù)的話,apt-get命令會(huì)進(jìn)入交互模式,需要用戶輸入命令來進(jìn)行確認(rèn),但在docker環(huán)境中是無法響 應(yīng)這種交互的。apt-get 命令執(zhí)行完畢之后,容器就會(huì)停止,但對(duì)容器的改動(dòng)不會(huì)丟失。
4. 查看容器(ps)
# 列出當(dāng)前所有正在運(yùn)行的container docker ps # 列出所有的container docker ps -a # 列出最近一次啟動(dòng)的container docker ps -l
5. 保存對(duì)容器的修改(commit)
當(dāng)你對(duì)某一個(gè)容器做了修改之后(通過在容器中運(yùn)行某一個(gè)命令),可以把對(duì)容器的修改保存下來,這樣下次可以從保存后的最新狀態(tài)運(yùn)行該容器。
# 保存對(duì)容器的修改; -a, --author="" Author; -m, --message="" Commit message docker commit ID new_image_name
6. 對(duì)容器的操作(rm、stop、start、kill、logs、diff、top、cp、restart、attach)
# 刪除所有容器 docker rm `docker ps -a -q` # 刪除單個(gè)容器; -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 # 停止、啟動(dòng)、殺死一個(gè)容器 docker stop Name/ID docker start Name/ID docker kill Name/ID # 從一個(gè)容器中取日志; -f, --follow=false Follow log output; -t, --timestamps=false Show timestamps docker logs Name/ID # 列出一個(gè)容器里面被改變的文件或者目錄,list列表會(huì)顯示出三種事件,A 增加的,D 刪除的,C 被改變的 docker diff Name/ID # 顯示一個(gè)運(yùn)行的容器里面的進(jìn)程信息 docker top Name/ID # 從容器里面拷貝文件/目錄到本地一個(gè)路徑 docker cp Name:/container_path to_path docker cp ID:/container_path to_path # 重啟一個(gè)正在運(yùn)行的容器; -t, --time=10 Number of seconds to try to stop for before killing the container, Default=10 docker restart Name/ID # 附加到一個(gè)運(yùn)行的容器上面; --no-stdin=false Do not attach stdin; --sig-proxy=true Proxify all received signal to the process docker attach ID
Note: attach命令允許你查看或者影響一個(gè)運(yùn)行的容器。你可以在同一時(shí)間attach同一個(gè)容器。你也可以從一個(gè)容器中脫離出來,是從CTRL-C。
7. 保存和加載鏡像(save、load)
當(dāng)需要把一臺(tái)機(jī)器上的鏡像遷移到另一臺(tái)機(jī)器的時(shí)候,需要保存鏡像與加載鏡像。
# 保存鏡像到一個(gè)tar包; -o, --output="" Write to an file docker save image_name -o file_path # 加載一個(gè)tar包格式的鏡像; -i, --input="" Read from a tar archive file docker load -i file_path # 機(jī)器a docker save image_name > /home/save.tar # 使用scp將save.tar拷到機(jī)器b上,然后: docker load < /home/save.tar
8、 登錄registry server(login)
# 登陸registry server; -e, --email="" Email; -p, --password="" Password; -u, --username="" Usernamedocker login
9. 發(fā)布image(push)
# 發(fā)布docker鏡像 docker push new_image_name
10. 根據(jù)Dockerfile 構(gòu)建出一個(gè)容器
#build --no-cache=false Do not use cache when building the image -q, --quiet=false Suppress the verbose output generated by the containers --rm=true Remove intermediate containers after a successful build -t, --tag="" Repository name (and optionally a tag) to be applied to the resulting image in case of success docker build -t image_name Dockerfile_path
其它參考:
http://blog.csdn.net/wsscy2004/article/details/25878363
http://blog.csdn.net/iloveyin/article/details/40542431
相關(guān)文章
docker可視化管理工具portainer忘記密碼重置教程的實(shí)現(xiàn)
本文主要介紹了docker可視化管理工具portainer忘記密碼重置教程的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08基于Docker部署Tomcat集群、 Nginx負(fù)載均衡的問題小結(jié)
這篇文章主要介紹了基于Docker部署 Tomcat集群、 Nginx負(fù)載均衡,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09云原生教程之使用Docker部署webssh工具sshwifty
Sshwifty是一個(gè)開源的WebSSH?&?WebTelnet客戶端,下面這篇文章主要給大家介紹了關(guān)于云原生教程之使用Docker部署webssh工具sshwifty的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03用docker搭建selenium grid分布式環(huán)境實(shí)踐之路
這篇文章主要介紹了用docker搭建selenium grid分布式環(huán)境實(shí)踐之路,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03為運(yùn)行中的docker容器設(shè)置時(shí)區(qū)
本文主要介紹了為運(yùn)行中的docker容器設(shè)置時(shí)區(qū),主要介紹了2種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05