当前位置: 首页 > news >正文

Docker容器虚拟化(一)—安装与镜像管理

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

简介

  • 容器虚拟化,比传统的虚拟化轻量
  • 2013年出现,发展非常迅猛
  • Redhat在6.5版本开始支持docker
  • 使用go语言开发,基于apache2.0协议
  • 开源软件,项目代码在github维护

Docker就是一个Container的管理工具,Container就是一个更轻量级的虚拟机,但是这个虚拟机没有操作系统和设备(操作系统是共享的)。

关于docker和container的关系: https://my.oschina.net/u/3497124/blog/1023498
关于虚拟机和container的比较:https://my.oschina.net/u/3497124/blog/1503684

docker的优势

  • 启动非常快,秒级实现
  • 资源利用率很高,一台机器可以跑上千个docker容器
  • 更快的交付和部署,一次创建和配置后,可以在任意地方运行
  • 内核级别的虚拟化,不需要额外的hypevisor支持,会有更高的性能和效率
  • 易迁移,平台依赖性不强

mark

docker核心概念

  • 镜像,是一个只读的模板,类似于安装系统用到的那个iso文件,我们通过镜像来完成各种应用的部署。
  • 容器,镜像类似于操作系统,而容器类似于虚拟机本身。它可以被启动、开始、停止、删除等操作,每个容器都是相互隔离的。
  • 仓库,存放镜像的一个场所,仓库分为公开仓库和私有仓库。 最大的公开仓库是Docker hub(hub.docker.com),国内公开仓库(dockerpool.com)

docker安装

环境:centos 3.10.0-514.el7.x86_64
docker:Docker version 1.12.6, build c4618fb/1.12.6

[root@study ~]# yum install -y epel-release

[root@study ~]# yum install -y docker

启动:
[root@study ~]# systemctl start docker.service

docker镜像管理

从docker.com下载centos镜像:
[root@study ~]# docker pull centos

