设为首页 收藏本站
查看: 1078|回复: 0

[经验分享] docker 基本操作Ⅱ(关于镜像操作)

[复制链接]

尚未签到

发表于 2018-5-27 06:40:02 | 显示全部楼层 |阅读模式
  1 通过模板创建镜像
  - 导入镜像基本操作
https://openvz.org/Download/template/precreated 在这个网址里面下载对应的模板
[root@chy src]# wget http://download.openvz.org/template/precreated/centos-7-x86_64-minimal.tar.gz //下载centos7的模板
[root@chy src]# cat centos-7-x86_64-minimal.tar.gz |docker import - centos7 //导入镜像
sha256:3e6c83d2f3749ae4cde27673354cab305bfdc360e40d10d072f6a7dfd3edcdca
[root@chy src]# docker images //查看已经导入的镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos7             latest              3e6c83d2f374        2 minutes ago       435MB
centos_with_net     latest              87dde0ff7187        21 hours ago        277MB
centos              latest              d123f4e55e12        10 days ago         197MB
[root@chy src]# docker run -itd centos7 bash//将镜像启动centos7的容器
67c68340c58643e4e085dc04fe7552bad4ff6c41c612c26e9ce99e63b4872317
[root@chy src]# docker exec -it 67c68340c5 bash //进入到centos7的容器里
[root@67c68340c586 /]# cat /etc/issue //查看版本
[root@67c68340c586 /]# uname -a //查看内核(这个内核与宿主机的内核是一致的,如果我的镜像的模板是centos6的,我的宿主机是centos7的那我最后在docker镜像里查看的内核也就是centos7的。
Linux 67c68340c586 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux  - 导出/恢复镜像基本操作
[root@chy ~]# docker save -o centos7-daochu.tar centos7// -o 后面跟的是centos7-daochu(导出后的文件名)centos7这个是要导出的镜像名
[root@chy ~]# docker rm -f 67c68340c586//删除运行的容器
67c68340c586
[root@chy ~]# docker rmi 3e6c83d2f374 删除镜像
Untagged: centos7:latest
Deleted: sha256:3e6c83d2f3749ae4cde27673354cab305bfdc360e40d10d072f6a7dfd3edcdca
Deleted: sha256:788edba9eaa8ade63d8ba9d5747281c5da2b34b12a6c80f4dffd8ad9e05f68c1
[root@chy ~]# docker images //查看没有镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos_with_net     latest              87dde0ff7187        22 hours ago        277MB
centos              latest              d123f4e55e12        10 days ago         197MB
[root@chy ~]# docker load --input centos7-daochu.tar //恢复镜像(恢复镜像还有一个命令是docker load < centos7-daochu
788edba9eaa8: Loading layer [==================================================>]  446.1MB/446.1MB
Loaded image: centos7:latest
[root@chy ~]# docker images //查看已经恢复成功
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos7             latest              3e6c83d2f374        38 minutes ago      435MB
centos_with_net     latest              87dde0ff7187        22 hours ago        277MB
centos              latest              d123f4e55e12        10 days ago         197MB  导出镜像还有一个命令是如下
docker export container_id > file.tar // 导出容器,可以迁移到其他机器上,需要导入
cat file.tar |docker import - aming_test  //这样会生成aming_test的镜像(导入镜像)  2 容器管理
[root@chy ~]# docker create -it centos7 bash //创建容器但是容器没有启动
[root@chy ~]# docker start 438606c //启动容器
438606c
[root@chy src]# docker run -itd centos7 //这个也是启动容器之不多这个是create与start融为一体了
[root@chy ~]# docker run -itd --name centos6 centos_with_net bash // --name 给容器自定义名字
46fe5b5e5d2768727e8256e79ba6a3b3255394bda5398ec4011526fec8437dbc
[root@chy ~]# docker ps //查看定义的名称centos6,这个在进入容器的时候就可以直接跟名字不用给它的id了
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
46fe5b5e5d27        centos_with_net     "bash"              7 seconds ago       Up 5 seconds                            centos6
[root@chy ~]# docker run --rm -it centos_with_net  bash -c "sleep 30" //--rm 可以让容器退出后直接删除,在这里命令执行完容器就会退出
[root@chy ~]# docker logs 34effba //获取到容器的运行历史信息
111
[root@chy ~]# docker exec -it 34effba bash //可以临时打开一个虚拟终端,并且exit后,容器依然运行着
[root@chy ~]# docker rm 34effba//删除容器,如果要删除的是一个运行的容器需要加-m 后面在跟container_id,(container_id是ps的时候查看到的)
34effba
[root@chy ~]# docker start $(docker ps -a | awk '{ print $1}' | tail -n +2) //启动所有的容器命令
46fe5b5e5d27
438606c2d465
[root@chy ~]# docker rm $(docker ps -a | awk '{ print $1}' | tail -n +2) //删除所有的容器命令
46fe5b5e5d27
438606c2d465
docker rmi $(docker images | awk '{print $3}' |tail -n +2) //删除所有的镜像  3 仓库管理
  - 创建私有仓库
