首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Docker容器虚拟化(一)—安装与镜像管理 原

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

作者头像
阿dai学长
发布2019-04-03 10:34:43
8900
发布2019-04-03 10:34:43
举报
文章被收录于专栏:阿dai_linux阿dai_linux

简介

  • 容器虚拟化,比传统的虚拟化轻量
  • 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支持,会有更高的性能和效率
  • 易迁移,平台依赖性不强

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
##前提是需要在官网先注册一个用户

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017/10/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
    • docker的优势
    • docker核心概念
    • docker安装
    • docker镜像管理
      • 官网docker相关的命令
      • docker镜像使用容器生成新的镜像
        • 镜像内的操作
          • 保存更改过的镜像
          • 基于本地模板导入创建镜像
          相关产品与服务
          容器镜像服务
          容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档