天天看點

docker入門實踐01Docker 實踐入門

Docker 實踐入門

本文的所有操作的環境都是在一台虛拟機上操作完成的。

$ cat /etc/issue
Ubuntu 14.04.1 LTS \n \l

$ uname -r
3.13.0-32-generic
           

安裝Docker

必要條件

  • 核心版本要至少為3.8
  • 64位的CPU
  • 核心必須支援一種适合的存儲驅動
    • Device Manager (The Device Manager is a Control Panel applet in Microsoft Windows operating systems. It allows users to view and control the hardware attached to the computer。)
    • AUFS(AUFS implements a union mount for Linux file systems)
    • VFS (A virtual file system (VFS) is an abstraction layer on top of a more concrete file system. The purpose of a VFS is to allow client applications to access different types of concrete file systems in a uniform way)
    • btrfs (btrfs is intended to address the lack of pooling, snapshots, checksums, and integral multi-device spanning in Linux file systems.)
    • 預設的存儲驅動通常是 Device Mapper
  • 核心必須支援并且開啟 cgroup 和 namespace。

檢查前提條件

(1) 核心

$ uname -a
Linux jackson-virtual-machine 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
           

可以看到本機所裝的核心版本為

3.13.0-32-generic

,可以滿足docker的安裝條件,當然如果不滿足核心條件,更新核心也是很容易的。

$ sudo apt-get update  # 這一步更新包清單
$ sudo apt-get upgrade # 安裝所有更新
           

如果不想更新那麼多,可以

$ sudo apt-get install XXXX #安裝相應的核心包
           

最後需要更新 Grub 啟動加載器來加載新的核心

$ sudo update-grub
           

重新開機

$ sudo reboot
           

(2) 檢查 Device Mapper

Device Mapper在2.6.9核心中就引入了。檢視方法有兩種:

$ ls -l /sys/class/misc/device-mapper
lrwxrwxrwx 1 root root 0  1月 25 21:30 /sys/class/misc/device-mapper -> ../../devices/virtual/misc/device-mapper

$ sudo grep device-mapper /proc/devices  # 如果沒有,可以通過 sudo modprobe dm_mod 
252 device-mapper
           

(3) 檢查cgroup和namespace

這兩個從2.6版本就開始內建在了linux核心中。我們經常會碰到 docker,lxc,cgroup,namespace幾個在一起,這幾個具體什麼關系呢。

  • docker(go 語言實作)是 lxc 的管理器
  • lxc 是 cgroup的管理工具
  • cgroup 是namespace 的使用者空間的接口

安裝docker

使用docker團隊的DEB軟體倉庫來安裝docker。是以對于ubuntu需要添加APT倉庫。

$ sudo sh -c "echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
           

ps: 新裝的ubuntu系統的預設源都是使用

/etc/apt/sources.list

這個檔案,而這個檔案裡預設的都是國外的網站,所有有時候會遇到下載下傳軟體很慢的情況,這時候隻要更開一下這個檔案,将檔案裡面的源換成國内的網站,比如163等。

接下來添加docker倉庫的GPG秘鑰。

$ curl -s https://get.docker.io/gpg | sudo apt-key add -
           

在ubunt系統中,用gpg可以實作加密和解密。在這裡,我們添加新的源,需要下載下傳一個加密檔案,否則無法下載下傳。

apt-key add filename

Add a new key to the list of trusted keys. The key is read from

filename, or standard input if filename is -.

apt-key是Debian軟體包的安全管理工具。每個釋出的deb包,都是通過密鑰認證的,apt-key用來管理密鑰。

$ sudo apt-get update 
$ sudo apt-get install lxc-docker 
           

完成後

$ sudo docker # 會出現help
           

docker使用

docker的常用基本指令

(1) 看docker程式是否在運作

$ sudo status docker 
docker start/running, process 3004

$ sudo stop docker
docker stop/waiting
 
$ sudo start docker
docker start/running, process 3068
           

或者

$ sudo service docker status
docker start/running, process 3068
 
$ sudo service docker stop
docker stop/waiting
 
$ sudo service docker start
docker start/running, process 3145
           