[root@chy ~]# docker pull registry //下载registry 镜像,registy为docker官方提供的一个镜像,我们可以用它来创建本地的docker私有仓库
[root@chy ~]#  docker run -d -p 5000:5000 registry //以registry镜像启动容器,-p会把容器的端口映射到宿主机上,:左边为宿主机监听端口,:右边为容器监听端口  - 将镜像上传到私有仓库
[root@chy ~]# docker tag centos7 192.168.212.10:5000/centos7 //标记一下tag,必须要带有私有仓库的ip:port
[root@chy ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
192.168.212.10:5000/centos7   latest              3e6c83d2f374        2 hours ago         435MB
centos7                       latest              3e6c83d2f374        2 hours ago         435MB
centos_with_net               latest              87dde0ff7187        24 hours ago        277MB
registry                      latest              a07e3f32a779        10 days ago         33.3MB
centos                        latest              d123f4e55e12        10 days ago         197MB
[root@chy ~]# docker push 192.168.212.10:5000/centos7 //上传时报了如下的错误
The push refers to a repository [192.168.212.10:5000/centos7]
Get https://192.168.212.10:5000/v2/: http: server gave HTTP response to HTTPS client
解决方法如下:
[root@chy ~]# vi /etc/docker/daemon.json //更改配置文件
{ "insecure-registries":["192.168.212.10:5000"] }
如上指定一个私有仓库的地址,这个地址是宿主机的地址
[root@chy ~]# systemctl restart docker //重启服务
[root@chy ~]# docker start ef587be45616 //启动私有仓库的容器
ef587be45616
[root@chy ~]# docker push 192.168.212.10:5000/centos7 //上传镜像到私有仓库
The push refers to a repository [192.168.212.10:5000/centos7]
788edba9eaa8: Pushed
latest: digest: sha256:d57bd79f46a10c520a62814d33a13c7c4c2b10ac143650effc9c8e35b2094565 size: 529
[root@chy ~]# curl 127.0.0.1:5000/v2/_catalog //查看到推送上来的镜像
{"repositories":["centos7"]}  - 怎么下载私有仓库的镜像
[root@chy ~]# docker pull 192.168.212.10:5000/centos7 //直接docker pull即可,这里的ip一定要是私有仓库的地址  如果在其它机器下载私有仓库切记不要忘记指定私有仓库地址的ip。([root@chy ~]# vi /etc/docker/daemon.json //更改配置文件
  { "insecure-registries":["192.168.212.10:5000"] })
   4 数据管理
  - 容器是由镜像启动的,这时如果将容器关闭或者是删除那容器里面的数据去哪了呢?这时数据也会一并的消除,这样肯定是不符合我们的要求的,所以呢想到了一个方法就是将宿主机的一个目录挂载到容器中,这时容器里产生的数据都会在宿主机的目录中显示。下面就是如何将容器中的数据搞到容器中
  - 挂载本地的目录到容器里
[root@chy ~]# docker run -tid -v /data/:/data centos7 bash ////-v 用来指定挂载目录,:前面的/data/为宿主机本地目录,:后面的/data/为容器里的目录,会在容器中自动创建
bc7f95597ceb71a3ebfcb5f4955d171b69f740c9598396eae1234966d68d1f81
[root@chy ~]# ls -l /data/ 查看宿主机/data目录
总用量 16
drwxr-xr-x 3 root   root   4096 9月   2 18:38 backup
drwxr-xr-x 3 root   root   4096 10月 27 06:15 gitroot
drwxr-xr-x 9 mysql1 mysql1 4096 11月 14 22:47 mariadb
drwxr-xr-x 5 root   root   4096 8月   5 04:40 wwwroot
[root@chy ~]# docker exec -it bc7f9559 bash
[root@bc7f95597ceb /]# mkdir /data/123 之后在容器中创建一个目录
[root@bc7f95597ceb /]# exit
[root@chy ~]# ls /data/ 在宿主机可以查看到在容器中创建的目录
123/       backup/    gitroot/   .htpasswd  mariadb/   wwwroot/  - 挂载数据卷
[root@chy ~]# docker ps //这里需要先查看容器的名字centos7的为sharp_kepler(查看names下面的名字)
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
bc7f95597ceb        centos7             "bash"                   8 minutes ago       Up 8 minutes                                 sharp_kepler
ef587be45616        registry            "/entrypoint.sh /e..."   About an hour ago   Up About an hour    0.0.0.0:5000->5000/tcp   elastic_varahamihira
[root@chy ~]# docker run -itd --volumes-from sharp_kepler  centos7  bash //--volumes-from后面跟容器的names的名字,之后在跟一个镜像名
12204913c07468253f58a52c52e328ff5cdb1d01866ae55e86d7c9f088a0bf2f  - 定义数据卷容器
  有时候,我们需要多个容器之间相互共享数据,类似于linux里面的NFS,所以就可以搭建一个专门的数据卷容器,然后其他容器直接挂载该数据卷。
[root@chy ~]# docker run -itd -v /data/ --name testvol centos bash
这里的-v是有两层含义的第一层含义是: 冒号是用来交互宿主机与容器的目录的映射,第二层含义是作为数据卷容器就是不加冒号的写法,第二层的含义是用来共享一个目录,所以-v /data的目录为共享目录
然后让其他容器挂载该数据卷
docker run -itd --volumes-from testvol aming123 bash  希望看过的童鞋多多指教,谢谢! DSC0000.gif
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-481533-1-1.html 上篇帖子: Docker提高拉取官网镜像的速度 下篇帖子: pipework配置docker网桥网络
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表