docker 常用指令

$ docker --help
Commands:
ps            列出容器列表
logs          輸出當前容器日誌信息
port          查看映射端口對應的容器內部源端口
images        列出鏡像
diff          檢查容器文件系統上的更改
events        從服務器獲取實時事件
history       查看一個鏡像歷史
info          顯示系統相關信息
inspect       查看容器詳細信息
top           查看容器中運行的進程信息
Version       查看 docker 版本號
wait          查看容器停止時的退出狀態值
attach        當前 shell 下 attach 連接指定運行鏡像
cp            從容器中拷貝指定文件或者目錄到宿主機中
create        創建一個新的容器,同 run,但不啓動容器
exec          在已存在的容器上運行命令
export        導出容器的內容流作爲一個 tar 歸檔文件
kill          kill 指定 docker 容器
pause         暫停容器
rename        容器重命名
restart       重啓運行的容器
rm            移除一個或者多個容器
run           創建一個新的容器並運行
start         啓動容器
stats         顯示容器的實時流資源使用統計信息
stop          停止容器
tag           給源中鏡像打標籤
unpause       取消暫停容器
Update        更新一個或多個容器的配置
load          從一個 tar 包中加載一個鏡像
pull          從docker鏡像源服務器拉取指定鏡像
push          推送指定鏡像或者庫鏡像至docker源服務器
import        從tar包中的內容創建一個新的文件系統映像
search        在 docker hub 中搜索鏡像
rmi           移除一個或多個鏡像[無容器使用該鏡像纔可刪除,否則需刪除相關容器纔可繼續或 -f 強制刪除]
build         通過 Dockerfile 定製鏡像
commit        提交當前容器爲新的鏡像
save          保存一個鏡像爲一個 tar 包[對應 load]
login         註冊或者登陸一個 docker 源服務器
logout        從當前 Docker registry 退出

例子:

1.查看docker信息

root@db4:~# docker info
Containers: 7
 Running: 2
 Paused: 0
 Stopped: 5
Images: 3
Server Version: 17.03.1-ce
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 44
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
...此處省略

2.查看鏡像和容器列表

root@db4:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
root@db4:~# docker ps
CONTAINER ID        IMAGE                                                  COMMAND                  CREATED             STATUS              PORTS                NAMES

3.運行容器

root@db4:~# docker run -d -p 80:80 registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14
B96812081345e323933cebe0c3e4b624e62ba673ee51647c6aca6c8cca4bb80f
root@db4:~# docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
registry.cn-hangzhou.aliyuncs.com/jonny/nginx   1.9.14              54f500f03701        8 months ago        236 MB
root@db4:~# docker ps
CONTAINER ID        IMAGE                                                  COMMAND                  CREATED             STATUS              PORTS                NAMES
b96812081345        registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14   "/usr/bin/supervisord"   23 seconds ago      Up 22 seconds       0.0.0.0:80->80/tcp   sad_babbage 
# docker run 參數:
-d 後臺運行 
-p 端口映射 
-p=“80:80”
-v 目錄掛載 
-v=“宿主機路徑:容器路徑”
-w 創建工作目錄 
-w=“宿主機路徑”
-h 指定容器的主機名 
-h=“主機名”

訪問:

圖片.png

4.查看容器信息

# 對容器操作有兩個對象: 容器ID和容器名稱。 

# 查看容器所有信息 
docker inspect b8f76748becc   
docker inspect sad_babbage 
# 根據條件查看某字段 
root@db4:~# docker inspect -f '{{.NetworkSettings.Networks.bridge.IPAddress}}'  b8f76748becc
172.17.0.3

5.容器操作:停止、啓動、重啓容器、kill

root@db4:~# docker ps
CONTAINER ID        IMAGE                                                  COMMAND                  CREATED             STATUS              PORTS                NAMES
b96812081345        registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14   "/usr/bin/supervisord"   32 minutes ago      Up 32 minutes       0.0.0.0:80->80/tcp   sad_babbage 
root@db4:~# docker port b96
80/tcp -> 0.0.0.0:80 
root@db4:~# docker stop b96
b96
root@db4:~# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
root@db4:~# docker ps -a 
CONTAINER ID        IMAGE                                                  COMMAND                  CREATED             STATUS                      PORTS               NAMES
b96812081345        registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14   "/usr/bin/supervisord"   32 minutes ago      Exited (0) 9 seconds ago                        sad_babbage 
root@db4:~# docker wait b96
0
root@db4:~# docker start b96
b96
root@db4:~# docker ps 
CONTAINER ID        IMAGE                                                  COMMAND                  CREATED             STATUS              PORTS                NAMES
b96812081345        registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14   "/usr/bin/supervisord"   34 minutes ago      Up 4 seconds        0.0.0.0:80->80/tcp   sad_babbage
root@db4:~# docker restart b96
b96
root@db4:~# docker kill b96
b96 
root@db4:~# docker ps -a
CONTAINER ID        IMAGE                                                  COMMAND                  CREATED             STATUS                        PORTS               NAMES
b96812081345        registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14   "/usr/bin/supervisord"   36 minutes ago      Exited (137) 10 seconds ago                       sad_babbage

