镜像(Images)

镜像是Docker的三大核心之一,类似于虚拟机,作用和虚拟机是一样的,唯独是组成部分会有些区别。简单的说如果我们想启动一个容器就必须要有镜像。docker运行容器前需要本地存在对应的镜像,如果镜像不存在本地,docker会尝试先从默认镜像仓库下载(Docker Hub),用户也可以通过配置,使用自定义的镜像仓库。

镜像文件存储结构?

docker相关文件存放在:/var/lib/docker目录下

/var/lib/docker
├── builder
├── buildkit
├── containerd
├── containers
├── image
├── network
├── overlay2
├── plugins
├── runtimes
├── swarm
├── tmp
├── trust
└── volumes

国内doceker仓库地址

vim /etc/docker/daemon.json 

{
"registry-mirrors":[ "https://registry.docker-cn.com" ]
}
#获取镜像
docker pull 【镜像名称】 [root@controller ~]# docker pull ubuntu
Using default tag: latest
Trying to pull repository docker.io/library/ubuntu ...
latest: Pulling from docker.io/library/ubuntu
660c48dd555d: Pull complete
4c7380416e78: Pull complete
421e436b5f80: Pull complete
e4ce6c3651b3: Pull complete
be588e74bd34: Pull complete
Digest: sha256:7c67a2206d3c04703e5c23518707bdd4916c057562dd51c74b99b2ba26af0f79 #查看镜像
[root@controller ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu latest 20c44cd7596f 3 days ago 122.8 MB
字段解释:
REPOSITORY 来自哪个仓库
TAG 镜像标签信息 (别名的作用)
IMAGE ID 镜像ID号(唯一)
CREATED 创建时间
SIZE 大小
#获取一个ubuntu 14.04版本
[root@controller ~]# docker pull ubuntu: 14.04
[root@controller ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu latest 20c44cd7596f 3 days ago 122.8 MB
docker.io/ubuntu 14.04 d6ed29ffda6b 3 days ago 221 MB
我们可以看到这两个images来自同一个仓库,TAG标签不一样标记不同的镜像,使用docker tag命令可以为本地镜像添加新的标签。 #查看镜像详细信息
docker inspect 【IMAGE ID】
[root@controller ~]# docker inspect 20c44cd7596f
[
{
"Id": "sha256:20c44cd7596ff4807aef84273c99588d22749e2a7e15a7545ac96347baa65eda",
"RepoTags": [
"docker.io/ubuntu:latest"
],
...
-f参数指定查询:
[root@controller ~]# docker inspect -f {{".RepoTags"}} 20c44cd7596f
[docker.io/ubuntu:latest] #搜索镜像
[root@controller ~]# docker search nginx
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/nginx Official build of Nginx. 7317 [OK]
docker.io docker.io/jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 1174 [OK]
docker.io docker.io/richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 475 [OK] #删除镜像
docker rmi 【标签或ID】
[root@controller ~]# docker rmi ubuntu:latest
Untagged: ubuntu:latest #运行一个容器
[root@controller ~]# docker run ubuntu echo 'hello I am ubuntu #查看存在的所有容器
[root@controller ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2bb8a93f5d34 ubuntu "echo 'hello I am ubu" About a minute ago Exited (0) About a minute ago gigantic_heyrovsky
#这时删除镜像出现错误
[root@controller ~]# docker rmi ubuntu
Error response from daemon: conflict: unable to remove repository reference "ubuntu" (must force) - container 2bb8a93f5d34 is using its referenced image 20c44cd7596f
#强制删除加-f
[root@controller ~]# docker rmi -f ubuntu #创建镜像
创建镜像有三种方法:
1.已有镜像创建
2.本地模板导入
3.Dockerfile创建 1.已有镜像创建
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string
-c, --change value
--help
-m, --message string
-p, --pause #首先启动一个镜像
[root@controller ~]# docker run -ti ubuntu:14.04 /bin/bash
root@c5439518dcbe:/# touch test
root@c5439518dcbe:/# exit
记住容器的ID:c5439518dcbe #创建一个新的镜像
[root@controller ~]# docker commit -m "added a new file" -a "Docker newbee" c5439518dcbe test
sha256:4efd37e84d4998fbc7cff55d21fa360fdd60f1d7319050e70512754bf2851b7f
[root@controller ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test latest 4efd37e84d49 18 seconds ago 221 MB #模板下载:https://download.openvz.org/template/precreated/
[root@controller ~]# cat ubuntu-12.04-x86-minimal.tar.gz |docker import - ubuntu:12.04
sha256:8d6161cea96c6b2f2ef39be5b2370e38cb003e8f3b5c2d3f960e0e7d37733d41 #导出镜像到本地
[root@controller ~]# docker save -o ubuntu_12.04.tar ubuntu:12.04
#从本地导入镜像
[root@controller ~]# docker load --input ubuntu_12.04.tar

[root@controller ~]# docker load < ubuntu_12.04.tar #上传镜像(默认上传到Docker hub)
[root@controller ~]# docker push --help
Usage: docker push [OPTIONS] NAME[:TAG]
[root@controller ~]# docker tag test:latest user/test:latest
[root@controller ~]# docker push user/test:latest

小结:

镜像是使用Docker的前提,因此可以结合公司的生产环境制作好镜像,方便使用。

Docker学习之2——镜像的更多相关文章

  1. Docker学习笔记之镜像与容器

    0x00 概述 镜像和容器作为 Docker 里最基础的概念,我们很有必要了解 Docker 对它们的很多定义以及其他与它们有关的知识.在这一小节里,我们就专门针对镜像与容器两个概念展开,细致的梳理与 ...

  2. docker学习笔记2--对镜像/容器的命令操作

    Docker启动一个Centos镜像 我们下载完成一个Centos镜像之后,开始启动 docker run -d -i -t <imageID> /bin/bash 这样就能启动一个一直停 ...

  3. Docker学习笔记:镜像、容器、数据卷

    核心概念 镜像:一个只读的模板,类似虚拟机的镜像. 容器:可以理解为镜像的一个运行实例.运行时类似于沙箱,多个容器互相独立. 仓库:存放镜像文件的地方. 镜像 命令表格 命令 解释 选项 docker ...

  4. docker学习笔记-常用镜像相关命令

    docker images # 1.使用 [root@iZbp13qr3mm4ucsjumrlgqZ ~]# docker images REPOSITORY TAG IMAGE ID CREATED ...

  5. Docker学习笔记--2 镜像的创建

    如果我们需要在Docker环境下部署tomcat.redis.mysql.nginx.php等应用服务环境,有下面三种方法: 1,根据系统镜像创建Docker容器,这时容器就相当于是一个虚拟机,进入容 ...

  6. docker学习之二镜像创建

    继上一篇docker入门之后写一点使用的经验. 通过命令:docker run -it REPOSITORY或IMAGE ID   注:-it后面跟的字段可以通过下面指令获得 创建运行的容器,会进入一 ...

  7. Docker学习笔记-CentOS7镜像

    前言: 环境:centos7.5 64 位 正文: 第一步:下载centos7镜像 docker pull centos 第二步:建立centos7的容器 sudo docker run --priv ...

  8. Docker学习(7) 构建镜像

    构建docker镜像 1 构建镜像的两种方式 1 通过容器构建镜像 2 通过Dockerfile构建镜像

  9. Docker学习笔记之通过 Dockerfile 创建镜像

    0x00 概述 由于 Docker 镜像的结构优势,使它的占用空间远小于普通的虚拟机镜像,而这就大幅减少了 Docker 镜像在网络或者其他介质中转移所花费的时间,进而提高了我们进行迁移部署的效率.不 ...

随机推荐

  1. Echarts 在动态HTML报告中的应用

    # 参考官网 http://echarts.baidu.com/examples/ <scripts> <!--- echarts examples ---> </scr ...

  2. 【Selenium】【BugList9】windows环境,fp = open("./"+ time.strftime("%Y-%m-%d %H:%M:%S") + " result.html",'wb'),报错:OSError: [Errno 22] Invalid argument: './2018-09-05 10:29:32 result.html'

    [代码] if __name__=="__main__": suite = unittest.TestSuite() suite.addTest(Baidu("test_ ...

  3. tensorflow学习之(十一)将python代码写入文件

    #save to file import tensorflow as tf import numpy as np ##(1)Save to file 把相关变量存储到文件中 #remember to ...

  4. CentOS7 + Django2.1 + uwsgi + nginx配置

    假设已经可以运行Django项目,可以runserver.也已经安装了uwsgi和nginx 现在需要进行配置. 刚开始进行uwsgi测试就不行,提示bash:'uwsgi' Command not ...

  5. 作业 -- 几道简单的Python题

    1.编写程序,要求生成10240个随机[0,512)之间的整数,并统计每个元素出现的次数. 2.编写程序,要求当用户输入一个列表和两个整数作为下标时,程序可以使用切片获取并输出列表中截取两个下标之间的 ...

  6. tp5

    tp5.1创建模块 把build.php放在应用目录下面, 然后打开cmd, cd../../ cd phpstudy/www/tp5 php think build tp5.1控制器 return ...

  7. easyui属性赋值

    了解easyui tree组件的童鞋估计都知道tree的node有他自己单独的属性(id,text,iconCls,checked,state,attribute,target).而原先这个几个属性想 ...

  8. java 项目相关 学习记录

    一位资深程序员大牛给予Java初学者的学习路线建议  [任何时期都可以好好看看] https://www.imooc.com/article/8993 https://www.jianshu.com/ ...

  9. Windows批处理命令学习中遇到的坑--持续更新中

    再次拾起windows批处理命令,下边将一些遇到的小问题写出来,希望可以帮到大家 1.set命令:set主要的作用是为变量赋值,类似于编程语言中的var i = Value:但是在使用的过程中一定要注 ...

  10. Matlib

    >>> name1=input('请输入第一个名字;') 请输入第一个名字;陈汉彬 >>> name2=input('请输入第二个名字;') 请输入第二个名字;钟宇 ...