(2)看docker的基本資訊(該指令會傳回所有的容器,以及docker使用的執行驅動,存儲驅動,docker的基本配置)

$ sudo docker info
Containers: 1
Images: 6
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Dirs: 8
Execution Driver: native-0.2
Kernel Version: 3.13.0-32-generic
Operating System: Ubuntu 14.04.1 LTS
CPUs: 4
Total Memory: 3.857 GiB
Name: jackson-virtual-machine
ID: 6YTW:46CV:LZM4:HKED:6ZML:PE5V:4EJK:VG2I:I5RS:BETR:FYDN:Y6SW
Username: jacksonzhangkun
Registry: [https://index.docker.io/v1/]
WARNING: No swap limit support
           

(3)檢視機器已有的鏡像

$ sudo docker images
REPOSITORY  TAG IMAGE IDCREATED VIRTUAL SIZE
busybox latest  234ee811cd806 weeks ago 2.434 MB
           

新裝的docker上面是什麼都沒有的。

(4)搜尋鏡像

$ sudo docker search busybox
           

會有很多list出來,OFFICIAL字段下有

[OK]

說明它是官方鏡像。

(5)下載下傳鏡像

$ sudo docker pull busybox
busybox:latest: The image you are pulling has been verified

511136ea3c5a: Already exists 
df7546f9f060: Already exists 
ea13149945cb: Already exists 
Status: Image is up to date for busybox:latest
           

這個鏡像我已經下載下傳過了,是以顯示已經存在。

(6)運作一個鏡像

$ sudo docker run -t -i busybox
/ # ls
bin      etc      lib      linuxrc  mnt      proc     run      sys      usr
dev      home     lib64    media    opt      root     sbin     tmp      var
/ # 
           

其中

-i

标志保證容器的STDIN是開的,

-t

表示Docker要為新建立的容器配置設定一個僞tty終端。這樣,新建立的容器,才能提供一個互動式的shell。

ps: busybox是一個內建了一百多個最常用linux指令和工具的軟體工具箱。非常精簡,在(3)中,我們可以看到,隻有2.434 MB。

(7)看運作中的docker容器

$ sudo docker ps
           

如果要看所有目前容器的清單,如要加參數

-a

$ sudo docker ps -a 
           

如果要看最後一次運作的容器,可以

$ sudo docker ps -l
           

(8)啟動容器

$ sudo docker start (NAMES/CONTAINER ID)
           

(9) 深入容器,可以通過docker inspect,擷取更多的容器資訊

$ sudo docker inspect (NAMES/CONTAINER ID)
           

(10) 删除容器

$ sudo docker rm 3b39cfcdee2e
           

如果要全部删除,可以

$ sudo docker rm `sudo docker ps -a -q`
           

(11) 删除鏡像

$ sudo docker rmi <IMAGE ID>
           

建立一個自定義的docker容器

首先我現在這邊有個從官方下下來的一個busybox鏡像,并且我運作了一個基于該鏡像的一個容器。容器ID為:

$ sudo docker ps -q -l
1c6364f2f8b4

$ sudo docker start 1c6364f2f8b4
1c6364f2f8b4

$ sudo docker attach 1c6364f2f8b4

/ # hello
/bin/sh: hello: not found
/ #     
           

進入之後發現沒有

hello

這個指令,也是busybox這個鏡像裡面沒有這個指令。下面我們自定義一個一個簡單的

hello

指令。

/ # vi hello 
/ # sh hello 
hello world
/ # chmod +x hello 
/ # mv hello /bin/
/ # hello 
hello world
/ # exit

$ sudo docker commit 1c6364f2f8b4 busybox
082a195052cf6fb75f8cf86f467562ef94bedbab365591870fbfe127dfe91d85
           

docker commit送出的隻是建立容器的鏡像和容器目前狀态之間的差異部分。提高後我們在基于新的busybox鏡像建立容器。可以看到建立的時間變成了兩分鐘前。

$ sudo docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
busybox             latest              082a195052cf        2 minutes ago       2.434 MB

$ sudo docker run -ti busybox
/ # hello 
hello world
           

上面示範了簡單的怎麼自定義docker鏡像。

轉載于:https://www.cnblogs.com/zk47/p/4709140.html

繼續閱讀