一、docker簡介
二、docker安裝
[root@lamp sky9890]# yum install docker
[root@lamp sky9890]# systemctl start docker.service #啟動docker服務
[root@lamp sky9890]# systemctl enable docker.service #随系統啟動自動加載
[root@lamp sky9890]# docker #檢視docker用戶端
[root@lamp sky9890]# docker -v #版本
docker version 1.13.1, build 4ef4b30/1.13.1
三、擷取鏡像
鏡像是一個模闆,可以從鏡像伺服器上擷取做好的鏡像。docker使用docker pull指令來擷取需要的鏡像。
下别分别擷取centos、ubuntu、tomcat、nginx、mysql、mongo等鏡像。
1.從docker鏡像中查找可用nginx鏡像版本擷取鏡像
[root@lamp sky9890]# docker search centos
[root@lamp sky9890]# docker search ubuntu
[root@lamp sky9890]# docker search tomcat
[root@lamp sky9890]# docker search nginx
2.從docker鏡像中下載下傳需要的版本
[root@lamp sky9890]# docker pull centos
[root@lamp sky9890]# docker pull mysql
[root@lamp sky9890]# docker pull tomcat
[root@lamp sky9890]# docker pull mysql:latest
[root@lamp sky9890]# docker pull python:3.5
[root@lamp sky9890]# docker pull mongo:latest
3.顯示本地已經下載下傳的鏡像
[root@lamp sky9890]# docker images
repository tag image id created size
docker.io/tomcat latest 4e7840b49fad 3 days ago 529 mb
docker.io/nginx latest a1523e859360 4 days ago 127 mb
docker.io/python 3.5 0320ef7199ca 4 days ago 909 mb
docker.io/mysql latest c8ad2be69a22 4 days ago 465 mb
docker.io/php latest e66ae809d99a 4 days ago 405 mb
docker.io/httpd latest c5a012f9cf45 4 days ago 165 mb
docker.io/mongo latest bcef5fd2979d 9 days ago 386 mb
docker.io/ubuntu latest 72300a873c2c 9 days ago 64.2 mb
docker.io/centos latest 470671670cac 6 weeks ago 237 mb
三、如何使用容器
1.使用 docker run 指令來在容器内運作一個應用程式。
[root@lamp sky9890]# docker run ubuntu /bin/echo "hello world"
hello world
2.運作交換式的容器
[root@lamp sky9890]# docker run -i -t ubuntu /bin/bash
root@1c7c38a6f99a:/#
root@1c7c38a6f99a:/# cat /proc/version
linux version 3.10.0-862.11.6.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (red hat 4.8.5-28) (gcc) ) #1
smp tue aug 14 21:49:04 utc 2018
root@1c7c38a6f99a:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@1c7c38a6f99a:/usr# exit
exit
3.啟動容器(背景模式)
[root@lamp sky9890]# docker run -d ubuntu /bin/sh -c "while true;do echo hello world; sleep 1; done"
1f77ab03073f192d18737110bf97ec04b72524fb34e3a570571d4cac6b4e5b31
4.檢視docker程序
[root@lamp sky9890]# docker ps
container id image command created status ports names
1f77ab03073f ubuntu "/bin/sh -c 'while..." 38 seconds ago up 36 seconds competent_ramanujan
8778ad75ade3 ubuntu "/bin/bash" 14 minutes ago up 14 minutes ubuntu-test
7757db1528de ubuntu "/bin/bash" 19 minutes ago up 15 minutes distracted_austin
輸出詳情介紹:
container id: 容器 id。
image: 使用的鏡像。
command: 啟動容器時運作的指令。
created: 容器的建立時間。
status: 容器狀态。
狀态有7種:
created(已建立)
restarting(重新開機中)
running(運作中)
removing(遷移中)
paused(暫停)
exited(停止)
dead(死亡)
ports: 容器的端口資訊和使用的連接配接類型(tcp\udp)。
names: 自動配置設定的容器名稱。
5.檢視容器的标準輸出
[root@lamp sky9890]# docker logs 1f77ab03073f
......
6.停止容器
[root@lamp sky9890]# docker stop 1f77ab03073f
1f77ab03073f
container id image command created status ports names
8778ad75ade3 ubuntu "/bin/bash" 18 minutes ago up 17 minutes ubuntu-test
7757db1528de ubuntu "/bin/bash" 22 minutes ago up 18 minutes distracted_austin
7.删除容器
[root@lamp sky9890]#docker rm -f 1f77ab03073f
四、docker執行個體安裝
1.docker安裝centos
[root@lamp sky9890]# docker run -itd --name centos-test centos
f2896d1d87c996685c7c98fb9c85241a10483fb1f2dcd843a4b0d6cab212d636
#通過exec運作容器
[root@lamp sky9890]# docker exec -it centos-test /bin/bash
[root@f2896d1d87c9 /]# exit
[root@lamp sky9890]# docker ps #安裝成功
container id image command created status ports names
f2896d1d87c9 centos "/bin/bash" about a minute ago up about a minute centos-test
8778ad75ade3 ubuntu "/bin/bash" 34 minutes ago up 34 minutes ubuntu-test
7757db1528de ubuntu "/bin/bash" 39 minutes ago up 35 minutes distracted_austin
2.docker安裝nginx
#運作容器
[root@lamp sky9890]# docker run --name nginx-test -p 8080:80 -d nginx
5f4aa091f8244b4f21a585b8426240218312b90f46dc23f8cfdc825f1a981b6e
參數說明:
--name nginx-test:容器名稱。
-p 8080:80: 端口進行映射,将本地 8080 端口映射到容器内部的 80 端口。
-d nginx: 設定容器在在背景一直運作。
測試
五、在容器中的相關操作
1.啟動centos容器
[root@lamp sky9890]# docker run -it centos /bin/bash
[root@b90d59c23a38 /]# lsb_release
bash: lsb_release: command not found
[root@b90d59c23a38 /]# yum install redhat-lsb -y
[root@b90d59c23a38 /]# lsb_release -a
lsb version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-
amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
distributor id: centos
description: centos linux release 8.1.1911 (core)
release: 8.1.1911
codename: core
2.啟動ubuntu容器
root@737d61040022:/# lsb-release -a
bash: lsb-release: command not found
root@737d61040022:/# apt-get install lsb-release
reading package lists... done
building dependency tree
reading state information... done
e: unable to locate package lsb-release
#出現上述原因,說明需要更新apt庫
root@737d61040022:/# apt-get update
e: could not get lock /var/lib/apt/lists/lock - open (11: resource temporarily unavailable)
e: unable to lock directory /var/lib/apt/lists/
#出現上述原因,是由于資源被鎖的原因,可能是上次安裝時沒正常完成,而導緻出現此狀況。需要執行下面兩條指令:
root@737d61040022:/# rm /var/cache/apt/archives/lock
root@737d61040022:/# rm /var/lib/apt/lists/lock
root@737d61040022:/# apt-get update #更新軟體清單
root@737d61040022:/# apt-get upgrade #更新軟體
.........
root@737d61040022:/# lsb_release -a
no lsb modules are available.
distributor id: ubuntu
description: ubuntu 18.04.4 lts
release: 18.04
codename: bionic
root@737d61040022:/# apt-get install lsb-core
六、解決docker pull 速度慢的問題
将docker鏡像源修改為國内的阿裡雲:
#vi /etc/docker/daemon.json 檔案中添加以下參數(沒有該檔案則建立):
{
"registry-mirrors": ["https://9cpn8tt6.mirror.aliyuncs.com"]
}
#systemctl daemon-reload
#systemctl restart docker