Is it going on-------------Docker容器的安裝以及基本的操作指令

來雲朋發表於2020-11-09

什麼是Docker

docker是在linux容器中執行的開源工具,是一種輕量級的虛擬機器。docker設計的宗旨是,通過應用元件的封裝、釋出、部署、執行等生命週期的管理,達到元件級別的“一次封裝,到處執行”的目的。這裡的元件,可以看成一個完整的作業系統。

docker的應用場景:

解決開發人員和運維人員之間的協作關係,加快應用交付速度
以支援大規模的分佈系統的水平擴充套件,真正給企業開發帶來福音## docker的
能實現把需要的伺服器從騰訊雲遷移到阿里雲

優缺點:

特點Docker容器虛擬機器
啟動速度秒級分鐘級
計算能力耗損幾乎無耗損50%左右
效能接近原生弱於
系統支援量(單機)上千個幾十個
隔離性資源限制完全隔離

docker的三大核心概念:映象、容器、倉庫。

映象

Docker是建立容器的基礎類似於虛擬機器的快照。

容器

容器是從映象建立的執行例項、可以被啟動停止和刪除

倉庫

Docker是集中存放映象的地方

實操一

安裝docker容器

安裝環境:centos 7.4

yum -y install yum-utils device-mapper-persistent-data lvm2
cd /etc/yum.repos.d/
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce
systemctl start docker.service
systemctl enable docker.service
docker version		##看docker的版本資訊

下面是安裝獲取映象的過程
*************************************映象加速不需要************
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://7k2y.mirror.aliyuncs.com"]
}
EOF
************************************************************
vim /etc/sysctl.conf
net.ipv4.ip_forward=1			##優化
sysctl -p
systemctl restart network
systemctl reload docker
systemctl restart docker
docker pull nginx	##獲取nginx的映象
docker search nginx	##檢視官網上有哪些nginx的映象
[root@localhost yum.repos.d]# docker images
名稱		    便籤			映象id	          什麼時候建立的          映象大小
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              c39a868aad02        3 days ago          133MB

cat /var/lib/docker/image/overlay2/repositories.json
可以檢視具體的下載檔案資訊

知識點:獲取映象資訊新增映象標籤

docker inspect c39a868aad02	##後面跟的是映象的ID
[root@localhost overlay2]# docker tag nginx:latest nginx:webb ##為nginx新增一個新的標籤webb
[root@localhost overlay2]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              c39a868aad02        3 days ago          133MB
nginx               webb                c39a868aad02        3 days ago          133MB
[root@localhost overlay2]# docker images |grep nginx
nginx               latest              c39a868aad02        3 days ago          133MB
nginx               webb                c39a868aad02        3 days ago          133MB
[root@localhost overlay2]# docker rmi nginx:webb   ##刪除標籤
Untagged: nginx:webb
[root@localhost overlay2]# docker images |grep nginx
nginx               latest              c39a868aad02        3 days ago          133MB

注意:刪除標籤僅剩下一個的時候就不要刪了,在刪的話就會把映象刪除,另外當映象存在兩個標籤的時候不能直接通過刪id刪除映象。

知識點:映象的存出和刪除和載入

docker save -o /opt/nginx nginx:latest   ##存出映象相當備份一樣
docker rmi c39a868aad02			##刪除映象
docker load < /opt/nginx		##載入映象相當於恢復
或者用docker --input /opt/nginx		恢復

知識點:建立容器,檢視容器
創時:-i指的是標準輸入-t是讓docker分配一個偽終端

[root@localhost opt]# docker create -it nginx:latest /bin/bash
d22ad9d458af8feec78bf6b847bfb0fc41306c0e7e019b30c9bc33379dce6014
[root@localhost opt]# docker ps -a	##-a列出所有
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
d22ad9d458af        nginx:latest        "/docker-entrypoint.…"   36 seconds ago      Created                                 serene_almeida

知識點:開啟容器,證明容器無論開不開啟都不能直接刪除映象,需先刪除容器後才能刪除映象。容器開啟時無法刪除容器

