温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Docker容器之镜像管理,端口映射,容器互联

发布时间:2020-03-06 03:16:48 来源:网络 阅读:258 作者:SiceLc 栏目:云计算

docker镜像的分层

  • Dockerfile 中的每个指令都会创建一个新的镜像层;
  • 镜像层将会被缓存和复用;
  • 当 Dockerfile 的指令修改了,复制的文件变化了,或者构建镜像时指定的变量不同了,对应的镜像层缓存就会失效;
  • 某一层的镜像缓存失效之后,它之后的镜像层缓存都会失效;
  • 镜像层是不变的,如果在某一层中添加一个文件,然后在下一层中删除它,则镜像中依然包含该文件

docker镜像

  • 是应用发布的标准格式
  • 可支撑一个docker容器的运行

docker镜像的创建方法

  • 基于已有镜像创建
  • 基于本地模板创建
  • 基于dockerfile创建

基于已有镜像创建

将容器里面运行的程序及运行环境打包生成新的镜像

docker commit [选项] 容器ID/名称 仓库名称:[标签]
-m:说明信息
-a:作者信息
-p:生成过程中停止容器的运行

基于本地模板创建

  • 通过导入操作系统模板文件生成新的镜像
  • 使用wget命令导入为本地镜像
  • 导入成功后可查看本地镜像信息

基于 Dockerfile 创建

  • Dockerfile 是由一组指令组成的文件
  • Dockerfile 结构的四部分
    • 基础镜像信息
    • 维护者信息
    • 镜像操作指令
    • 容器启动时执行指令
  • 使用 Dockerfile 创建镜像并在容器中运行

dockerfile操作指令

Docker容器之镜像管理,端口映射,容器互联

基于已有镜像创建

[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 <xu>                //维护镜像的用户信息
RUN yum -y update                           //镜像操作指令安装Apache软件
RUN yum -y install httpd                       //安装Apache服务               
EXPOSE 80                                 //开启80端口
ADD index.html /var/www/html/index.html     //复制网址首页文件
ADD run.sh /run.sh                       //将执行脚本复制到镜像中
RUN chmod 755 /run.sh
CMD ["/run.sh"]                           //启动容器时执行脚本
[root@localhost apache]# vim run.sh       //编辑run.sh脚本
#!/bin/bash
rm -rf /run/httpd/*              //清除缓存
exec /usr/sbin/apachectl -D FOREGROUND            //执行apache
[root@localhost apache]# echo "this is test web" > index.html     //创建页面信息
[root@localhost apache]# ls
Dockerfile  index.html  run.sh
[root@localhost apache]# docker build -t httpd:centos .            //执行创建镜像
[root@localhost apache]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               centos              b267aaf2c395        22 seconds ago      401MB
[root@localhost apache]# docker ps -a       //此时没有容器生成
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost apache]# docker run -d -p 1234:80 httpd:centos        //创建映射,创建容器
34c424efdab9e381116de697c4971200b1564b1e38644407cc58d5ba8923a0ea
[root@localhost apache]# docker ps -a         //容器开启,1234是外部端口,80是内部端口
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
34c424efdab9        httpd:centos        "/run.sh"           9 seconds ago       Up 7 seconds        0.0.0.0:1234->80/tcp   great_williamson

Docker容器之镜像管理,端口映射,容器互联

公有仓库与私有仓库

  • 随着创建的镜像日志增多,就需要有一个保存镜像的地方,这就是仓库。目前主要有两种仓库:公共仓库、私有仓库。最方便的就是使用公共仓库上传和下载镜像,下载公共仓库中的镜像不需要注册,但是上传是需要注册的:公共仓库网址

公有仓库

//需要注册docker账号
//将创建好的 httpd:centos 镜像。上传到刚申请的公共仓库中:
docker tag httpd:centos xu/httpd:centos
docker push xu/httpd:centos

私有仓库

[root@localhost ~]# docker pull registry          //下载 registry镜像
[root@localhost ~]# vim /etc/docker/daemon.json
{
    "insecure-registries": ["192.168.13.128:5000"],         //指定仓库地址和端口号
    "registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com"]      //镜像加速
}
[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        httpd:centos        "/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
httpd               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 httpd:centos 192.168.13.128:5000/httpd         //修改标签
[root@localhost ~]# docker push 192.168.13.128:5000/httpd  ##上传镜像
[root@localhost ~]# curl -XGET http://192.168.13.128:5000/v2/_catalog        //获取私有仓库列表
{"repositories":["httpd"]}
[root@localhost ~]# docker pull 192.168.13.128:5000/httpd       //通过私有仓库下载

Docker 网络通信

  • docker 提供了映射容器端口到宿主机和容器互联机制来为容器提供网络服务。

端口映射

  • Docker 提供端口映射机制来将容器内的服务提供给外部网络访问,实质上就是将宿主机的端口映射到容器中,使得外部网络访问宿主机的端口便可访问容器内的服务。
    [root@localhost ~]# docker run -d -P nginx       //随机指定端口
    [root@localhost ~]# docker ps -a                 //查看容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                    NAMES
    bcd11c99804e        nginx               "nginx -g 'daemon of…"   13 seconds ago      Up 13 seconds                 0.0.0.0:32768->80/tcp

    利用浏览器访问32768端口

    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
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI