- 論壇徽章:
- 18
|
學(xué)習(xí)docker前,理解以下幾個(gè)概念有助于更好的使用docker。
鏡像,容器,倉(cāng)庫(kù)。
鏡像,就是一個(gè)操作系統(tǒng)環(huán)境,里面只有你需要的幾個(gè)應(yīng)用程序,如apache,mysql,php之類(lèi),只讀模板。
容器,從鏡像創(chuàng)建的運(yùn)行實(shí)例?梢暈橐粋(gè)簡(jiǎn)易環(huán)境中和其中運(yùn)行的應(yīng)用。
倉(cāng)庫(kù),存放鏡像的地方。學(xué)過(guò)git的同學(xué)可能更容易理解。
一、安裝docker時(shí),增加第三方源epel
如果是centos7,下載并安裝這個(gè)軟件包
#wget http://mirror.hust.edu.cn/epel/b ... se-7-0.2.noarch.rpm
#rpm -ivh epel-release-7-0.2.noarch.rpm
如果是centos6,下載并安裝這個(gè)軟件包
#wget http://mirrors.hustunique.com/ep ... ease-6-8.noarch.rpm 32位
#rpm -Uvh http://mirrors.sohu.com/fedora-e ... ease-6-8.noarch.rpm 64位
#rpm -ivh epel-release-6-8.noarch.rpm
二、安裝docker
#yum install docker-io
如果之前的系統(tǒng)中存在docker這個(gè)軟件,最好先刪除掉這個(gè)包,一個(gè)老舊的包
#service docker start
#chkconfig docker on
[iyunv@localhost master]# docker -v
Docker version 1.5.0, build a8a31ef/1.5.0
三、鏡像相關(guān)操作
3.1列出本地所有的鏡像,顯示出的依次是:鏡像所屬倉(cāng)庫(kù) 標(biāo)簽名 鏡像id 創(chuàng)建日期 所占空間大小
[iyunv@localhost master]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos 7 fd44297e2ddb 6 weeks ago 215.7 MB
centos centos7 fd44297e2ddb 6 weeks ago 215.7 MB
centos latest fd44297e2ddb 6 weeks ago 215.7 MB
四、容器相關(guān)操作
4.1查看容器
[iyunv@localhost master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4304fbe22a8 centos:7 "/bin/bash" 18 minutes ago Up 17 minutes goofy_euclid
4.2 新建、查看容器
//新建,使用docker create命令新建一個(gè)docker容器,該命令新建的容器處于停止的狀態(tài)
[iyunv@localhost master]# docker create -i -t centos:centos7 /bin/bash
72096e2a30802ce5b5e504cbc904e241da3fdb7ee11b8eef989e362b8953db2c
//查看剛創(chuàng)建的容器72096e2a3080
[iyunv@localhost master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72096e2a3080 centos:7 "/bin/bash" 5 seconds ago elated_franklin
d4304fbe22a8 centos:7 "/bin/bash" 19 minutes ago Up 18 minutes goofy_euclid
4.3 啟動(dòng)新建的容器
//啟動(dòng)
[iyunv@localhost master]# docker start 72096e2a3080
72096e2a3080
//查看容器的狀態(tài)
[iyunv@localhost master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72096e2a3080 centos:7 "/bin/bash" 4 minutes ago Up 2 seconds elated_franklin
d4304fbe22a8 centos:7 "/bin/bash" 23 minutes ago Up 23 minutes goofy_euclid
4.4 進(jìn)入新建的容器
[iyunv@localhost master]# docker exec -ti d4304fbe22a8 /bin/bash
[iyunv@d4304fbe22a8 /]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
4.5 退出容器,使用命令exit或ctrl+d,用exit后不關(guān)閉容器 而是在后臺(tái)繼續(xù)運(yùn)行
[iyunv@d4304fbe22a8 /]# exit
exit
[iyunv@localhost master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72096e2a3080 centos:7 "/bin/bash" 4 minutes ago Up 2 seconds elated_franklin
d4304fbe22a8 centos:7 "/bin/bash" 23 minutes ago Up 23 minutes goofy_euclid
4.6 停止新建的容器
//停止
[iyunv@localhost master]# docker stop 72096e2a3080
72096e2a3080
//重新啟動(dòng)
[iyunv@localhost master]# docker restart 72096e2a3080
//查看
[iyunv@localhost master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72096e2a3080 centos:7 "/bin/bash" 35 minutes ago Exited (137) 43 seconds ago elated_franklin
d4304fbe22a8 centos:7 "/bin/bash" 55 minutes ago Exited (0) 7 minutes ago goofy_euclid
[iyunv@localhost master]#
4.7創(chuàng)建守護(hù)式容器
守護(hù)式容器:沒(méi)有交互式會(huì)話,非常適合運(yùn)行應(yīng)用程序和服務(wù)。
//更多的時(shí)候,需要讓docker容器在后臺(tái)以守護(hù)態(tài)形式運(yùn)行。用戶可以通過(guò)添加-d參數(shù)來(lái)添加,
[iyunv@localhost master]# docker run --name daemon -d centos:centos7 /bin/bash -c "while true;do echo hello world;sleep 1; done"
07369b21b6c0130b12af0ca2c689e5cd08181422475457d3e17519565159c818
[iyunv@localhost master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07369b21b6c0 centos:7 "/bin/bash -c 'while 7 seconds ago Up 5 seconds daemon
//此時(shí)用docker run 只會(huì)返回一個(gè)容器id,必須用docker attach才能附著到新的shell會(huì)話,附著到容器會(huì)話,顯示他一直在打印hello world
[iyunv@localhost master]# docker attach 07369b21b6c0
hello world
hello world
hello world
4.8停止守護(hù)式容器
[iyunv@localhost master]# docker stop 07369b21b6c0
4.9在容器內(nèi)運(yùn)行進(jìn)程
我們可以通過(guò)docker exec命令在容器內(nèi)部額外啟動(dòng)新進(jìn)程?梢栽谌萜鲀(nèi)創(chuàng)建的進(jìn)程有兩種類(lèi)型:后臺(tái)任務(wù)和交互式任務(wù)
//后臺(tái)任務(wù)在容器內(nèi)運(yùn)行,且沒(méi)有交互需求,交互式任務(wù)則保持在前臺(tái)運(yùn)行。
[iyunv@localhost master]#docker exec -d daemon touch /tmp/test
-d: 表明需要運(yùn)行一個(gè)后臺(tái)進(jìn)程。
//驗(yàn)證
[iyunv@localhost master]#docker exec -t -i daemon /bin/bash
root@2d39e9e78ae0:/# cd /tmp/
root@2d39e9e78ae0:/tmp# ls
test
4.10刪除停止?fàn)顟B(tài)的容器
[iyunv@localhost master]# docker rm d4304fbe22a8
d4304fbe22a8
[iyunv@localhost master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
719f071a730d centos:7 "/bin/bash -c 'while 46 minutes ago Exited (0) 2 minutes ago daemon
c43565344cb7 centos:7 "/bin/echo hello" 53 minutes ago Exited (0) 53 minutes ago prickly_galileo
07369b21b6c0 centos:7 "/bin/bash -c 'while About an hour ago Exited (137) 47 minutes ago clever_torvalds
3cd2fa08c942 centos:7 "/bin/bash" About an hour ago Exited (0) 26 minutes ago ecstatic_pare
b8db23a1c3e6 centos:7 "/bin/bash" About an hour ago Exited (0) About an hour ago happy_jones
72096e2a3080 centos:7 "/bin/bash" 2 hours ago Exited (137) About an hour ago elated_franklin
4.11刪除運(yùn)行中的容器
[iyunv@localhost master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
719f071a730d centos:7 "/bin/bash -c 'while 55 minutes ago Exited (0) 12 minutes ago daemon
c43565344cb7 centos:7 "/bin/echo hello" About an hour ago Exited (0) 8 minutes ago prickly_galileo
07369b21b6c0 centos:7 "/bin/bash -c 'while About an hour ago Exited (137) 56 minutes ago clever_torvalds
3cd2fa08c942 centos:7 "/bin/bash" About an hour ago Exited (0) 36 minutes ago ecstatic_pare
b8db23a1c3e6 centos:7 "/bin/bash" About an hour ago Exited (0) About an hour ago happy_jones
72096e2a3080 centos:7 "/bin/bash" 2 hours ago Up 12 seconds elated_franklin
[iyunv@localhost master]# docker rm -f 72096e2a3080
72096e2a3080
[iyunv@localhost master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
719f071a730d centos:7 "/bin/bash -c 'while About an hour ago Exited (0) 16 minutes ago daemon
c43565344cb7 centos:7 "/bin/echo hello" About an hour ago Exited (0) 13 minutes ago prickly_galileo
07369b21b6c0 centos:7 "/bin/bash -c 'while About an hour ago Exited (137) About an hour ago clever_torvalds
3cd2fa08c942 centos:7 "/bin/bash" About an hour ago Exited (0) 40 minutes ago ecstatic_pare
b8db23a1c3e6 centos:7 "/bin/bash" About an hour ago Exited (0) About an hour ago happy_jones
4.12導(dǎo)出和導(dǎo)入容器
//docker export命令導(dǎo)出
[iyunv@localhost master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
719f071a730d centos:7 "/bin/bash -c 'while About an hour ago Exited (0) 16 minutes ago daemon
c43565344cb7 centos:7 "/bin/echo hello" About an hour ago Exited (0) 13 minutes ago prickly_galileo
07369b21b6c0 centos:7 "/bin/bash -c 'while About an hour ago Exited (137) About an hour ago clever_torvalds
3cd2fa08c942 centos:7 "/bin/bash" About an hour ago Exited (0) 40 minutes ago ecstatic_pare
b8db23a1c3e6 centos:7 "/bin/bash" About an hour ago Exited (0) About an hour ago happy_jones
[iyunv@localhost master]# docker export b8db23a1c3e6 > test.tar
[iyunv@localhost master]# ls
rc.tomcat.tar.gz software test.tar ttp_admin-150525.tar.gz www.ttpai.cn-150525.tar.gz
//docker import命令導(dǎo)入,成為鏡像
[iyunv@localhost master]# cat test.tar | docker import - test/centos:v1
fc9df930147ad57f2ea516748375864a612dc6fe645f0e32f5992ac19088adf6
[iyunv@localhost master]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
test/centos v1 fc9df930147a 15 seconds ago 215.7 MB
centos 7 fd44297e2ddb 6 weeks ago 215.7 MB
centos centos7 fd44297e2ddb 6 weeks ago 215.7 MB
centos latest fd44297e2ddb 6 weeks ago 215.7 MB |
|