docker run 命令

镜像(image):An image is a filesystem and parameters to use at runtime. It doesn’t have state and never changes
容器(container): A container is a running instance of an image.
简言之,容器是镜像的实例。

运行命令的时候,执行如下步骤:
1 检查是否有名叫 hello-world 软件镜像(image)
2 如果没有,则从 docker hub 上下载
3 加载该镜像(image)到 容器(container)中,并运行

创建自己的镜像 image

首先运行 docker run docker/whalesay cowsay boo-boo
下载运行 docker hub 上的 whalesay 镜像。

- 创建一个 Dockerfile 文件

添加如下命令到文件中:
FROM docker/whalesay:latest (告诉docker当前的image是依靠于哪个image生成的)
RUN apt-get -y update && apt-get install -y fortunes(whalesay 镜像是基于ubuntu的,使用apt-get install来安装packages. 这两个命令更新了packages包,并安装了fortune 程序)
CMD /usr/games/fortune -a | cowsay(告诉镜像image在环境设置好以后的最后一个命令,该命令运行了furtune -a,并将输出发给额cowsay命令)
到这里,一个基本的Dockerfile就创建好了

- 根据 Dockerfile 来 Build 镜像

使用 docker build 命令来来创建镜像
-t 参数,给创建的镜像命名(give a tag)
. 不要忘记这个命令(就是一个英文句号),告诉 docker build 查找当前目录里的 dockerfile 文件。
$ docker build -t docker-whale .
 
