其實有人學了好久仍是把docker當虛擬機來使用,可是docker其實和虛擬機是徹底不同的,如何理解這一區別呢,我以爲本身動手作一個docker的hello world鏡像是最好的linux
在作以前咱們能夠先本身體驗一下docker官方的helloworld鏡像,首先在本身的機器上安裝上docker,安裝完成以後從倉庫裏pull鏡像 sudo docker pull hello-world
接着就是把這個鏡像運行起來 sudo docker run hello-world
git
➜ 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
首先你們要知道的是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