docer學習筆記(一):基礎及使用
1、docker: www.docker.com github:https://github.com/docker/docker.github.io
docker由go語言所寫,基於apache 2.0協議釋出:方便移植,可以讓開發者打包應用和依賴的庫,然後釋出到linux主機
企業事先把環境部署好,打包一個模板(映象)(庫及配置檔案等),放到docker裡面,然後放到一個公共伺服器上,然後任何人可以下載下來直接使用;不需要在部署上花費更多的時間了;
基於linux kernel,docker從1.3x開始,版本分為社群版ce和企業版ee,當前穩定版為1.79; 也可以win上直接安裝;
社群版包括stale和edge兩種釋出形式;
docker和傳統的虛擬化比較:它們底層都是硬體伺服器,然後在服務裡安裝 linux系統,然後再上層則不同;
傳統虛擬化:在linux系統裡再安裝linux主機,然後在主機裡跑各種服務;就想vmware 和 kvm 軟體;
docker虛擬化:在linux系統裡安裝docker服務,然後就可以啟動容器了;然後直接拉一個映象下來直接啟動;啟動非常快,一個伺服器可以跑上千個容器;
傳統虛擬化層如下:分鐘級啟動;
bins/libs 上面跑 app A app B
Guset linux主機(作業系統)
hypervisor kvm服務管理器
server 硬體伺服器
host os linux系統
docker 虛擬化:啟動非常快,一個docker可以跑上千個服務;
bins/libs 上面跑 app A app B
docker engine
host os linux系統
server 硬體伺服器
docker的優勢:
啟動非常快,秒級啟動;
資源利用率高,一臺高配置伺服器可以跑上千個容器;
方便移植;可以在任意地方執行;
核心基本的虛擬化,不需要額外的hypevisor支援,有更改的效能和效率,易遷移,平臺依賴性不強;
2、映象和容器和區別:
映象:是一個只讀的模板,類似於安裝系統用到那個iso的映象檔案,通過映象來完成各種應用的部署;
容器:映象類似於作業系統,而容器就是虛擬機器本身,它可以啟動\停止、刪除等操作;每個容器是相互隔離的;
倉庫:存放映象的一個場所,倉庫分為公共倉庫和私有倉庫,
docker 的安裝:
下載docker源:curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo
yum安裝: yum install -y docker-ce 安裝社群版
當然因為軟體在國外,如果下載慢,也可以手動下載下來,然後在通過rpm來安裝;如下:
wget https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/25docker/docker-ce-17.09.0.ce-1.el7.centos.x86_64.rpm
rpm -ivh docker-ce-17.09.0.ce-1.el7.centos.x86_64.rpm
啟動: systemctl start docker
yum list |grep docker #其中18.09 表示18年9月3日;
docker.x86_64 2:1.13.1-91.git07f3374.el7.centos
docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable
安裝程式碼:
[root@localhost_001 src]# curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2424 100 2424 0 0 1785 0 0:00:01 0:00:01 --:--:-- 1786
[root@localhost_001 src]# ls /etc/yum.repos.d/docker.repo
/etc/yum.repos.d/docker.repo
[root@fenye2019 ~]# yum list |grep docker
cockpit-docker.x86_64 176-4.el7.centos extras
containerd.io.x86_64 1.2.4-3.1.el7 docker-ce-stable
docker.x86_64 2:1.13.1-91.git07f3374.el7.centos
docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable
docker-ce-cli.x86_64 1:18.09.3-3.el7 docker-ce-stable
docker-ce-selinux.noarch 17.03.3.ce-1.el7 docker-ce-stable
[root@localhost_001 src]# yum install -y docker-ce
[root@localhost_001 ~]# ps aux |grep docker
root 1562 2.9 5.0 388248 50280 ? Ssl 22:15 0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root 1704 0.0 0.0 112720 972 pts/0 S+ 22:15 0:00 grep --color=auto docker
註釋:docker每次啟動會幫我生成一些規則,但是不會自動儲存; 其實儲存或者不儲存都都可以;
service iptables save 儲存這些規則;
有時候如果刪除這些規則,docker會出現問題,然後就需要手動重啟docker,一般不建議去修改這些規則;
[root@localhost_001 ~]# iptables -t filter -nvL
Chain INPUT (policy ACCEPT 133 packets, 12364 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DOCKER-USER all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 DOCKER-ISOLATION-STAGE-1 all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- * docker0 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
0 0 DOCKER all -- * docker0 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- docker0 !docker0 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- docker0 docker0 0.0.0.0/0 0.0.0.0/0
3、映象管理:
直接拉取一個映象: docker pull centos
由於再國外,下載會比較慢:所有可以用到加速器連線:使用阿里雲映象加速器
新增這個檔案:/etc/daemon.json
[root@localhost_001 ~]# cat /etc/docker/daemon.jsom
{
"registry-mirrors": ["https://dhq9bx4f.mirror.aliyuncs.com"]
}
註釋:入上圖的URL是不同的,是在阿里申請到後填寫到這裡的;
直接拉取一個映象: docker pull centos
[root@localhost_001 ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a02a4930cb5d: Pull complete
Status: Downloaded newer image for centos:latest
檢視映象: docker image image ID 標識它的唯一
[root@localhost_001 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 1e1148e4cc2c 3 months ago 202MB
搜尋官方有哪些映象可以拉取: docker search centos
[root@localhost_001 ~]# docker search jumpserver
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
jumpserver/jumpserver 14
jiaxiangkong/jumpserver_docker 開源跳板機(堡壘機):認證,授權,審計,自動化運維 10
jumpserver/jms_all 6
wojiushixiaobai/jumpserver Jumpserver ALL 5 [OK]
hhding/jumpserver-docker ssh proxy node 3 [OK]
njqaaa/jumpserver jumpserver 2 [OK]
zhegao/jumpserver Jumpserver 1.4.0 2
註釋:搜尋映象時,可以看到有官方的映象,還有別人自己製作好然後上傳的映象可供下載; 你也可以做好映象,然後釋出到官方上去;
給映象打標籤 docker tag centos yuanhh
[root@localhost_001 ~]# docker tag centos fenye
[root@localhost_001 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 1e1148e4cc2c 3 months ago 202MB
fenye latest 1e1148e4cc2c 3 months ago 202MB
註釋:雖然打了標籤後,但是它的 IMAGE ID 是一樣的; 標識同一個檔案;
[root@localhost_001 ~]# docker tag centos yuanhh:190312
[root@localhost_001 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 1e1148e4cc2c 3 months ago 202MB
fenye latest 1e1148e4cc2c 3 months ago 202MB
yuanhh 190312 1e1148e4cc2c 3 months ago 202MB
註釋: docker tag centos yuanhh:190312 這樣來修改tag標籤;
[root@localhost_001 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 1e1148e4cc2c 3 months ago 202MB
fenye latest 1e1148e4cc2c 3 months ago 202MB
yuanhh 190312 1e1148e4cc2c 3 months ago 202MB
yuanhh 202020 1e1148e4cc2c 3 months ago 202MB
4、啟動 一個映象: 映象啟動後就變成了容器; docker run -itd centos
-i 表示讓容器標準輸入開啟 -t 表示開啟一個偽終端 -d 表示後臺啟動
[root@localhost_001 ~]# docker run -itd centos
65ef47baa2192d53af1835568213dec90d446c450ddc4cd01ff0633ecde41672
檢視執行的容器: docker ps -a #加上 -a 表示檢視所有容器
[root@localhost_001 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65ef47baa219 centos "/bin/bash" 35 seconds ago Up 31 seconds sharp_matsumoto
[root@localhost_001 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65ef47baa219 centos "/bin/bash" 53 seconds ago Up 48 seconds sharp_matsumoto
4、刪除映象: docker rmi centos 後面的引數可以是tag,如果是tag,則表示刪除這個tag,當引數時id 時,則刪除整個映象,標籤也一起刪除;
[root@localhost_001 ~]# docker rmi yuanhh:202020
Untagged: yuanhh:202020
[root@localhost_001 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 94e814e2efa8 15 hours ago 88.9MB
centos latest 1e1148e4cc2c 3 months ago 202MB
fenye latest 1e1148e4cc2c 3 months ago 202MB
yuanhh 190312 1e1148e4cc2c 3 months ago 202MB