6.創建容器,但不運行

root@db4:~# docker create registry.cn-hangzhou.aliyuncs.com/jonny/nginx:v2.0
bc1a0887280df3494313007c512d30319828c377cd03dc3f6799ac5b6deace0a
root@db4:~# docker ps -a
CONTAINER ID        IMAGE                                                  COMMAND                  CREATED             STATUS                         PORTS                            NAMES
bc1a0887280d        registry.cn-hangzhou.aliyuncs.com/jonny/nginx:v2.0     "/usr/bin/supervisord"   21 seconds ago      Created                                                         relaxed_swanson 
b8f76748becc        e8b                                                    "/usr/bin/supervisord"   About an hour ago   Up About an hour               80/tcp, 0.0.0.0:8080->8080/tcp   nginx_8080
b96812081345        registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14   "/usr/bin/supervisord"   2 hours ago         Exited (0) About an hour ago                                    sad_babbage 
root@db4:~#

7.查看容器的日誌

root@db4:~# docker logs  b96
/usr/lib/python2.7/site-packages/supervisor/options.py:296: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
  'Supervisord is running as root and it is searching '
2017-05-24 14:37:08,664 CRIT Supervisor running as root (no user in config file)
2017-05-24 14:37:08,664 WARN Include ed extra file "/etc/supervisor.d/supervisord.ini" during parsing
2017-05-24 14:37:08,675 INFO RPC interface 'supervisor' initialized
2017-05-24 14:37:08,675 CRIT Server 'unix_http_server' running without any HTTP authentication checking
2017-05-24 14:37:08,675 INFO supervisord started with pid 1
2017-05-24 14:37:09,680 INFO spawned: 'nginx' with pid 7
2017-05-24 14:37:09,683 INFO spawned: 'crond' with pid 8
2017-05-24 14:37:10,722 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2017-05-24 14:37:10,722 INFO success: crond entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

# -f 跟蹤日誌類似於linux 命令的tail –f

8.刪除容器

root@db4:~# docker ps -a
CONTAINER ID        IMAGE                                                  COMMAND                  CREATED             STATUS                         PORTS                            NAMES
b8f76748becc        e8b                                                    "/usr/bin/supervisord"   9 minutes ago       Up 9 minutes                   80/tcp, 0.0.0.0:8080->8080/tcp   sharp_pasteur 
b96812081345        registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14   "/usr/bin/supervisord"   55 minutes ago      Up 9 minutes                   0.0.0.0:80->80/tcp               sad_babbage 
865ce2a32e57        registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14   "/usr/bin/supervisord"   56 minutes ago      Created                                                         trusting_heisenberg 
4ffd4bc6cb1c        registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14   "/usr/bin/supervisord"   56 minutes ago      Exited (0) 56 minutes ago                                       suspicious_franklin 
ff44d0a5295a        54f500f03701                                           "/usr/bin/supervisord"   58 minutes ago      Exited (0) 56 minutes ago                                       nervous_noyce 
7cb320d7a6e1        registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14   "/usr/bin/supervisord"   About an hour ago   Exited (0) 58 minutes ago                                       blissful_jepsen 
1f69c23f6450        registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14   "/usr/bin/supervisord"   About an hour ago   Exited (0) About an hour ago                                    eloquent_lichterman 
# 刪除單個容器 (已經停止的容器) 
root@db4:~# docker rm 865ce2a32e57
865ce2a32e57 
# 刪除所有容器 
root@db4:~# docker rm $(docker ps -a -q)
4ffd4bc6cb1c
ff44d0a5295a
7cb320d7a6e1
root@db4:~# docker ps -a
CONTAINER ID        IMAGE                                                  COMMAND                  CREATED             STATUS              PORTS                            NAMES
b8f76748becc        e8b                                                    "/usr/bin/supervisord"   11 minutes ago      Up 11 minutes       80/tcp, 0.0.0.0:8080->8080/tcp   sharp_pasteur 
b96812081345        registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14   "/usr/bin/supervisord"   58 minutes ago      Up 11 minutes       0.0.0.0:80->80/tcp               sad_babbage

