1. 程式人生 > >【Docker】容器操作(轉)

【Docker】容器操作(轉)

來自:https://www.cnblogs.com/zydev/p/5803461.html

列出主機上的容器

列出正在執行的容器:            docker ps

列出所有容器:               docker ps -a 

列出最近使用的容器,包括沒有執行的:    docker ps -l

僅列出容器的ID,不包括沒有執行的:        docker ps q

 

建立容器

引數:

--name  指定容器的名字

--rm      容器執行完畢會自動刪除

-i -t       建立一個提供互動式shell的容器。

-d         在後臺執行容器,並且打印出容器的ID。

建立互動式容器

[[email protected] ~]$ docker run --name  weblogic -i -t centos /bin/bash
[[email protected] /]# exit
exit

對於互動式容器,當退出shell後,容器會關閉。 後面可以通過命令重新啟動容器。

 

建立守護式容器

這種容器指容器可以長期一直執行,沒有互動式會話,非常適合容器中執行後臺應用程式和服務(如資料庫服務、web伺服器等)。

[[email protected] ~]$ docker run -d centos /bin/bash -c  "while true;do echo hello docker;sleep 1;done" 
af2e15753ec8e27f5a7daad6881cd23c9e098330e9be66920e78e07e8844ac8c

 

啟動和停止容器

docker start/stop/restart continer id/name

docker -t 可以指定時間停止容器,預設是10s

強制停止容器

docker kill continer id/name

 

附著到一個容器上

docker attach continer id/name

當重新啟動容器時,會沿用建立容器(docker run)命令時指定的引數來執行,可能需要按回車才進入。

這時就已經相當於在容器內部了的shell操作了。如果操作過程中,退出了shell。容器也會隨之停止。

複製程式碼
[[email protected] ~]$ docker run -i -t centos /bin/bash
[[email protected] /]# exit
exit
[[email protected] ~]$ docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
cf9b70252ba5        centos              "/bin/bash"         22 seconds ago      Exited (0) 11 seconds ago                       happy_bohr          
[[email protected] ~]$ docker start cf9
cf9
[[email protected] ~]$ docker attach cf9
[[email protected] /]# 
複製程式碼

 

在執行的容器中執行命令

複製程式碼
[[email protected] ~]$ docker exec -t -i af2 /bin/bash
[[email protected] /]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 03:27 ?        00:00:00 /bin/bash -c while true;do echo hello docker;sleep
root        33     0  0 03:28 ?        00:00:00 /bin/bash
root        54    33 10 03:28 ?        00:00:00 ps -ef
root        55     1  0 03:28 ?        00:00:00 sleep 1
複製程式碼

可以通過docker exec命令在容器內部額外啟動新程序,退出容器,並不會停止容器。

 

檢視容器的詳細資訊

檢視容器的執行時後臺輸出資訊

docker logs continer id/name

 

複製程式碼
[[email protected] ~]$ docker logs cf9
[[email protected] /]# exit
exit
[[email protected] /]# exit
exit
[[email protected] /]# 
[[email protected] /]# exit
exit
[[email protected] ~]$ docker logs af2
hello docker
hello docker
hello docker
複製程式碼

 

利用docker inspect 命令可以檢視容器更多的資訊。 如ip地址等,這對守護容器還是很有意義的。

  View Code

 

刪除已經停止執行的容器

[[email protected] ~]$ docker  stop af2
af2
[[email protected] ~]$ docker rm af2
af2

一次刪除所有停止的容器

[[email protected] ~]$ docker rm $(docker ps -a -q)

 

檢視容器內的改變資訊

建立一個容器,會在容器的對應的映象上增加一個可寫層,映象部分是隻讀的。通過 diff命令可以看出改變的資訊。如:

 

複製程式碼
[[email protected] ~]$ docker run -i -t centos /bin/bash
[[email protected] /]# touch demo.ext
[[email protected] /]# echo hello docker >demo.ext 
[[email protected] /]# rm -rf anaconda-post.log 
[[email protected] /]# exit
exit
[[email protected] ~]$ docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
224de7986c5f        centos              "/bin/bash"         40 seconds ago      Exited (0) 3 seconds ago                       desperate_curie     
[[email protected] ~]$ docker diff 224d
D /anaconda-post.log
A /demo.ext
C /root
A /root/.bash_history
複製程式碼

 

說明:每行代表一個變動的檔案或目錄。其中 A 表示新增、C表示被修改、D表示被刪除

 

 

主機和容器之間的檔案拷貝

容器--->主機

複製程式碼
[[email protected] ~]$ docker exec  -t -i 9f bin/bash
[[email protected] /]# cat demo.txt 
hello
[[email protected] /]# exit
exit
[[email protected] ~]$ docker cp 9f:/demo.txt /test [[email protected] ~]$ cat /test/demo.txt hello
複製程式碼

主機-->容器

[[email protected] ~]$  cp ~/1.txt  /var/lib/docker/aufs/mnt/9f49397623ade7dfd2beb4d84454cbdb9878a4b22a2bab2e8b5db72bcffe60a0/test

[[email protected] ~]$ docker exec   -t -i 9f   /bin/bash
[[email protected] /]# ls /test
1.txt

 

重新命名容器

[[email protected] ~]$ docker rename stoic_meitner demo