docker pull centos:7
docker create -it centos:7 /bin/bash
docker start 7e6257c9f8d8	##開啟nginx容器
[root@localhost opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
77dc8d519ae9        centos:7            "/bin/bash"              4 minutes ago       Created                                 beautiful_galois
d22ad9d458af        nginx:latest        "/docker-entrypoint.…"   10 minutes ago      Up(開啟) 5 seconds        80/tcp              serene_almeida
[root@localhost opt]# docker rmi c39a868aad02
Error response from daemon: conflict: unable to delete c39a868aad02 (cannot be forced) - image is being used by running container d22ad9d458af
[root@localhost opt]# docker rmi 7e6257c9f8d8
Error response from daemon: conflict: unable to delete 7e6257c9f8d8 (must be forced) - image is being used by stopped container 77dc8d519ae9
[root@localhost opt]# docker rm d22ad9d458af	#容器開啟時沒法刪容器
Error response from daemon: You cannot remove a running container d22ad9d458af8feec78bf6b847bfb0fc41306c0e7e019b30c9bc33379dce6014. Stop the container before attempting removal or force remove
[root@localhost opt]# docker stop d22ad9d458af
d22ad9d458af
[root@localhost opt]# docker rm d22ad9d458af
d22ad9d458af
[root@localhost opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
77dc8d519ae9        centos:7            "/bin/bash"         11 minutes ago      Created                                 beautiful_galois
[root@localhost opt]# docker rmi c39a868aad02
Untagged: nginx:latest
Deleted: sha256:c39a868aad02a383c7e490e0fc4a5b0217f667f2de764bc2755e315a5adf64a1
Deleted: sha256:1af47386c12cb24b362bd785e71058ab675fbdcbda8e7a7ba0dd10c3cff75a2a
Deleted: sha256:25ee02f9e42f8cdca7ca8bc522cb69ac4a86a55401818a13c7a01e52a81894fe
Deleted: sha256:b1d2bc3292aa3d64794f99d2885d35b454993b4af24ad350a969d27925db7ebb
Deleted: sha256:4216e20d59f9fc90bccdd14493452532806d4803a25514366de2a9c5560624d0
Deleted: sha256:d0fe97fa8b8cefdffcef1d62b65aba51a6c87b6679628a2b50fc6a7a579f764c

知識點:可以直接用一條命令建立映象,建立容器,並執行容器,但是執行後直接關閉容器 器狀態是Exited

[root@localhost opt]# docker run nginx /usr/bin/bash -c ls /
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
bb79b6b2107f: Pull complete 
5a9f1c0027a7: Pull complete 
b5c20b2b484f: Pull complete 
166a2418f7e8: Pull complete 
1966ea362d23: Pull complete 
Digest: sha256:aeade65e99e5d5e7ce162833636f692354c227ff438556e5f3ed0335b7cc2f1b
Status: Downloaded newer image for nginx:latest
/docker-entrypoint.sh: 38: exec: /usr/bin/bash: not found
[root@localhost opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
3451e8cbe989        nginx               "/docker-entrypoint.…"   7 seconds ago       Exited (127) 6 seconds ago                       jolly_shtern

上一個操作是有錯誤的,因為nginx不需要用/usr/bin/bash解決辦法如下
正常的結束結果應該是Exited(0)

[root@localhost opt]# docker run nginx /bin/bash -c ls /
[root@localhost opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
d9c6651b7506        nginx               "/docker-entrypoint.…"   22 seconds ago      Exited (0) 21 seconds ago 

知識點:測試當用run建立映象並進入死迴圈狀態時,其會一直出現hello,只有在開一個終端把容器關閉才能停止,且這時候的停止不是為正常狀態的停止

[root@localhost ~]# docker run paigeeworld/centos7 /bin/bash -c "while true;do echo hello;done"
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED              STATUS                      PORTS               NAMES
1deec0730a89        paigeeworld/centos7   "/bin/bash -c 'while…"   About a minute ago   Up About a minute                               hungry_shockley
d9c6651b7506        nginx                 "/docker-entrypoint.…"   46 minutes ago       Exited (0) 46 minutes ago                       trusting_bose
[root@localhost ~]# docker stop 1deec0730a89
1deec0730a89
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                        PORTS               NAMES
1deec0730a89        paigeeworld/centos7   "/bin/bash -c 'while…"   2 minutes ago       Exited (137) 12 seconds ago 

知識點:進入容器,退出容器,容器匯出,刪除容器,容器匯入(注意容器匯入時會生成映象不會建立容器) 批量刪除指令碼不會刪掉正在執行的容器,用這個特性可以篩選出執行的容器,刪不用容器

[root@localhost opt]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
fbc4997954bb        nginx:latest          "/docker-entrypoint.…"   12 minutes ago      Created                                 clever_wing
1deec0730a89        paigeeworld/centos7   "/bin/bash -c 'while…"   28 minutes ago      Up 11 minutes                           hungry_shockley
[root@localhost ~]# docker exec -it 1deec0730a89 /bin/bash
bash-4.2# exit
exit
[root@localhost ~]# docker export 1deec0730a89 > /opt/nginx_c
[root@localhost opt]# docker rm 1deec0730a89  ##刪除
1deec0730a89
[root@localhost opt]# mv nginx_c centos7_c	##改名
[root@localhost opt]# cat centos7_c | docker import - paigeeworld/centos7:latest
sha256:ce7bcd00ef6d7dc586183c3e88b123ee16bbe5f0d90ce8ef1d3589699e18eb88
[root@localhost opt]# docker ps -a	##檢視一下
[root@localhost opt]# docker images	##檢視建立的映象
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
paigeeworld/centos7   latest              ce7bcd00ef6d        3 minutes ago       355MB
centos                7                   09fd2f9e071b        7 minutes ago       0B
nginx                 latest              c39a868aad02        3 days ago          133MB
paigeeworld/centos7   <none>              4cbe7aa905e7        6 years ago         382MB

[root@localhost opt]# docker ps -a |awk '{print "docker rm "$1}' |bash	##批量刪除容器指令碼
Error: No such container: CONTAINER
d6dbbc749b42
fbc4997954bb
[root@localhost opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

實操二:Docker建立映象

映象:應用釋出的標準格式,支撐一個Docker容器的執行

基於已經有映象建立

將容器裡面執行的程式及執行環境打包生成新的映象
docker commit [選項] 容器ID/名稱 倉庫名稱:[標籤]
-m 說明資訊
-a 作者資訊
-p 生成過程中停止容器的執行

[root@localhost opt]# docker create -it centos:7 /bin/bash
8d5566c344a54f8c9a452009907c6c9e75b6402e65c551002c53ac34865b458d
[root@localhost opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
8d5566c344a5        centos:7            "/bin/bash"         3 seconds ago       Created                                 nice_dubinsky
[root@localhost opt]# docker commit -m "new" -a "daokes" 8d5566c344a5 daokes:test
sha256:53ba842ca996e9cc44c47c82ac3a53c5bc8a39b6ee5951cb7795912e6cd8ca9d
[root@localhost opt]# docker images |grep daokes
daokes                test                53ba842ca996        15 seconds ago      203MB

基於本地模組建立

通過匯入作業系統模板檔案生成新的映象
使用wget命令匯入為本地映象
wget http://download.openvz.org/template/precreated/debian-7.0-x86-minimal.tar.gz
匯入成功後可檢視本地映象資訊
docker images |grep new

wget https://download.openvz.org/template/precreated/debian-7.0-x86-minimal.tar.gz  ##一般先下載好後再傳到opt 在執行
[root@localhost opt]# cat debian-7.0-x86-minimal.tar.gz | docker import - daokes:new
sha256:ea6866d81fa7c08c1f7d07d1fd35ba484791d1dbd5dce53ade205e5ffa890a20
[root@localhost opt]# docker images|grep daokes
daokes              new                 ea6866d81fa7        2 minutes ago       215MB

基於Dockerfile建立

Dockerfile是由一組指令組成的檔案
Dockerfile結構四部分
基於映象資訊
維護者資訊
映象操作指令
容器啟動時執行指令
Dockerfile每行支援一條指令,每條指令可攜帶多個引數,支援使用以“#”號開頭的註釋
注意:每建立一個製作映象的時候中間會生成容器,執行的時候在一步步的刪除後 在建立最後生成的才是你需要的 也就是說建立過程中會產生一個臨時的容器,建立完後會被remove掉,並且保留你中間出錯的指令。
先刪除所有的映象

mkdir apache
cd apache/
vim Dockerfile 
FROM centos:7	##基於的基礎映象
MAINTAINER lai	##維護映象的使用者資訊
RUN yum -y update	##靜像超作指令安裝Apache
RUN yum -y install httpd
EXPOSE 80		##開80埠
ADD index.html /var/www/html/index.html	##複製網站首頁檔案
ADD run.sh /run.sh	##將執行指令碼複製到映象中
RUN chmod 755 /run.sh	
CMD [ "/run.sh" ]	##啟動容器時執行指令碼

vim index.html
<h1>this is my http</h1>

vim run.sh
#!/bin/bash
rm -rf /run/httpd/*
exec /usr/sbin/apachectl -D FOREGROUND

docker build -t httpd:centos .
Successfully built 9c1babec53b9
Successfully tagged httpd:centos
[root@localhost apache]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               centos              9c1babec53b9        13 seconds ago      419MB
centos              7                   7e6257c9f8d8        3 months ago        203MB
[root@localhost apache]# docker run -d -p 1216:80 httpd:centos
081c5795f2e75875ed1ebd00f5bb57c82cce6f616e2df102af433e9795ca53fa
開啟對外的對映在瀏覽器上測試

在這裡插入圖片描述

關鍵字

docker images ---------------------------------------------------------看所有映象
docker rmi nginx:web-------------------------------------------------刪標籤
docker save -o /opt/nginx nginx:latest -------------------------- 載出
docker load < /opt/nginx ---------------------------------------------載入
docker create -it nginx:latest /bin/bash ---------------------------創容器
docker ps -a ------------------------------------------------------------看容器
docker run nginx /bin/bash -c ls / ---------------------------------- 一鍵部署測試
docker exec -it 1deec0730a89 /bin/bash ------------------------入容器
exit
docker export 1deec0730a89 > /opt/nginx_c ------------------容器載出
docker ps -a |awk ‘{print "docker rm "$1}’ -----------------------批量刪容器
docker commit -m “new” -a “daokes” -----------------------------基於已有映象的
cat debian-7.0-x86-minimal.tar.gz | docker import - daokes:new 基於本地模組

相關文章