Sending build context to Docker daemon 2.048 kB
...snip...
Removing intermediate container cb53c9d09f3b
Successfully built c2c3152907b5

  1. Docker检查确认所有build所需都齐了,输出如下消息:

    Sending build context to Docker daemon 2.048 kB
  2. Docker 检查 whalesay image 是否存在,不存在则从Docker Hub上pull下来。

    Step 1 : FROM docker/whalesay:latest
    ---> 6b362a9f73eb

    在每一步结束,都会有一个ID被打印出来。这个ID是每一步创建出的 Layer 的ID。 Dockerfile 中每一行命令都和 image 中的一个 Layer 相关。你自己的ID会显示的不一样。

  3. Docker 创建一个临时容器 container 来运行 whalesay 镜像image。在这个临时容器中,Docker 运行 Dockerfile中的下一条命令, 即 RUN 命令。这会安装 fortune 。这里会有很多输出,就和在 Ubuntu 中创建出来的消息一样。

    Step 2 : RUN apt-get -y update && apt-get install -y fortunes
    ---> Running in 05d4eda04526
    Ign http://archive.ubuntu.com trusty InRelease
    Get:1 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
    Get:2 http://archive.ubuntu.com trusty-security InRelease [65.9 kB]
    Hit http://archive.ubuntu.com trusty Release.gpg
    Hit http://archive.ubuntu.com trusty Release
    Get:3 http://archive.ubuntu.com trusty-updates/main Sources [480 kB]
    Get:4 http://archive.ubuntu.com trusty-updates/restricted Sources [5921 B]
    Get:5 http://archive.ubuntu.com trusty-updates/universe Sources [214 kB]
    Get:6 http://archive.ubuntu.com trusty-updates/main amd64 Packages [1160 kB]
    Get:7 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [20.4 kB]
    Get:8 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [505 kB]
    Get:9 http://archive.ubuntu.com trusty-security/main Sources [157 kB]
    Get:10 http://archive.ubuntu.com trusty-security/restricted Sources [4621 B]
    Get:11 http://archive.ubuntu.com trusty-security/universe Sources [54.5 kB]
    Get:12 http://archive.ubuntu.com trusty-security/main amd64 Packages [700 kB]
    Get:13 http://archive.ubuntu.com trusty-security/restricted amd64 Packages [17.0 kB]
    Get:14 http://archive.ubuntu.com trusty-security/universe amd64 Packages [191 kB]
    Hit http://archive.ubuntu.com trusty/main Sources
    Hit http://archive.ubuntu.com trusty/restricted Sources
    Hit http://archive.ubuntu.com trusty/universe Sources
    Hit http://archive.ubuntu.com trusty/main amd64 Packages
    Hit http://archive.ubuntu.com trusty/restricted amd64 Packages
    Hit http://archive.ubuntu.com trusty/universe amd64 Packages
    Fetched 3640 kB in 11s (329 kB/s)
    Reading package lists...
    Reading package lists...
    Building dependency tree...
    Reading state information...
    The following extra packages will be installed:
    fortune-mod fortunes-min librecode0
    Suggested packages:
    x11-utils bsdmainutils
    The following NEW packages will be installed:
    fortune-mod fortunes fortunes-min librecode0
    0 upgraded, 4 newly installed, 0 to remove and 92 not upgraded.
    Need to get 1961 kB of archives.
    After this operation, 4817 kB of additional disk space will be used.
    Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main librecode0 amd64 3.6-21 [771 kB]
    Get:2 http://archive.ubuntu.com/ubuntu/ trusty/universe fortune-mod amd64 1:1.99.1-7 [39.5 kB]
    Get:3 http://archive.ubuntu.com/ubuntu/ trusty/universe fortunes-min all 1:1.99.1-7 [61.8 kB]
    Get:4 http://archive.ubuntu.com/ubuntu/ trusty/universe fortunes all 1:1.99.1-7 [1089 kB]
    debconf: unable to initialize frontend: Dialog
    debconf: (TERM is not set, so the dialog frontend is not usable.)
    debconf: falling back to frontend: Readline
    debconf: unable to initialize frontend: Readline
    debconf: (This frontend requires a controlling tty.)
    debconf: falling back to frontend: Teletype
    dpkg-preconfigure: unable to re-open stdin:
    Fetched 1961 kB in 19s (101 kB/s)
    Selecting previously unselected package librecode0:amd64.
    (Reading database ... 13116 files and directories currently installed.)
    Preparing to unpack .../librecode0_3.6-21_amd64.deb ...
    Unpacking librecode0:amd64 (3.6-21) ...
    Selecting previously unselected package fortune-mod.
    Preparing to unpack .../fortune-mod_1%3a1.99.1-7_amd64.deb ...
    Unpacking fortune-mod (1:1.99.1-7) ...
    Selecting previously unselected package fortunes-min.
    Preparing to unpack .../fortunes-min_1%3a1.99.1-7_all.deb ...
    Unpacking fortunes-min (1:1.99.1-7) ...
    Selecting previously unselected package fortunes.
    Preparing to unpack .../fortunes_1%3a1.99.1-7_all.deb ...
    Unpacking fortunes (1:1.99.1-7) ...
    Setting up librecode0:amd64 (3.6-21) ...
    Setting up fortune-mod (1:1.99.1-7) ...
    Setting up fortunes-min (1:1.99.1-7) ...
    Setting up fortunes (1:1.99.1-7) ...
    Processing triggers for libc-bin (2.19-0ubuntu6.6) ...
    ---> dfaf993d4a2e
    Removing intermediate container 05d4eda04526

    RUN 结束,有一个 layer 会被创建,并且之前创建的临时容器 container 也被去除了。

  4. 一个新的临时容器 container 被创建出来,Docker 为CMD命令添加一个 Layer,然后在移除该容器 congtainer

    Step 3 : CMD /usr/games/fortune -a | cowsay
    ---> Running in a8e6faa88df3
    ---> 7d9495d03763
    Removing intermediate container a8e6faa88df3
    Successfully built 7d9495d03763

到这里,我们就创建了一个自己的镜像 image,名叫 docker-whale.

接下来直接运行 docker run docker-whale





