03docker镜像

docker的镜像操作

Union文件系统是Docker镜像的基础。
UnionFS(联合文件系统):Union文件系统是一种分层,轻量级并且高性能的文件系统。它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同的目录挂载到同一个虚拟文件系统下。
最上层是可写层。

1:下载镜像:格式 docker [image] pull [仓库地址]/NAME:TAG

ubuntu@ubuntu:~$ docker pull registry.hub.docker.com/ubuntu:18.04      #下载ubuntu镜像,标签为18.04
ubuntu@ubuntu:~$ docker pull centos:7.2.1511   #下载一个centos镜像

2:查看本地仓库的镜像

ubuntu@ubuntu:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              18.04               775349758637        12 days ago         64.2MB
ubuntu              latest              775349758637        12 days ago         64.2MB  
centos              7.2.1511            9aec5c5fe4ba        8 months ago        195MB
hello-world         latest              fce289e99eb9        10 months ago       1.84kB
#latest标签的版本是不稳定的,会随着版本更新而变化,建议使用稳定带标签的版本
#查看子命令:man docker-images

3:使用镜像创建一个容器

ubuntu@ubuntu:~$ docker run  -ti 775349758637  /bin/bash
root@e6cce6710d88:/# 

ubuntu@ubuntu:~$ docker run  -it ubuntu:18.04 bash
root@fc41542c62b5:/# 

4;使用tag命令为本地镜像添加别名

ubuntu@ubuntu:~$ docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              18.04               775349758637        13 days ago         64.2MB
ubuntu              latest              775349758637        13 days ago         64.2MB
centos              7.2.1511            9aec5c5fe4ba        8 months ago        195MB
hello-world         latest              fce289e99eb9        10 months ago       1.84kB
ubuntu@ubuntu:~$ docker tag ubuntu:18.04 myubuntu:18.04  #   添加别名
ubuntu@ubuntu:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myubuntu            18.04               775349758637        13 days ago         64.2MB
ubuntu              18.04               775349758637        13 days ago         64.2MB
ubuntu              latest              775349758637        13 days ago         64.2MB
centos              7.2.1511            9aec5c5fe4ba        8 months ago        195MB
hello-world         latest              fce289e99eb9        10 months ago       1.84kB

#别名IMAGE ID是一样的,指向同一个镜像

5:使用inspect命令查看详细信息docker [image] inspect 镜像

