Bboysoul's Blog

首页 公告 RSS

自己动手做一个最小的docker镜像

April 16, 2018 本文有 1101 个字 需要花费 3 分钟阅读

概述

其实有人学了很久还是把docker当虚拟机来使用,但是docker其实和虚拟机是完全不一样的,如何理解这一区别呢,我觉得自己动手做一个docker的hello world镜像是最好的

先体验一下hello-world镜像

在做之前我们可以先自己体验一下docker官方的helloworld镜像,首先在自己的机器上安装上docker,安装完成之后从仓库里pull镜像
sudo docker pull hello-world
接着就是把这个镜像运行起来
sudo docker run hello-world

➜  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/

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

基础知识

首先docker和虚拟机不一样的地方就是docker使用了主机自己的内核,这就是为什么linux的容器只能跑在linux上,windows的镜像只能跑在windows上的原因了,还有docker镜像里面是没有包含内核的,只有一些软件需要的包和软件本身,也就是比如我的Ubuntu的docker镜像里面是没有Ubuntu内核的,包含了Ubuntu除了内核之外的所有东西。如果你不相信的话你可以在实体机Ubuntu下pull一个centos镜像运行起来看看是不是和你的实体机Ubuntu系统内核是一样的。

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

首先大家要知道的是hello-world镜像的Dockerfile

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

什么意思呢,第一行的意思就是白手起家从零构建一个镜像,我不需要一个基础镜像,第二行意思是把本机的hello这个二进制文件拷贝到镜像中去,最后一行就是执行了,然后hello这个二进制文件就可以在自己实体机系统内核跑起来了
所以我们制作一个最小的docker镜像第一步要准备这个hello文件

操作

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

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

之后build镜像
docker build -t mini .

➜  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

接着运行起来就好

➜  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/

欢迎关注我的博客www.bboy.app
Have Fun


Tags:

本站总访问量 本站总访客数