1 安裝docker的apt源
apt-get install apt-transport-https ca-certificates curl software-properties-common
2 添加docker官方的GPG
root@zhf-linux:/home# curl -s https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK
3 添加docker的源。如果沒有docker.list則自己創建一個
root@zhf-linux:/etc/apt/sources.list.d# bash -c "echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
4 安裝docker
apt install docker.io
5 查看docker的版本:
root@zhf-linux:/var/lib/docker# docker version
Client:
Version: 1.13.1
API version: 1.26
Go version: go1.6.2
Git commit: 092cba3
Built: Thu Sep 7 17:23:05 2017
OS/Arch: linux/386
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Go version: go1.6.2
Git commit: 092cba3
Built: Thu Sep 7 17:23:05 2017
OS/Arch: linux/386
Experimental: false
到這里docker整個就安裝完了,我們來看下如何來啟動一個鏡像
1 通過docker run命令來啟動一個httpd的鏡像。如果沒有發現httpd鏡像會從docker hub下載鏡像。下載完成后,鏡像httpd被保存到本地
docker run -d -p 80:80 httpd
2 docker images可以看到已經下載到本地
root@zhf-linux:/home/zhf/zhf/c_prj/make_function# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest 7659d5a9a057 2 weeks ago 189 MB
3 docker ps顯示容器正在運行
root@zhf-linux:/home/zhf/zhf/c_prj/make_function# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39ae70ca12b2 httpd "httpd-foreground" 6 minutes ago Up 6 minutes 0.0.0.0:80->80/tcp compassionate_bohr
接下來看下如何下載一個鏡像。通過docker pull下載。docker pull會從Docker Hub進行下載。
root@zhf-linux:/home/zhf/zhf/c_prj/make_function# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
cf7dde121f94: Pull complete
Digest: sha256:0e06ef5e1945a718b02a8c319e15bae44f47039005530bc617a5d071190ed3fc
Status: Downloaded newer image for hello-world:latest
通過docker images看到這個鏡像只有600多K
root@zhf-linux:/home/zhf/zhf/c_prj/make_function# docker images hello-world
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest abd130ec0722 3 months ago 665 kB
通過docker run hello-world來執行。結果如下:
root@zhf-linux:/home/zhf/zhf/c_prj/make_function# 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.
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://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
下載的docker鏡像默認存儲路徑是/var/lib/docker .具體的鏡像文件就在containers文件夾中
root@zhf-linux:/var/lib/docker# ls -al
total 44
drwx--x--x 11 root root 4096 Oct 30 22:28 .
drwxr-xr-x 79 root root 4096 Dec 11 21:08 ..
drwx------ 5 root root 4096 Oct 30 22:28 aufs
drwx------ 8 root root 4096 Dec 19 20:59 containers
drwx------ 3 root root 4096 Oct 30 22:28 image
drwxr-x--- 3 root root 4096 Oct 30 22:28 network
drwx------ 4 root root 4096 Oct 30 22:28 plugins
drwx------ 2 root root 4096 Oct 30 22:28 swarm
drwx------ 2 root root 4096 Dec 19 20:50 tmp
drwx------ 2 root root 4096 Oct 30 22:28 trust
drwx------ 2 root root 4096 Oct 30 22:28 volumes
Dockerfile是鏡像的描述文件,定義了如何構建Docker鏡像。Dockerfile需要到docker網站去查看:
查看方法如下:
1 首先在網站上注冊一個docker賬號
2 在上面的搜索欄中輸入hello-world
3 得到倉庫中所有關於hello world的鏡像,第一個就是我們下載的鏡像
4 點擊進入后,會發現如下的dockerfile鏈接
5 點擊進入后跳轉到github上的鏈接,可以看到hello-world的dockerfile寫法
只有短短三條指令。
-
FROM scratch
此鏡像是從白手起家,從 0 開始構建。 -
COPY hello /
將文件“hello”復制到鏡像的根目錄。 -
CMD ["/hello"]
容器啟動時,執行 /hello
鏡像 hello-world 中就只有一個可執行文件 “hello”,其功能就是打印出 “Hello from Docker ......” 等信息。
/hello 就是文件系統的全部內容,連最基本的 /bin,/usr, /lib, /dev 都沒有。 而/hello是根據hello.c編譯而來的。可以點進去看下hello.c的內容,其實就是打印我們剛才執行docker run hello-world的內容。