ubuntu@ubuntu:~$ docker inspect ubuntu:18.04
[
    {
        "Id": "sha256:775349758637aff77bf85e2ff0597e86e3e859183ef0baba8b3e8fc8d3cba51c",
        "RepoTags": [
            "myubuntu:18.04",
            "ubuntu:18.04",
            "ubuntu:latest"
        ],
        "RepoDigests": [
            "ubuntu@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2019-10-31T22:20:37.714423666Z",
        "Container": "4df7a03525342ee760076ade1c80bbdd041f236654f624ca581ada54310a1574",
        "ContainerConfig": {
            "Hostname": "4df7a0352534",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
...
-f:查看某一个特定字段的内容
ubuntu@ubuntu:~$ docker inspect -f {{".Id"}} ubuntu:18.04
sha256:775349758637aff77bf85e2ff0597e86e3e859183ef0baba8b3e8fc8d3cba51c
ubuntu@ubuntu:~$ docker inspect  -f='{{ .NetworkSettings.Networks.bridge.IPAddress }}'  92c
172.17.0.2
ubuntu@ubuntu:~$ docker inspect  --format {{".NetworkSettings.Networks.bridge.IPAddress"}}  92c      #不要有空格
172.17.0.2
#注意格式-f{{.xxxx}}


6:使用history查看镜像历史

ubuntu@ubuntu:~$ docker history --no-trunc  ubuntu:18.04	

7:搜寻镜像

查看帮助文档 man docker-search
ubuntu@ubuntu:~$ docker search --filter=stars=3 nginx

8.删除镜像docker rmi IMAGe[IMAGE...]

1:删除自定义标签myubuntu镜像
ubuntu@ubuntu:~$ docker rmi myubuntu:18.04  
Untagged: myubuntu:18.04
#同一镜像拥有多个标签,只删除指定的标签

2:查看本机所有存在的容器	
ubuntu@ubuntu:~$ docker ps ubuntu@ubuntu:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
fc41542c62b5        775349758637        "bash"              3 hours ago         Up 3 hours                              sleepy_roentgen

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
fc41542c62b5        775349758637        "bash"              3 hours ago         Up 3 hours                              sleepy_roentgen


3:无法删除正在运行的镜像
ubuntu@ubuntu:~$ docker rmi ubuntu:latest
Error response from daemon: conflict: unable to remove repository reference "ubuntu:latest" (must force) - container fc41542c62b5 is using its referenced image 775349758637

4:-f参数强制删除正在运行的容器(不推荐使用)
ubuntu@ubuntu:~$ docker rmi -f  ubuntu:latest 
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d

9:清理docker运行产生的临时镜像文件

ubuntu@ubuntu:~$ docker image prune -f    #强制删除遗留的临时镜像文件层
Total reclaimed space: 0B
# -a:删除所有无用的镜像

10:创建镜像

方法1:基于已有容器创建

格式:docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

-a :作者信息

-c:提交时执行的Dockerfile文件,包括CMD|ENV|LABEL等等

-m:提交的信息,评论解释

-P:提交时候暂停容器运行

案例:

1:启动一个镜像,并在里面创建一个文件
ubuntu@ubuntu:~$ docker run -it ubuntu:18.04 /bin/bash
root@f20664a41969:/# pwd   
/
root@f20664a41969:/# touch test.txt
root@f20664a41969:/# exit
exit

#记住容器ID:f20664a41969

2:提交一个新的镜像
ubuntu@ubuntu:~$ docker  commit -m "Add a new file test.txt" -a "author_007" f20664a41969 test:0.1
sha256:55f6618d9cdf8c56a1d80b0f0926341da231613a4e7646f47b0b7ab47c5eb21f   #新的镜像id


3:查看当前镜像
ubuntu@ubuntu:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
test                0.1                 55f6618d9cdf        About a minute ago   64.2MB
ubuntu              18.04               775349758637        13 days ago          64.2MB
centos              7.2.1511            9aec5c5fe4ba        8 months ago         195MB
hello-world         latest              fce289e99eb9        10 months ago        1.84kB

4:运行镜像test:0.1查看是否有test文件
ubuntu@ubuntu:~$ docker run -it test:0.1 /bin/bash
root@acfd9348cffe:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  test.txt  usr
boot  etc  lib   media  opt  root  sbin  sys  tmp       var


方法2:基于本地模板导入

方法3:基于Dockerfile创建

11:存出和载入镜像

1:导出镜像到本地文件
ubuntu@ubuntu:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                0.1                 55f6618d9cdf        10 minutes ago      64.2MB
ubuntu              18.04               775349758637        13 days ago         64.2MB
centos              7.2.1511            9aec5c5fe4ba        8 months ago        195MB
hello-world         latest              fce289e99eb9        10 months ago       1.84kB
ubuntu@ubuntu:~$ docker save -o  ubuntu_18.04.tar   ubuntu:18.04  #导出镜像
ubuntu@ubuntu:~$ ls
apt-transport-https  linux-image-extra-5.3.0-19-generic  snap
ca-certificates      Music                               Templates
curl                 new_ubuntu.txt                      ubuntu_18.04.tar

2:将导出的tar文件再导入本地镜像库

ubuntu@ubuntu:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7.2.1511            9aec5c5fe4ba        8 months ago        195MB

ubuntu@ubuntu:~$ docker load -i ubuntu_18.04.tar   #导入镜像
Loaded image: ubuntu:18.04
ubuntu@ubuntu:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              18.04               775349758637        13 days ago         64.2MB
centos              7.2.1511            9aec5c5fe4ba        8 months ago        195MB

posted @ 2019-11-15 21:55  Noob52037  阅读(172)  评论(0编辑  收藏  举报