linux怎么查看本机内存大小
233
2022-10-30
Docker容器之镜像管理,端口映射,容器互联
docker镜像的分层
Dockerfile 中的每个指令都会创建一个新的镜像层; 镜像层将会被缓存和复用; 当 Dockerfile 的指令修改了,复制的文件变化了,或者构建镜像时指定的变量不同了,对应的镜像层缓存就会失效; 某一层的镜像缓存失效之后,它之后的镜像层缓存都会失效; 镜像层是不变的,如果在某一层中添加一个文件,然后在下一层中删除它,则镜像中依然包含该文件
docker镜像
是应用发布的标准格式 可支撑一个docker容器的运行
docker镜像的创建方法
基于已有镜像创建 基于本地模板创建 基于dockerfile创建
基于已有镜像创建
将容器里面运行的程序及运行环境打包生成新的镜像
docker commit [选项] 容器ID/名称 仓库名称:[标签] -m:说明信息 -a:作者信息 -p:生成过程中停止容器的运行
基于本地模板创建
通过导入操作系统模板文件生成新的镜像 使用wget命令导入为本地镜像 导入成功后可查看本地镜像信息
基于 Dockerfile 创建
Dockerfile 是由一组指令组成的文件 Dockerfile 结构的四部分 基础镜像信息 维护者信息 镜像操作指令 容器启动时执行指令 使用 Dockerfile 创建镜像并在容器中运行
dockerfile操作指令
基于已有镜像创建
[root@localhost ~]# docker pull centos //下载镜像 [root@localhost ~]# docker create -it centos /bin/bash //基于centos镜像创建容器 30d395e63fc32b9dcf96029869f40a8002990f689410cca2660af4056ed2614f [root@localhost ~]# docker ps -a //查看容器信息 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 30d395e63fc3 centos "/bin/bash" 7 seconds ago Created inspiring_germain [root@localhost ~]# docker commit -m "new" -a "daoke" 30d395e63fc3 daoke:centos //将容器里面运行的程序及运行环境打包生成新的镜像 sha256:66d76f9225b94ce6156db953bd16c384f74067f981d45bee99340f3a965506d3 [root@localhost ~]# docker images //查看镜像 REPOSITORY TAG IMAGE ID CREATED SIZE daoke centos 66d76f9225b9 10 seconds ago 220MB centos latest 0f3e07c0138f 3 months ago 220MB
基于本地模板创建
[root@localhost ~]# mount.cifs //192.168.100.3/LNMP-C7 /mnt/ //将本地模板挂载到Linux上 Password for root@//192.168.100.3/LNMP-C7: [root@localhost ~]# cd /mnt //切换目录到/mnt [root@localhost docker]# ls debian-7.0-x86-minimal.tar.gz [root@localhost mnt]# cat debian-7.0-x86-minimal.tar.gz | docker import - daoke:new //基于本地模板创建一个镜像 sha256:487145d2411f0440c50fd93d0e8a9e27610d2de745a25d06955f21c80e65753a [root@localhost mnt]# docker images //查看镜像 REPOSITORY TAG IMAGE ID CREATED SIZE daoke new 487145d2411f 8 seconds ago 215MB centos latest 0f3e07c0138f 3 months ago 220MB
基于dockefile文件创建
[root@localhost ~]# mkdir apache //创建一个目录
[root@localhost ~]# cd apache/
[root@localhost apache]# vim Dockerfile //编写一个dockerfile文件
FROM centos //基于的基础镜像
MAINTAINER The porject
公有仓库与私有仓库
随着创建的镜像日志增多,就需要有一个保存镜像的地方,这就是仓库。目前主要有两种仓库:公共仓库、私有仓库。最方便的就是使用公共仓库上传和下载镜像,下载公共仓库中的镜像不需要注册,但是上传是需要注册的:公共仓库网址
公有仓库
//需要注册docker账号 //将创建好的 镜像。上传到刚申请的公共仓库中: docker tag xu/httpd:centos docker push xu/~]# docker pull registry //下载 registry镜像 [root@localhost ~]# vim /etc/docker/daemon.json { "insecure-registries": ["192.168.13.128:5000"], //指定仓库地址和端口号 "registry-mirrors": [" //镜像加速 } [root@localhost ~]# systemctl stop docker //停止docker,开启docker [root@localhost ~]# systemctl start docker [root@localhost ~]# docker create -it registry /bin/bash //创建registry镜像容器 209dadd90f5c555ba328fae5763a61ae5fe4489acc4bfb945a99bb2307a9f139 [root@localhost ~]# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 209dadd90f5c registry "/entrypoint.sh /bin…" 4 seconds ago Created admiring_dewdney 34c424efdab9 "/run.sh" 13 minutes ago Exited (137) 35 seconds ago great_williamson [root@localhost ~]# docker start 209dadd90f5c //开启容器 209dadd90f5c [root@localhost ~]# docker run -d -p 5000:5000 -v /data/registry:/tmp/registry registry //创建映射端口和数据卷,宿主局的/data自动挂载容器重点的/tmp fd4185499dfa29f1a1133f59b706a5524572ae3f22140137214ab4c8212ea8a4 [root@localhost ~]# docker images //查看一下当前的镜像 REPOSITORY TAG IMAGE ID CREATED SIZE centos b267aaf2c395 17 minutes ago 401MB centos latest 0f3e07c0138f 3 months ago 220MB registry latest f32a97de94e1 10 months ago 25.8MB [root@localhost ~]# docker tag 192.168.13.128:5000/ //修改标签 [root@localhost ~]# docker push 192.168.13.128:5000/ ##上传镜像 [root@localhost ~]# curl -XGET //获取私有仓库列表 {"repositories":["httpd"]} [root@localhost ~]# docker pull 192.168.13.128:5000/ //通过私有仓库下载
Docker 网络通信
docker 提供了映射容器端口到宿主机和容器互联机制来为容器提供网络服务。
端口映射
容器互联(使用centos镜像)
[root@localhost ~]# docker run -itd -P --name web1 centos /bin/bash //创建web1容器 87c58af3100fbc112bf344a421942dd53451c0c663b697a55a8d410868f314bf [root@localhost ~]# docker run -itd -P --name web2 --link web1:web1 centos /bin/bash //创建web2连接web1容器 7a84075802b5689912c323196b5af398fb5912316efda014921c0e23d3e9cdd2 [root@localhost ~]# docker ps -a //查看容器信息 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7a84075802b5 centos "/bin/bash" 6 seconds ago Up 5 seconds web2 87c58af3100f centos "/bin/bash" 42 seconds ago Up 41 seconds web1 [root@localhost ~]# docker exec -it 7a84075802b5 /bin/bash //进入web2容器 [root@7a84075802b5 /]# ping web1 //pingweb1看是否互联互通 PING web1 (172.17.0.5) 56(84) bytes of data. 64 bytes from web1 (172.17.0.5): icmp_seq=1 ttl=64 time=0.090 ms 64 bytes from web1 (172.17.0.5): icmp_seq=2 ttl=64 time=0.089 ms
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~