Centos 6.5 安裝和使用docker
基於本人一貫的習慣,關於“某某某是什麽”這樣的問題,請百度吧,會有更專業的人士,會比我說的更詳細更深,這裏我只給出本人親歷的安裝和使用過程。
1.安裝
先檢查服務器環境,docker要求操作系統CentOS6以上,kernel 版本必須2.6.32-431或更高,即>=CentOS 6.5,運行docker時實際提示3.8.0及以上,必須64bit,32bit不支持docker。
[root@201 ~]# uname -r
2.6.32-642.1.1.el6.x86_64
- 1
- 2
docker的存儲驅動是Device Mapper,看下你的驅動是否符合
[root@201 ~]# grep device-mapper /proc/devices
253 device-mapper
- 1
- 2
看來我們符合以上要求,然後開始安裝。
對於centos 6.5 需要先安裝源
[root@201 ~]# rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
- 1
註意centos 6要安裝的是docker-io來源於Fedora epel庫,是第三方庫,所以先要確保安裝了epel
[root@201 ~]# rpm -q epel-release
epel-release-6-8.noarch
- 1
- 2
ok,我們已經安裝過epel,然後yum一下,稍等片刻就安裝成功
[root@201 ~]# yum install docker-io -y
- 1
如果系統是centos 7則不用以上步驟,並且安裝包是docker而不是docker-io
[root@201 ~]# yum install docker -y
- 1
2.啟動
啟動守護進程
[root@201 ~]# service docker start
- 1
查看啟動信息
[[email protected]201 data]# docker info
Containers: 0
Images: 0
Storage Driver: devicemapper
Pool Name: docker-253:0-286421-pool
Pool Blocksize: 65.54 kB
Backing Filesystem: extfs
Data file: /dev/loop0
Metadata file: /dev/loop1
Data Space Used: 305.7 MB
Data Space Total: 107.4 GB
Data Space Available: 3.536 GB
Metadata Space Used: 729.1 kB
Metadata Space Total: 2.147 GB
Metadata Space Available: 2.147 GB
Udev Sync Supported: true
Deferred Removal Enabled: false
Data loop file: /var/lib/docker/devicemapper/devicemapper/data
Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
Library Version: 1.02.117-RHEL6 (2016-04-01)
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 2.6.32-642.1.1.el6.x86_64
Operating System: <unknown>
CPUs: 1
Total Memory: 996.2 MiB
Name: 201.liberalman.cn
ID: BHFO:EYCF:XVAZ:J637:2LUV:RR6Y:UR7Z:MZ43:PR4N:CGUD:7EAJ:5SBI
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
設置開機啟動
[root@201 ~]# chkconfig docker on
- 1
docker日誌路徑
/var/log/docker
- 1
3.實例測試
測試1
我們下載一個centos image
[root@201 data]# docker pull centos
latest: Pulling from centos
3690474eb5b4: Pull complete
342c399b9d0e: Pull complete
915b9d3c92fc: Downloading 23.78 MB/70.58 MB
915b9d3c92fc: Downloading 70.58 MB/70.58 MB
d5c89a2047c8: Download complete
Digest: sha256:14bc8ca808518a2703b6eff1a5f3b7065d4b5d4b388b575ae6a27db8791ab19b
Status: Downloaded newer image for centos:latest
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
列出鏡像,指定centos關鍵詞看看我們下載的鏡像
[root@201 data]# docker images centos
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos latest d5c89a2047c8 33 hours ago 196.7 MB
- 1
- 2
- 3
我們下載了一個近200M的鏡像,好,運行container
[root@201 data]# docker run -i -t centos /bin/bash
[root@f41d95a52943 /]#
- 1
- 2
-t表示傳遞給容器tty終端,-i是interactive,表示可以交互。可以看到我的終端也由[email protected] data變成了[email protected],說明進入了容器的終端,要退出輸入exit就可以。如果要新開一個終端進入我們之前啟動的容器,先查詢container id
[root@201 data]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f41d95a52943 centos "/bin/bash" 12 minutes ago Up 12 minutes backstabbing_poincare
- 1
- 2
- 3
id是f41d95a52943然後在新終端中登錄
docker exec -it f41d95a52943 /bin/bash
- 1
這樣就可以登錄了。
我們在docker中起了一個centos的最小系統,登錄到此系統[[email protected] /]後,查看下其ip地址,發現沒有ifconfig命令,使用 yum install net-tools 安裝後
[root@f41d95a52943 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:1 prefixlen 64 scopeid 0x20<link>
ether 02:42:ac:11:00:01 txqueuelen 0 (Ethernet)
RX packets 10375 bytes 12736213 (12.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5643 bytes 309098 (301.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
可以看到我們docker中的centos系統,分配了一個ip 172.17.0.1,對外ping百度等網站可以ping通!是不是到此就結束了呢,當重啟container的時候,你就會發現,ifconfig命令又沒有了,默認是不會為我們保存修改的,所以需要我們提交對image的修改。在宿主機中查詢容器id,然後提交修改
查看剛運行過的容器
[root@201 ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e108c90fa11 centos "/bin/bash" 2 minutes ago Up 2 minutes ecstatic_franklin
[root@201 ~]# docker start 4e108c90fa11
4e108c90fa11
然後連上去
[root@201 ~]# docker exec -it 4e108c90fa11 /bin/bash
安裝net-tools包
[root@4e108c90fa11 /]# yum install net-tools -y
退出,在宿主機上提交評論
[root@201 ~]# docker commit -m="add net-tools packages,nclude ifconfig command" 4e108c90fa11
129d59168480299c786d0585e9eb74fdbde3dcd00ee2cd2815d11b7749e53ac7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
好,現在重新啟動剛才的container,連上去試試ifconfig,就會發現ifconfig已經保存在image中了。
測試2
宿主機上,我們在container上運行一個web服務器
[root@201 data]# docker run -d -P training/webapp python app.py
Unable to find image ‘training/webapp:latest‘ locally
latest: Pulling from training/webapp
e9e06b06e14c: Pull complete
02a8815912ca: Download complete
Status: Downloaded newer image for training/webapp:latest
f0b7172ab3a14e6f078e7b01310a3b027e28234baf44f4f6f1aa68465e854f60
- 1
- 2
- 3
- 4
- 5
- 6
- 7
這個web應用為docker官方提供,本地沒有,所以自動去倉庫下載,很方便的實現image,code 共享,並且到處運行。
image都是分層的,所以更改後構建比較快速。
[root@201 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
272a09b3f713 training/webapp "python app.py" About a minute ago Up About a minute 0.0.0.0:32768->5000/tcp serene_jang
- 1
- 2
- 3
看到web服務器將docker內部的5000端口映射到我們宿主機的的32768端口,同時我們在宿主機上查看ip信息
[root@201 ~]# ps -ef|grep docker-proxy|grep -v "grep"
root 3163 3041 0 10:56 pts/0 00:00:00 docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 32768 -container-ip 172.17.0.1 -container-port 5000
[root@201 ~]# netstat -npl|grep 32768
tcp 0 0 :::32768 :::* LISTEN 3163/docker-proxy
[root@201 ~]# ifconfig
docker0 Link encap:Ethernet HWaddr 82:EA:95:80:A9:F3
inet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::c057:bbff:fe36:55cf/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:9 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:612 (612.0 b) TX bytes:468 (468.0 b)
eth0 Link encap:Ethernet HWaddr 08:00:27:DD:0D:82
inet addr:192.168.1.201 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fedd:d82/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:469 errors:0 dropped:0 overruns:0 frame:0
TX packets:304 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:46223 (45.1 KiB) TX bytes:67929 (66.3 KiB)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
宿主機的ip是192.168.1.201,我們訪問這個web服務器,通過宿主機上的瀏覽器訪問http://192.168.1.201:32768/,看到輸出
Hello world!
- 1
4.制作屬於自己的鏡像
突發奇想,制作一個mysql鏡像,然後在自己服務器上開一堆mysql的服務,提供不同端口來訪問,搞個集群什麽的,呵呵
首先創建一個目錄/data/mysql,並創建一個Dockerfile文件,文件內容如下
vim Dockerfile
FROM centos6:centos6
MAINTAINER liberalman "[email protected]"
RUN yum install -y mysql-server
RUN service mysqld start && mysql -e "grant all privileges on *.* to ‘socho‘@‘%‘ identified by ‘Looks137‘;"&& mysql -u root -e "show databases;"
VOLUME ["/data/volume1"]
EXPOSE 3306
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
註意:當初寫此文的時候,在線拉取的mysql鏡像還是5.6版本的,現在都成了5.7了,有讀者反映上面的配置運行不通過,因為mysql 5.7開始,密碼會隨機保存到安裝Mysql的用戶根目錄下創建一個.mysql_secret文件中,5.7.6之後更是保存到了mysqld.log中。所以目前這個配置已經不能用了,等我找到5.7之後的解決辦法再更新,抱歉!
在Dockerfile所在目錄下運行build命令來生成image文件,指定文件名mysql_test
[[email protected]201 mysql]# docker build -t mysql_test ./
...
Starting mysqld: [ OK ]
Database
information_schema
mysql
test
...
Successfully built ac45bc85be5e
創建成功,查看鏡像
[[email protected]201 ~]# docker images mysql_test
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
mysql_test latest ac45bc85be5e About a minute ago 374.3 MB
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
這會創建一個在centos6系統上安裝的mysql服務器,鏡像名稱是mysql_test,同時也會將centos6系統下載到本地來。好嘞,啟動下
[root@201 ~]# docker run --name=mysqlserver -d -P mysql_test
a903be8b69fe540395e61a6cff3db16cf944ca1177cb581f7e591247a12b0737
[root@201 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a903be8b69fe mysql_test "/usr/bin/mysqld_saf About a minute ago Up About a minute 0.0.0.0:32769->3306/tcp mysqlserver
- 1
- 2
- 3
- 4
- 5
容器內的數據庫已經啟動了,而且映射到宿主機32769端口,連接下
[[email protected]201 mysql]# mysql -hlocalhost -uroot -P 32769
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
連接正常,mysql可以用了。既然是用docker,我們就要體現它的牛逼之處,所以我要啟動2個以上的mysql容器,以後用來做數據庫集群也可以啊,哈哈
[root@201 ~]# docker run -p 3307:3306 --name=mysqlserver1 -d -P mysql_test1
8126d375d2f832a0381704424b45669178bc718598c1c8fa8868587455df6fed
[root@201 ~]# docker run -p 3308:3306 --name=mysqlserver2 -d -P mysql_test
ad920abde38b249e35600bf75f64bc44e7a007b399995e4e0fa17e7c9e5ed1a2
[root@201 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad920abde38b mysql_test "/usr/bin/mysqld_saf 21 seconds ago Up 20 seconds 0.0.0.0:3308->3306/tcp mysqlserver2
8126d375d2f8 mysql_test "/usr/bin/mysqld_saf About a minute ago Up About a minute 0.0.0.0:3307->3306/tcp mysqlserver1
a903be8b69fe mysql_test "/usr/bin/mysqld_saf 34 minutes ago Up 34 minutes 0.0.0.0:32769->3306/tcp mysqlserver
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
看吧,啟動三個容器,分別按32769、3307、3308三個不同的端口連接都OK,數據庫相互獨立不影響。
做了以上的活還不夠,如果真的要在生產環境中使用多個mysql,最好在Dockerfile文件中設置容器中mysql的datadir目錄掛載宿主機目錄下,以便將真實產生的數據寫入到宿主機中,否則容器一關閉所有的數據就丟失了,當然不同容器掛載到宿主機中的目錄一定要不同,千萬不要數據混淆了
VOLUME ["/var/lib/mysql"]
- 1
好了,到此寫了很多,先寫到這裏,接下來研究怎麽做彈性伸縮和擴容!
5.docker命令
關閉容器
- docker stop 容器名
- docker kill 容器名
殺死所有正在運行的容器
- docker kill $(docker ps -a -q)
刪除所有已經停止的容器
- docker rm $(docker ps -a
刪除所有未打 dangling 標簽的鏡像
- docker rmi $(docker images -q -f dangling=true)
通過image id刪除鏡像
- docker rmi -f
刪除所有鏡像
- docker rmi $(docker images -q)
搜索和centos相關的鏡像
[[email protected]201 data]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 2453 [OK]
jdeathe/centos-ssh CentOS-6 6.8 x86_64 / CentOS-7 7.2.1511 x8... 26 [OK]
nimmis/java-centos This is docker images of CentOS 7 with dif... 13 [OK]
million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 12 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 10 [OK]
torusware/speedus-centos Always updated official CentOS docker imag... 8 [OK]
nickistre/centos-lamp LAMP on centos setup 4 [OK]
...
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
查看運行中某個容器的進程,容器id是f41d95a52943
[root@201 data]# docker top f41d95a52943
UID PID PPID C STIME TTY TIME CMD
root 5845 5388 0 14:23 pts/2 00:00:00 /bin/bash
- 1
- 2
- 3
查詢日誌
- docker logs <容器id>
6.遇到問題
1.啟動失敗,查看日誌文件後,發現
symbol dm_task_get_info_with_deferred_remove, version Base not defined in file libdevmapper.so.1.02 with link time reference
解決辦法:執行:yum upgrade device-mapper-libs
2.Error response from daemon: Cannot start container d9bf569e3f0db33bc3d800cca4e6be1ccf7ac89e91951ba6486e3239f92516a5: [8] System error: mountpoint for cgroup not found
解決辦法:因為cgroup在宿主機上沒有掛載。
vim /etc/fstab
none /sys/fs/cgroup cgroup defaults 0 0
重啟機器,docker也重啟下即可
3.Conflict. The name “mysqlserver” is already in use by container 19a5ab1401f6
命名沖突問題,刪掉
docker rm mysqlserver
創建於 2016-07-21 杭州,更新於 2016-07-22 杭州。
該文章在以下平臺同步
- >LIBERALMAN:http://api.liberalman.cn:40000/article/54
- >CSDN:http://blog.csdn.net/socho/article/details/51999744
- >簡書:http://www.jianshu.com/p/3ffa4e5cbb86
Centos 6.5 安裝和使用docker