本身动手作一个最小的docker镜像

概述

其实有人学了好久仍是把docker当虚拟机来使用,可是docker其实和虚拟机是彻底不同的,如何理解这一区别呢,我以为本身动手作一个docker的hello world镜像是最好的linux

先体验一下hello-world镜像

在作以前咱们能够先本身体验一下docker官方的helloworld镜像,首先在本身的机器上安装上docker,安装完成以后从仓库里pull镜像 sudo docker pull hello-world 接着就是把这个镜像运行起来 sudo docker run hello-worldgit

➜  dockerfile sudo docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

没错就是打印出了一些信息,这是怎么作到的呢?接下来咱们了解一些基础知识github

基础知识

首先docker和虚拟机不同的地方就是docker使用了主机本身的内核,这就是为何linux的容器只能跑在linux上,windows的镜像只能跑在windows上的缘由了,还有docker镜像里面是没有包含内核的,只有一些软件须要的包和软件自己,也就是好比个人Ubuntu的docker镜像里面是没有Ubuntu内核的,包含了Ubuntu除了内核以外的全部东西。若是你不相信的话你能够在实体机Ubuntu下pull一个centos镜像运行起来看看是否是和你的实体机Ubuntu系统内核是同样的。docker

那么docker的hello-world的原理是什么

首先你们要知道的是hello-world镜像的Dockerfileubuntu

FROM scratch
ADD hello /
CMD ["/hello"]

什么意思呢,第一行的意思就是白手起家从零构建一个镜像,我不须要一个基础镜像,第二行意思是把本机的hello这个二进制文件拷贝到镜像中去,最后一行就是执行了,而后hello这个二进制文件就能够在本身实体机系统内核跑起来了 因此咱们制做一个最小的docker镜像第一步要准备这个hello文件windows

操做

首先准备这个二进制文件,为了简单化直接下载就好 https://github.com/docker-library/hello-world/blob/b7a78b7ccca62cc478919b101f3ab1334899df2b/hello 点击download下载 接着编写Dockerfilecentos

FROM scratch
ADD hello /
CMD ["/hello"]

以后build镜像 docker build -t mini .bash

➜  test docker build -t mini .
Sending build context to Docker daemon  3.584kB
Step 1/3 : FROM scratch
 ---> 
Step 2/3 : COPY hello /
 ---> 8b398d623500
Step 3/3 : CMD ["/hello"]
 ---> Running in e3e476d5ea2e
Removing intermediate container e3e476d5ea2e
 ---> 43f766e9d546
Successfully built 43f766e9d546
Successfully tagged mini:latest

接着运行起来就好app

➜  test sudo docker run mini
Hello from Docker.
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (Assuming it was not already locally available.)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

For more examples and ideas, visit:
 http://docs.docker.com/userguide/

欢迎关注Bboysoul的博客www.bboysoul.com Have Funide

相关文章
相关标签/搜索