docker镜像管理基础

docker是属于联合挂载,最上层的为可写成,下面的镜像都为只读挂载的,这个功能建立在特殊的文件系统上,docker info可以看到

 Storage Driver: overlay2
  Backing Filesystem: xfs

overlay2这个是一个特殊的文件系统,用来支持联合挂载的功能,但是这个文件系统是建立在xfs的底层文件系统之上的,不同的系统这里会不一样

docker容器官方镜像地址

hub.docker.com

镜像的制作方式

dockerfile

基于容器制作

先启动一个容器,在容器中做好你要做的修改,然后将这个做好修改的可写层打包作为一个镜像,比如:在一个cnetos的基础镜像上,安装一个nginx,将这个可写层使用docker commit命令打包为一个镜像

示例:

基于容器制作

[root@localhost ~]# docker run --name b1 -i -t  busybox 
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # mkdir -p /data/html
/ # vi /data/html/index.html

vi /data/html/index.html

<h1>Busybox httpd server.</h1>

基于容器制作镜像时,容器应该处于运行状态,因为他是基于容器的可写层做的镜像

在起一个终端执行docker commit来基于b1容器的可写层制作镜像-p表示制作时容器为停止状态,预防产生新的数据,造成数据不完整。

[root@localhost ~]# docker commit -p b1
sha256:df073d955fb3f67248c5d95d11ee55fe88b3ffc9b3e742ab4b33eed4a38f90a8
[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              df073d955fb3        20 seconds ago      1.22MB
busybox             latest              19485c79a9bb        4 days ago          1.22MB
nginx               1.14-alpine         8a2fb25a19f5        5 months ago        16MB

为了引用起来方便使用docker tag加上标签。

[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              df073d955fb3        20 seconds ago      1.22MB
busybox             latest              19485c79a9bb        4 days ago          1.22MB
nginx               1.14-alpine         8a2fb25a19f5        5 months ago        16MB
[root@localhost ~]# docker tag df073d955fb3 haoran/httpd:v0.1-1
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
haoran/httpd        v0.1-1              df073d955fb3        29 minutes ago      1.22MB
busybox             latest              19485c79a9bb        4 days ago          1.22MB
nginx               1.14-alpine         8a2fb25a19f5        5 months ago        16MB
[root@localhost ~]# docker tag haoran/httpd:v0.1-1 haoran/httpd:latest
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
haoran/httpd        latest              df073d955fb3        35 minutes ago      1.22MB
haoran/httpd        v0.1-1              df073d955fb3        35 minutes ago      1.22MB
busybox             latest              19485c79a9bb        4 days ago          1.22MB
nginx               1.14-alpine         8a2fb25a19f5        5 months ago        16MB

tag是专门的标签管理命令,一个镜像可以有多个标签,id号是同一个,如果没有标签需要引用镜像id号来打标签,docker hub的haoran/httpd仓库,v0.1-1的标签。

[root@localhost ~]# docker run --name t1 -i -t haoran/httpd:v0.1-1 
/ # ls
bin   data  dev   etc   home  proc  root  sys   tmp   usr   var
/ # cat /data/html/index.html 
<h1>Busybox httpd server.</h1>

进入容器后默认启动的命令是/bin/sh,现在要求只要一运行就执行httpd服务,不在运行/bin/sh,做镜像时修改默认程序

[root@localhost ~]# docker commit -a 'haoran <haoran@keji.com>' -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' -p b1 haoran/httpd:v0.2
sha256:037337399ca851d8721987dc0152596af82937b07da69aed82a09979fe631f54
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
haoran/httpd        v0.2                037337399ca8        5 minutes ago       1.22MB
[root@localhost ~]# docker run --name t2 haoran/httpd:v0.2

-a表示作者,-c表示将Dockerfile指令应用于创建的映像 ,-p表示在提交期间(创建镜像时)暂停容器(默认为true)

t2这个容器现在运行的只是httpd程序,并且在前台,什么信息都没有输出,在另外一个终端上查看这个容器的ip并访问

[root@localhost ~]# docker inspect t2
   "Cmd": [
                "/bin/httpd",
                "-f",
                "-h",
                "/data/html"
            ],
          "IPAddress": "172.17.0.3",
[root@localhost ~]# curl 172.17.0.3
<h1>Busybox httpd server.</h1>

把镜像推进仓库

先login登陆进仓库默认是docker hub

[root@localhost ~]# docker login -u dockerhubname
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@localhost ~]# 

在push推镜像

[root@localhost ~]# docker push dockerhubname/httpd
The push refers to repository [docker.io/dockerhaoran/httpd]
b99a5f8c3472: Pushed 
6c0ea40aef9d: Pushed 
latest: digest: sha256:b9ea9000c75809ac790f40ab7ff9bb0879c2aea3e8a853b0a8bd1e04daeccc58 size: 734

注意标签和仓库名要一样!!!

阿里云推镜像在网站内会有详情

原文地址:https://www.cnblogs.com/hao-ran/p/11492602.html