查看本地有哪些镜像:
[root@study ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    latest              196e0ce0c9fb        3 weeks ago         196.6 MB

[root@study ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
adai                part2               196e0ce0c9fb        3 weeks ago         196.6 MB
docker.io/centos    latest              196e0ce0c9fb        3 weeks ago         196.6 MB


为镜像设定名称和标签:
[root@study ~]# docker tag centos adai:part2

查看docker库可用的镜像:
[root@study ~]# docker search [image-name]
##[image-name]:该部分为关键字,如:
[root@study ~]# docker search centos
INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/centos                             The official build of CentOS.                   3682      [OK]       
##stars:表示维护该镜像的人数

运行docker中的镜像:
[root@study ~]# docker run -it centos /bin/bash
[root@e523a18c4268 /]# 
##-i表示让容器的标准输入打开
##-t表示分配一个伪终端
##要把-i -t 放到镜像名字前面
##当该镜像发生修改后,我们可以把该镜像提交重新生成一个新版本进行在本地。

退出:
[root@e523a18c4268 /]# exit
exit

查看正在运行的容器:
[root@study ~]# docker ps

##-a :可显示已经停止的容器
[root@study ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
e523a18c4268        centos              "/bin/bash"         3 minutes ago       Exited (0) 2 minutes ago                       hopeful_galileo

删除镜像:

[root@study ~]# docker rmi adai
##默认删除tag为latest的镜像
##如果删除同名不同标签的镜像需要加标签名,如adai:part2

用来删除指定镜像,其中后面的参数可以是tag,如果是tag时,实际上是删除该tag,只要该镜像还有其他tag,就不会删除该镜像。当后面的参数为镜像ID时,则会彻底删除整个镜像,连通所有标签一同删除。

官网docker相关的命令

docker build -t friendlyname .  # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyname  # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyname         # Same thing, but in detached mode
docker container ls                                # List all running containers
docker container ls -a             # List all containers, even those not running
docker container stop <hash>           # Gracefully stop the specified container
docker container kill <hash>         # Force shutdown of the specified container
docker container rm <hash>        # Remove specified container from this machine
docker container rm $(docker container ls -a -q)         # Remove all containers
docker image ls -a                             # List all images on this machine
docker image rm <image id>            # Remove specified image from this machine
docker image rm $(docker image ls -a -q)   # Remove all images from this machine
docker login             # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
docker push username/repository:tag            # Upload tagged image to registry
docker run username/repository:tag                   # Run image from a registry

docker镜像使用容器生成新的镜像

运行docker run后,进入到该容器中,我们做一些变更,比如安装一些东西,然后针对这个容器进行创建新的镜像。

启动已经存在的(关闭状态)docker容器:
[root@study ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
e523a18c4268        centos              "/bin/bash"         12 hours ago        Exited (0) 12 hours ago                       hopeful_galileo

[root@study ~]# docker start e52
e52
##e52:id,在此可简写

[root@study ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
e523a18c4268        centos              "/bin/bash"         12 hours ago        Up 32 seconds                           hopeful_galileo


进入已经开启的容器:
[root@study ~]# docker exec -it e52 /bin/bash
[root@e523a18c4268 /]# 

镜像内的操作

[root@e523a18c4268 /]# yum install -y net-tools wget

查看当前容器的版本:
[root@e523a18c4268 /]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core)

内核版本:
[root@e523a18c4268 /]# uname -a
Linux e523a18c4268 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@study ~]# docker commit -m "centos_with_nettools_and_wget" -a "adai" e523a18c4268 centos_with_net
##-m:=Mark,做标记
##-a:admin,维护人信息
##e52…:镜像ID
##centos_with_net:新的镜像名称

[root@study ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
centos_with_net     latest              5a2996994a25        About a minute ago   271.3 MB
adai                part2               196e0ce0c9fb        3 weeks ago          196.6 MB
docker.io/centos    latest              196e0ce0c9fb        3 weeks ago          196.6 MB

基于本地模板导入创建镜像

镜像模板站:https://openvz.org/Download/template/precreated

下载centos7镜像:
[root@study ~]# wget https://download.openvz.org/template/precreated/centos-7-x86_64-minimal.tar.gz

将该包导入docker镜像:
[root@study ~]# cat centos-7-x86_64-minimal.tar.gz | docker import - centos-7-x86_64-minimal

[root@study ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
centos-7-x86_64-minimal   latest              e21aadc0e309        About an hour ago   434.5 MB
centos_with_net           latest              5a2996994a25        About an hour ago   271.3 MB

把docker中的镜像导出来:
[root@study ~]# docker save -o centos_with_net.tar 5a2996994a25
##注:此处可以把镜像ID改为镜像名称:tag

[root@study ~]# du -sh centos_with_net.tar
268M	centos_with_net.tar

恢复本地docker镜像:
[root@study ~]# docker load --input centos_with_net.tar
或,
[root@study ~]# docker load < centos_with_net_adai.tar
db7d29fe3847: Loading layer 74.75 MB/74.75 MB
Loaded image ID: sha256:5a2996994a252181d1911e30e1cf229b4b358c5669d25afcade5b4c8902522d6
[root@study ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
centos-7-x86_64-minimal   latest              e21aadc0e309        About an hour ago   434.5 MB
<none>                    <none>              5a2996994a25        About an hour ago   271.3 MB
##恢复后没名字,使用docker tag改名

把自己的镜像推送到dockerhub官网:
[root@study ~]# docker push image_name
##前提是需要在官网先注册一个用户

转载于:https://my.oschina.net/adailinux/blog/1550730

相关文章:

  • Servlet常用方法
  • Mysql性能优化一
  • Android Animation(动画)---基础一
  • java:练习学校学生
  • [译]从形式到功能,设计思维的改变
  • Azure 基础:Queue Storage
  • 机器学习算法 Python R速查表
  • Nginx+Keepalived主备
  • Spotify模式并非“敏捷涅磐”
  • SQLServer存储过程返回值总结
  • 算法笔记--中国剩余定理
  • 创建Kafka0.8.2生产者与消费者
  • IDEA在编辑时提示could not autowire
  • linux 使用记录
  • 关于ashx不可重定向问题
  • - C#编程大幅提高OUTLOOK的邮件搜索能力!
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • JS基础篇--通过JS生成由字母与数字组合的随机字符串
  • js数组之filter
  • Laravel 实践之路: 数据库迁移与数据填充
  • laravel5.5 视图共享数据
  • LeetCode刷题——29. Divide Two Integers(Part 1靠自己)
  • Python_网络编程
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • 闭包--闭包之tab栏切换(四)
  • 初识 webpack
  • 搞机器学习要哪些技能
  • 关于List、List?、ListObject的区别
  • 经典排序算法及其 Java 实现
  • 让你的分享飞起来——极光推出社会化分享组件
  • 中文输入法与React文本输入框的问题与解决方案
  • 测评:对于写作的人来说,Markdown是你最好的朋友 ...
  • 进程与线程(三)——进程/线程间通信
  • ​LeetCode解法汇总307. 区域和检索 - 数组可修改
  • ​RecSys 2022 | 面向人岗匹配的双向选择偏好建模
  • #前后端分离# 头条发布系统
  • (a /b)*c的值
  • (day 2)JavaScript学习笔记(基础之变量、常量和注释)
  • (Git) gitignore基础使用
  • (Mirage系列之二)VMware Horizon Mirage的经典用户用例及真实案例分析
  • (Spark3.2.0)Spark SQL 初探: 使用大数据分析2000万KF数据
  • (二)hibernate配置管理
  • (机器学习-深度学习快速入门)第三章机器学习-第二节:机器学习模型之线性回归
  • (十七)Flask之大型项目目录结构示例【二扣蓝图】
  • (最全解法)输入一个整数,输出该数二进制表示中1的个数。
  • (最完美)小米手机6X的Usb调试模式在哪里打开的流程
  • .NET 8.0 发布到 IIS
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  • .Net FrameWork总结
  • .NET LINQ 通常分 Syntax Query 和Syntax Method
  • .Net Remoting(分离服务程序实现) - Part.3
  • .net 反编译_.net反编译的相关问题
  • .NET 中使用 Mutex 进行跨越进程边界的同步
  • .net连接oracle数据库
  • @GetMapping和@RequestMapping的区别