9.進入容器

root@db4:~# docker run -d -i -t registry.cn-hangzhou.aliyuncs.com/jonny/python:3 "/bin/sh"
7799fac95d9e13414bdee56013d2521968bf671e3706043f85fa66e343cf431d
root@db4:~# docker ps
CONTAINER ID        IMAGE                                              COMMAND             CREATED             STATUS                  PORTS               NAMES
7799fac95d9e        registry.cn-hangzhou.aliyuncs.com/jonny/python:3   "/bin/sh"           1 second ago        Up Less than a second                       elegant_raman 

root@db4:~# docker attach 779
# 
# ls 
bin  boot  core  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var 
# exit
root@db4:~# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Docker exec 
-d 執行指令 
-i 交互 
-t 終端 
root@db4:~# docker exec -d  b96  touch /root/exec.log
root@db4:~# docker exec b96 ls /root/
exec.log
root@db4:~# docker exec -it b96 sh 
/ # pwd 
/

10.從容器中拷貝文件到本地和從本地拷貝文件到容器

root@db4:~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
b8f76748becc        e8b                 "/usr/bin/supervisord"   About an hour ago   Up About an hour    80/tcp, 0.0.0.0:8080->8080/tcp   sharp_pasteur 
root@db4:~# docker rename b8f nginx_8080
root@db4:~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
b8f76748becc        e8b                 "/usr/bin/supervisord"   About an hour ago   Up About an hour    80/tcp, 0.0.0.0:8080->8080/tcp   nginx_8080

11.容器重命名

root@db4:~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
b8f76748becc        e8b                 "/usr/bin/supervisord"   About an hour ago   Up About an hour    80/tcp, 0.0.0.0:8080->8080/tcp   sharp_pasteur 
root@db4:~# docker rename b8f nginx_8080
root@db4:~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
b8f76748becc        e8b                 "/usr/bin/supervisord"   About an hour ago   Up About an hour    80/tcp, 0.0.0.0:8080->8080/tcp   nginx_8080

12.暫停容器

root@db4:~# docker pause b8f
b8f
暫停着服務不可用。 
root@db4:~# docker ps 
CONTAINER
 ID        IMAGE               COMMAND                  
CREATED             STATUS                      
PORTS                            NAMES
b8f76748becc        e8b                 "/usr/bin/supervisord"   About an hour ago   Up About an hour (Paused)   80/tcp, 0.0.0.0:8080->8080/tcp   sharp_pasteur 
root@db4:~# docker unpause b8f
b8f
root@db4:~# docker ps 
CONTAINER
 ID        IMAGE               COMMAND                  
CREATED             STATUS              PORTS                           
 NAMES
b8f76748becc        e8b                 "/usr/bin/supervisord"   About an hour ago   Up About an hour    80/tcp, 0.0.0.0:8080->8080/tcp   sharp_pasteur
13.查看容器進程
root@db4:~# docker top b96
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                45233               45216               0                   17:13               ?                   00:00:00            /usr/bin/python /usr/bin/supervisord 
root                45257               45233               0                   17:13               ?                   00:00:00            nginx: master process /usr/local/nginx/sbin/nginx -g daemon off;
root                45258               45233               0                   17:13               ?                   00:00:00            /usr/sbin/crond -f -L /usr/local/nginx/logs/crond.log
nobody              45259               45257               0                   17:13               ?                   00:00:00            nginx: worker process

13.查看容器的實時資源使用情況

root@db4:~# docker stats b8f
CONTAINER           CPU %               MEM USAGE / LIMIT       MEM %               NET I/O             BLOCK I/O           PIDS
b8f                 0.19%               3.602 MiB / 974.9 MiB   0.37%               3.27 kB / 3.2 kB    1.62 MB / 8.19 kB   4

14.查看docker 事件

# 事先執行docker events   
root@db4:~# docker events  
# 在另外一個終端對容器進行操作 
root@db4:~# docker stop b96
b96
root@db4:~# docker restart b96
b96
root@db4:~# docker events 