使用 docker 创建自己的镜像的更多相关文章

  1. docker创建自己的镜像并配置nginx

    前言 最近在研究docker,记录一下如何创建一个属于自己的镜像 本次使用linux版本为centos7.4(centos6也可以使用docker,只不过有部分功能只有7才有) 本次创建的镜像为cen ...

  2. docker 创建新的镜像到私有仓库

    docker:/data# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bd6db4127a9e centos &q ...

  3. Docker入门(windows版),利用Docker创建一个Hello World的web项目

    Docker 当大家点开这篇博客的时候,相信大家对docker多多少少也有些认识了,最近学习docker这门技术,略微有些心得,写篇文章记录一下学习过程并帮大家跳过一些坑. docker的核心有两个, ...

  4. docker学习之二镜像创建

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

  5. Docker 基于已有镜像的容器创建镜像

    Docker 基于已有镜像的容器创建镜像: docker:/root# docker run -it januswel/centos /bin/bash docker exec -it januswe ...

  6. Docker创建支持ssh服务的容器和镜像

    原文链接:Docker创建支持ssh服务的容器和镜像 1. 这里使用的centos作为容器,所以首先下载centos的images # sudo docker pull centos 2. 下载后执行 ...

  7. docker commit命令创建新的镜像

    docker commit命令创建新的镜像 1.运行容器 2.修改容器 3.将容器保存为新容器 (1)运行容器 [root@cmdb-server docker]# docker run -ti ce ...

  8. docker学习系列(二):使用Dockerfile创建自己的镜像

    dockerfile可以允许我们自己创建镜像,通过编写里面的下载软件命令,执行docker build 即可生成镜像文件. 初尝dockerfile 新建一个目录test,然后进入这个目录,创建一个名 ...

  9. docker创建redis镜像

    pull redis 镜像 创建redis的镜像有几种方式,可以直接从仓库中拉取,也可以采用dockerfile文件自己编译创建. 基于已有的redis镜像,docker可以采用run,或者creat ...

随机推荐

  1. XSS跨站攻击(二)

    本人最近在学习XSS,想总结一下常见的XSS攻击的几种情况,刚好看到<防御 XSS 的七条原则>这篇文章,里面讲的七条防御原则不正是针对XSS的几种利用方式吗?于是,借来学习一下. 原则1 ...

  2. 20155305乔磊《网络对抗》逆向及Bof基础

    20155305乔磊<网络对抗>逆向及Bof基础 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何 ...

  3. 20155321 《网络攻防》 Exp2 后门原理与实践

    20155321 <网络攻防> Exp2 后门原理与实践 实验内容 例举你能想到的一个后门进入到你系统中的可能方式? 我觉得人们在平时上网的时候可能会无意识地点击到一些恶意的网站,这些网站 ...

  4. 20155333 《网络对抗》Exp3 免杀原理与实践

    20155333 <网络对抗>Exp3 免杀原理与实践 基础问题回答 (1)杀软是如何检测出恶意代码的? 基于特征码的检测: 启发式恶意软件检测: 基于行为的恶意软件检测. (2)免杀是做 ...

  5. POJ1159

    这竟然是IOI虽然是2000年的,但其实也改变不了它水题的本质 我写了两种方法,这里都讲一下吧 考虑记忆化搜索,用f[i][j]表示当区间的左端为i,右端为j时最少要添加多少字符,所以ans就为f[1 ...

  6. python 回溯法 子集树模板 系列 —— 18、马踏棋盘

    问题 将马放到国际象棋的8*8棋盘board上的某个方格中,马按走棋规则进行移动,走遍棋盘上的64个方格,要求每个方格进入且只进入一次,找出一种可行的方案. 分析 说明:这个图是5*5的棋盘. 图片来 ...

  7. [CF1065F]Up and Down the Tree[树dp]

    题意 给定一棵以 \(1\) 为根的树,你每次可以选择跳到某个叶子节点,再跳到和他深度差不超过 \(k\) 的祖先.询问最多能够跳到多少个叶子节点. \(n,k\leq 10^6\) . 分析 最后的 ...

  8. 你应该知道Go语言的几个优势

    要说起GO语言的优势,我们就得从GO语言的历史讲起了-- 本文由腾讯技术工程官方号发表在腾讯云+社区 2007年,受够了C++煎熬的Google首席软件工程师Rob Pike纠集Robert Grie ...

  9. stl源码剖析 详细学习笔记 hashtable

    //---------------------------15/03/24---------------------------- //hashtable { /* 概述: sgi采用的是开链法完成h ...

  10. winform 记事本 剪切 粘贴 全选 撤销

    private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Undo(); } private voi ...