2017-05-24T17:41:26.703418212+08:00 container kill b96812081345e323933cebe0c3e4b624e62ba673ee51647c6aca6c8cca4bb80f (image=registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14, name=sad_babbage, signal=15)
2017-05-24T17:41:26.754823179+08:00 container die b96812081345e323933cebe0c3e4b624e62ba673ee51647c6aca6c8cca4bb80f (exitCode=0, image=registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14, name=sad_babbage)
2017-05-24T17:41:26.830372248+08:00 network disconnect 074ed6cf0e44bf95ade6f4f8b3c18781fc9fd65eab8481e064e2669a72e14e29 (container=b96812081345e323933cebe0c3e4b624e62ba673ee51647c6aca6c8cca4bb80f, name=bridge, type=bridge)
2017-05-24T17:41:26.868053643+08:00 container stop b96812081345e323933cebe0c3e4b624e62ba673ee51647c6aca6c8cca4bb80f (image=registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14, name=sad_babbage)
2017-05-24T17:41:33.871581991+08:00 network connect 074ed6cf0e44bf95ade6f4f8b3c18781fc9fd65eab8481e064e2669a72e14e29 (container=b96812081345e323933cebe0c3e4b624e62ba673ee51647c6aca6c8cca4bb80f, name=bridge, type=bridge)
2017-05-24T17:41:34.188260322+08:00 container start b96812081345e323933cebe0c3e4b624e62ba673ee51647c6aca6c8cca4bb80f (image=registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14, name=sad_babbage)
2017-05-24T17:41:34.188318535+08:00 container restart b96812081345e323933cebe0c3e4b624e62ba673ee51647c6aca6c8cca4bb80f (image=registry.cn-hangzhou.aliyuncs.com/jonny/nginx:1.9.14, name=sad_babbage)

15.查看容器文件的增、刪、改

root@db4:~# docker exec -it b96 sh  
/ # touch /tmp/diff.log
/ # exit
root@db4:~# docker diff b96
C /root
A /root/.ash_history 
A /root/.bash_history 
A /root/.viminfo 
C /run
A /run/supervisord.sock 
A /supervisord.pid 
C /tmp 
A /tmp/crond-stderr---supervisor-lJb_zr.log
A /tmp/crond-stdout---supervisor-K8nnKW.log
A /tmp/diff.log
…
 
Symbol  Description
A       A file or directory was added
D       A file or directory was deleted
C       A file or directory was changed

16.拉取鏡像

Docker官網鏡像地址:https://hub.docker.com/explore/
阿里雲鏡像地址:https://cs.console.aliyun.com/#/repo 
root@db4:~# docker pull registry.cn-hangzhou.aliyuncs.com/jonny/mysql:5.6
5.6: Pulling from jonny/mysql 
8b87079b7a06: Pull complete 
a3ed95caeb02: Pull complete 
…
Digest: sha256:f74beade896c31cf9725eb567f813d18a3e8cffdab252eca8c6ce6c4337d1443
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/jonny/mysql:5.6

17.搜索鏡像

root@db4:~# docker search nginx 
NAME                                     DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                                    Official build of Nginx.                        6058      [OK]       
jwilder/nginx-proxy                      Automated Nginx reverse proxy for docker c...   1032                 [OK]
richarvey/nginx-php-fpm                  Container running Nginx + PHP-FPM capable ...   379                  [OK]
jrcs/letsencrypt-nginx-proxy-companion   LetsEncrypt container to use with nginx as...   181                  [OK]
…

18.提交新鏡像

root@db4:~# docker exec -it b96 sh

/ # cd /usr/local/nginx/conf/

/usr/local/nginx/conf # grep 8080 nginx.conf

        listen       8080;

root@db4:~# docker commit b96 nginx:v2.0

sha256:e8b6bdca34fdd6e859ac794cefd14890c1a6a3d7514f10f9b85884fc4c782888

root@db4:~# docker run -d -p 8080:8080 e8b

b8f76748becc122df22d887f5eb7d503dd837126937f450d3c05a47641eb587f

訪問:

圖片.png

19.推送鏡像阿里雲倉庫

root@db4:~# docker login [email protected] registry.cn-hangzhou.aliyuncs.com
Password: 
Login Succeeded
root@db4:~# docker images
REPOSITORY                                       TAG                 IMAGE ID            CREATED             SIZE
nginx                                            latest              aaf9cb9f4204        11 minutes ago      236 MB
nginx                                            v2.0                e8b6bdca34fd        About an hour ago   236 MB
registry.cn-hangzhou.aliyuncs.com/jonny/nginx    1.9.14              54f500f03701        8 months ago        236 MB
registry.cn-hangzhou.aliyuncs.com/jonny/python   3                   7918f1fa1b61        10 months ago       686 MB
root@db4:~# docker tag nginx:v2.0 registry.cn-hangzhou.aliyuncs.com/jonny/nginx:v2.0  
root@db4:~# docker push registry.cn-hangzhou.aliyuncs.com/jonny/nginx:v2.0 
The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/jonny/nginx]
ee1ec61f5876: Pushed 
e4c220924fcf: Layer already exists 
880a03e21e97: Layer already exists 
8c2d24778f16: Layer already exists 
58d1bd0fe6cd: Layer already exists 
f3e31263e373: Layer already exists 
43d68a309658: Layer already exists 
4aa9a6e6b176: Layer already exists 
fb4d779f7310: Layer already exists 
5507160a5e67: Layer already exists 
251680350a8e: Layer already exists 
44e66153e719: Layer already exists 
3fc666989c1d: Layer already exists 
v2.0: digest: sha256:17fcbb1ba3cebee0146beb56193c84565d28b3eac5d9c92d9d51efff52a837db size: 3046
root@db4:~# docker logout registry.cn-hangzhou.aliyuncs.com
Removing login credentials for registry.cn-hangzhou.aliyuncs.com

查看:

圖片.png

20.刪除鏡像

root@db4:~# docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
nginx                                           v2.0                e8b6bdca34fd        14 minutes ago      236 MB
registry.cn-hangzhou.aliyuncs.com/jonny/nginx   1.9.14              54f500f03701        8 months ago        236 MB
registry.cn-hangzhou.aliyuncs.com/jonny/mysql   5.6                 73e25e0ad5a6        12 months ago       325 MB
root@db4:~# docker rmi  73e25e0ad5a6
Untagged: registry.cn-hangzhou.aliyuncs.com/jonny/mysql:5.6
Untagged: registry.cn-hangzhou.aliyuncs.com/jonny/mysql@sha256:f74beade896c31cf9725eb567f813d18a3e8cffdab252eca8c6ce6c4337d1443
Deleted: sha256:73e25e0ad5a687089694c06268cf13d4479cfeafe35e34bdd6aebc5419b5fc10
Deleted: sha256:6523d35a3424f7608e39c6aa4665a7524bccf39f56d5ade4644541dc6521d917
Deleted: sha256:cc21027dfe1dd9e4ea85ef9b2cfa19d68f533bcacf2929b0180fe8ad719d75f0
Deleted: sha256:bff752fe4a4634c1d5b0e9ea864aa02120955c44fc916e1c064bf690b8e1b417
Deleted: sha256:3fe3543590e576687bee1ba4ab19f861a5ebd02b0500447e398cd5e6a9c93d0e
Deleted: sha256:26c54fc486bc69cf7ce868d0150aee14d9a9b2c2dac49e353329ef4b313d0974
Deleted: sha256:0fdf917ab45866188e063827bcabe458e0f34a4b605ff14b3e166bc4258e2693
Deleted: sha256:4bdcc159819904f7f87657bb38afa79c99467a2ed84f228acd8c65b17f0deeb6
Deleted: sha256:fddaf298f07ba02349d9450c79f142e5b1c5ef291363e1a7a30a2074fc7f7122
Deleted: sha256:1f0d2eab171a3acee8114f2625538f5b7ff06f1a4d6d981a51759e5e2c51ce91
Deleted: sha256:111f35b1ed9c5131cdfa0a7919bc31e946eafbcefc3b15add943a40a1a76ae15
Deleted: sha256:373d98e5699867dd4419c9cc8935e235c7db8c61497493e87faee459088f1f3c
Deleted: sha256:1290b53396d9d123f46d81430b0f4ca1e5163f117be22a1e48c72a252e8980b7
Deleted: sha256:e6973fa144250a55057571ed5b0c5fa815fb136ba54ade10e01633aa398cdacb
Deleted: sha256:ef9f86c3f5e7cad05a0a558a1ad1a601b3b5511e21b50e70b517e13a0f62938f
Deleted: sha256:f5e12e98d7fb314c46673c0f4a14326eeca08a9dc7f9ea82e10e39d85c74900a
Deleted: sha256:f7cf774ed4094c199f554927b7b01690920b834f118e702ff2643560549f27fc
Deleted: sha256:6eb35183d3b8bb6aee54874076fb1ad77b5259c93b330986b0cbcaa44cbbbc00
root@db4:~# docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
nginx                                           v2.0                e8b6bdca34fd        16 minutes ago      236 MB
registry.cn-hangzhou.aliyuncs.com/jonny/nginx   1.9.14              54f500f03701        8 months ago        236 MB

21.保存鏡像和載入鏡像

# 將一個鏡像保存爲一個鏡像文件。 
root@db4:~# docker save -o nginx_v2.0.tar nginx:v2.0 
root@db4:~# ls 
nginx_v2.0.tar
# 將一個鏡像文件載入爲鏡像。 
root@db4:~# docker load --input nginx_v2.0.tar
或 
root@db4:~# docker load < nginx_v2.0.tar

22.導出容器和導入鏡像

將一個正在運行或者停止的容器導出爲一個容器文件 
root@db4:~#  docker export b96 > nginx.tar
root@db4:~# ls 
nginx.tar
將一個容器文件導入到本地鏡像倉庫 
root@db4:~# docker import nginx.tar nginx 
sha256:aaf9cb9f42047c24be3178c9205323175c0b872b3a695314e22ce1d432caa501
root@db4:~# docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
nginx                                           latest              aaf9cb9f4204        8 seconds ago       236 MB
nginx                                           v2.0                e8b6bdca34fd        About an hour ago   236 MB
registry.cn-hangzhou.aliyuncs.com/jonny/nginx   1.9.14              54f500f03701        8 months ago        236 MB

23.構建鏡像

root@db4:~# touch Dockerfile 
root@db4:~# vim Dockerfile 
root@db4:~# cat Dockerfile 
FROM registry.cn-hangzhou.aliyuncs.com/jonny/python:3
root@db4:~# docker build .
Sending build context to Docker daemon 490.1 MB
Step 1/1 : FROM registry.cn-hangzhou.aliyuncs.com/jonny/python:3
3: Pulling from jonny/python
5c90d4a2d1a8: Pull complete 
a3ed95caeb02: Pull complete 
7aacfb932892: Pull complete 
29b0204e3e44: Pull complete 
f7b3449113d2: Pull complete 
4696148a3d89: Pull complete 
a65fc447ceea: Pull complete 
eebbb7c52ba8: Pull complete 
Digest: sha256:e047386633dad2226fbdb5d01f138cf648a6245ef1250e9d1a34e7c95454fda2
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/jonny/python:3
 ---> 7918f1fa1b61
Successfully built 7918f1fa1b61
root@db4:~# docker images
REPOSITORY                                       TAG                 IMAGE ID            CREATED             SIZE
nginx                                            latest              aaf9cb9f4204        7 minutes ago       236 MB
nginx                                            v2.0                e8b6bdca34fd        About an hour ago   236 MB
registry.cn-hangzhou.aliyuncs.com/jonny/nginx    1.9.14              54f500f03701        8 months ago        236 MB
registry.cn-hangzhou.aliyuncs.com/jonny/python   3                   7918f1fa1b61        10 months ago       686 MB

24.查看鏡像的歷史

root@db4:~# docker history nginx:v2.0
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
e8b6bdca34fd        2 hours ago         /usr/bin/supervisord                            6.69 kB             
54f500f03701        8 months ago        /bin/sh -c #(nop) CMD ["/usr/bin/superviso...   0 B                 
<missing>           8 months ago        /bin/sh -c echo "55       23       *      ...   85 B                
<missing>           8 months ago        /bin/sh -c apk add logrotate supervisor vi...   68.5 MB             
<missing>           8 months ago        /bin/sh -c tar -xzvf nginx-${NGINX_VERSION...   35.6 MB             
<missing>           8 months ago        /bin/sh -c git clone https://github.com/ya...   1.06 MB             
<missing>           8 months ago        /bin/sh -c git clone https://github.com/ap...   285 kB              
<missing>           8 months ago        /bin/sh -c git clone https://github.com/ya...   865 kB              
<missing>           8 months ago        /bin/sh -c git clone https://github.com/cu...   164 kB              
<missing>           8 months ago        /bin/sh -c wget -c http://nginx.org/downlo...   908 kB              
<missing>           8 months ago        /bin/sh -c mkdir -p /var/run/nginx/             0 B                 
<missing>           8 months ago        /bin/sh -c #(nop) ENV NGINX_VERSION=1.9.14      0 B                 
<missing>           8 months ago        /bin/sh -c apk add --no-cache --virtual .b...   123 MB              
<missing>           8 months ago        /bin/sh -c wget http://sh.xiayu.site/mirro...   674 kB              
<missing>           8 months ago        /bin/sh -c #(nop) ADD file:fd71807f3b22f7f...   4.8 MB


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章