Docker:網絡及數據卷設置 [四]

1、Docker網絡設置

 默認狀況下,docker會建立一個橋接網卡[docker 0],docker有2種映射方式,一種是隨機映射,一種是指定映射html

提示:生產場景通常不使用隨機映射,可是隨機映射的好處就是由docker分配,端口不會衝突node

案例1:使用docker啓動nginx隨機映射配置

[root@docker1 ~]# docker run -d -P nginx
63cbe30165c8fb2ce7789a8173db6f2060705028ce6c326d1f0cd467bdee583c
[root@docker1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
63cbe30165c8 nginx "nginx -g 'daemon off" 8 seconds ago Up 7 seconds 0.0.0.0:32768->80/tcp, 0.0.0.0:32768->443/tcp drunk_bhabha
[root@docker1 ~]#
提示:物理機的32768被映射到80端口
-P表明隨機映射

訪問 
mysql

 


咱們能夠查看咱們的端口nginx

[root@docker1 ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 19995/mysqld
tcp 0 0 0.0.0.0:4369 0.0.0.0:* LISTEN 21574/epmd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1094/sshd
tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 21557/beam
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1372/master
tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 21557/beam
tcp6 0 0 :::4369 :::* LISTEN 21574/epmd
tcp6 0 0 :::22 :::* LISTEN 1094/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1372/master
tcp6 0 0 :::32768 :::* LISTEN 55956/docker-proxy
tcp6 0 0 :::32768 :::* LISTEN 55963/docker-proxy
tcp6 0 0 :::5672 :::* LISTEN 21557/beam
udp 0 0 0.0.0.0:123 0.0.0.0:* 19389/chronyd
udp 0 0 127.0.0.1:323 0.0.0.0:* 19389/chronyd
udp6 0 0 ::1:323 :::* 19389/chronyd

咱們能夠看一下iptables查看Nat表sql

[root@docker1 ~]# iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 2 packets, 473 bytes)
pkts bytes target prot opt in out source destination
13 664 DOCKER all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
Chain INPUT (policy ACCEPT 2 packets, 473 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 1 packets, 76 bytes)
pkts bytes target prot opt in out source destination
0 0 DOCKER all -- * * 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT 11 packets, 584 bytes)
pkts bytes target prot opt in out source destination
0 0 MASQUERADE all -- * !docker0 172.17.0.0/16 0.0.0.0/0
0 0 MASQUERADE tcp -- * * 172.17.0.2 172.17.0.2 tcp dpt:443
0 0 MASQUERADE tcp -- * * 172.17.0.2 172.17.0.2 tcp dpt:80
Chain DOCKER (2 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all -- docker0 * 0.0.0.0/0 0.0.0.0/0
9 456 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:32768 to:172.17.0.2:443
1 52 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:32768 to:172.17.0.2:80

進入容器docker

[root@docker1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
63cbe30165c8 nginx "nginx -g 'daemon off" 6 minutes ago Up 6 minutes 0.0.0.0:32768->80/tcp, 0.0.0.0:32768->443/tcp drunk_bhabha
[root@docker1 ~]# ./docker_in.sh 63cbe30165c8
提示:腳本能夠查看咱們基礎介紹

提示:由於咱們沒有給容器命名,因此須要使用ID進行進入centos

docker運行的第一進程的pid爲1bash

root@63cbe30165c8:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 31752 2872 ? Ss 22:53 0:00 nginx: master process nginx -g daemon off;
nginx 6 0.0 0.1 32144 1908 ? S 22:53 0:00 nginx: worker process
root 7 0.0 0.1 20252 1916 ? S 23:01 0:00 -bash
root 11 0.0 0.0 17492 1156 ? R+ 23:01 0:00 ps aux

docker默認會使用dhcp分配IP地址,可是咱們若是想訪問到須要設置iptables網絡

root@63cbe30165c8:/# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
22: eth0@if23: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe11:2/64 scope link
valid_lft forever preferred_lft forever

咱們可使用docker logs [ID/Name]來查看日誌app

[root@docker1 ~]# docker logs 63cbe30165c8
192.168.56.1 - - [19/Oct/2016:22:56:00 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0" "-"
192.168.56.1 - - [19/Oct/2016:22:56:00 +0000] "GET /favicon.ico HTTP/1.1" 404 169 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0" "-"
2016/10/19 22:56:00 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.56.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "92.168.228.134:32768"
192.168.56.1 - - [19/Oct/2016:22:56:00 +0000] "GET /favicon.ico HTTP/1.1" 404 169 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0" "-"
2016/10/19 22:56:00 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.56.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "92.168.228.134:32768"

案例2:使用docker啓動一個nginx,指定81端口

[root@docker1 ~]# docker run -d -p 92.168.228.134:81:80 --name mynginx nginx
65439bce352e712597f5a74dcf6c9978458fd02e6ed0f243fe87cad8c690d855
[root@docker1 ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65439bce352e nginx "nginx -g 'daemon off" 10 seconds ago Up 9 seconds 443/tcp, 92.168.228.134:81->80/tcp mynginx
提示: -d表明後臺運行 -p 指定端口 --name 指定名稱

訪問 



經過端口映射,咱們就能夠很是方便的去訪問到容器內部的服務

案例3:docker 啓動映射多端口

[root@docker1 ~]# docker run -d -p 443:443 -p 82:80 --name nginxv2 nginx
865aed79c5ec875c5d7a033267c9d124324201f92d66382a152d1743533a586b
[root@docker1 ~]# docker port nginxv2
443/tcp -> 0.0.0.0:443
80/tcp -> 0.0.0.0:82

2、Docker數據管理

Docker數據分爲兩種:

 
數據卷: -v /data
-v src:dst
數據卷容器: --volumes-from

  能夠將數據卷理解爲掛載,能夠將數據mount到docker鏡像中,讓我在docker鏡像裏寫數據時,實際寫入的是物理主機裏面。若是容器多了,不方便管

數據卷設置

案例:咱們建立一個容器,起名叫nginx-volume-test 掛載到容器中的/data目錄下

root@docker1 ~]# docker run -d --name nginx-volume-time -v /data nginx

進入容器,目錄下已經有咱們data目錄

[root@docker1 ~]# ./docker_in.sh nginx-volume-time
root@e3356d281a09:/# ls /
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

咱們能夠查看當前掛載目錄

root@e3356d281a09:/# mount
/dev/mapper/docker-253:0-134820964-eacae32f571b8595b10bac6c082732e75497540886e8c3ac72baa62d493a973e on / type xfs (rw,relatime,nouuid,attr2,inode64,logbsize=64k,sunit=128,swidth=128,noquota)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev type tmpfs (rw,nosuid,mode=755)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666)
mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (ro,nosuid,nodev,noexec,relatime)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,relatime,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (ro,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
cgroup on /sys/fs/cgroup/freezer type cgroup (ro,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/perf_event type cgroup (ro,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/cpuacct,cpu type cgroup (ro,nosuid,nodev,noexec,relatime,cpuacct,cpu)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (ro,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/blkio type cgroup (ro,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/devices type cgroup (ro,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/memory type cgroup (ro,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/net_cls type cgroup (ro,nosuid,nodev,noexec,relatime,net_cls)
cgroup on /sys/fs/cgroup/cpuset type cgroup (ro,nosuid,nodev,noexec,relatime,cpuset)
/dev/mapper/centos-root on /data type xfs (rw,relatime,attr2,inode64,noquota)
/dev/mapper/centos-root on /run/secrets type xfs (rw,relatime,attr2,inode64,noquota)
/dev/mapper/centos-root on /etc/resolv.conf type xfs (rw,relatime,attr2,inode64,noquota)
/dev/mapper/centos-root on /etc/hostname type xfs (rw,relatime,attr2,inode64,noquota)
/dev/mapper/centos-root on /etc/hosts type xfs (rw,relatime,attr2,inode64,noquota)
shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k)
proc on /proc/asound type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/bus type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/fs type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/irq type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/sys type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/sysrq-trigger type proc (ro,nosuid,nodev,noexec,relatime)
tmpfs on /proc/kcore type tmpfs (rw,nosuid,mode=755)
tmpfs on /proc/timer_stats type tmpfs (rw,nosuid,mode=755)

提示:雖然咱們不知道mount到哪裏,可是在卻有date目錄 
/dev/mapper/centos-root on /data type xfs (rw,relatime,attr2,inode64,noquota)

root@e3356d281a09:/# ls
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr va

咱們能夠經過docker inspect來查看mount的狀況

[root@docker1 ~]# docker inspect -f {{.Mounts}} nginx-volume-time
[{a6310b873c068834dd267a94c9a8173b8a6b21f2251066218ba3deb819705d36 /var/lib/docker/volumes/a6310b873c068834dd267a94c9a8173b8a6b21f2251066218ba3deb819705d36/_data /data local true }]

mount掛載就是咱們下面路徑下的內容

[root@docker1 _data]# pwd
/var/lib/docker/volumes/a6310b873c068834dd267a94c9a8173b8a6b21f2251066218ba3deb819705d36/_data

咱們能夠在這個文件下建立一個文件,而後在進容器中進行查看

[root@docker1 _data]# touch luoahong.txt
[root@docker1 _data]#
---------------------容器分割線-------------------------------
root@e3356d281a09:/# ls /data/
luoahong.txt

咱們還可使用-v指定一個卷用來做爲存儲 
咱們能夠建立一個目錄,用來指定。

[root@docker1 _data]# mkdir -p /data/docker-volume-nginx
[root@docker1 _data]# docker run -d --name nginx-volume-test2 -v /data/docker-volume-nginx/:/data nginx
b2bef96c8330583b555b576134e3f32db02050eb2bd3bb09defcfe89d4b72862
提示:-v /data/docker-volume-nginx 存儲文件的目錄
   data 容器中的目錄

查看

[root@docker1 _data]# echo "123" >>/data/docker-volume-nginx/1.txt
root@b2bef96c8330:/# cat /data/1.txt
123

提示:這種方法生產上會比較實用一些,可是這種方式dockerfile裏不支持.由於這種方法不可移植

咱們能夠添加幾個參數

[root@docker1 _data]# docker run -d --name nginx-volume-test2 -v /data/docker-volume-nginx/:/data:ro nginx
# 添加ro表明只讀,可使用只讀的方式來掛載

容器還能夠掛載文件

例如:咱們掛載一個.bash_history,掛載完並刪除

[root@docker1 ~]# docker run --rm -it -v /root/.bash_history:/.bash_history nginx /bin/bash
root@63ed2c5eef1c:/# head .bash_history
ifcon
如今若是咱們退出這個容器就會關閉

3.png-60.2kB

容器數據之間同步案例:

[root@docker1 ~]# docker run -d --name nfs -v /root/nfs-data:/data nginx
30c74a55685050027316347b6de20058af3665fb87b1fd7bca14e7316e8e900e
[root@docker1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
30c74a556850 nginx "nginx -g 'daemon off" 4 seconds ago Up 4 seconds 80/tcp, 443/tcp nfs
b2bef96c8330 nginx "nginx -g 'daemon off" 2 hours ago Up 2 hours 80/tcp, 443/tcp nginx-volume-test2
e3356d281a09 nginx "nginx -g 'daemon off" 3 days ago Up 3 days 80/tcp, 443/tcp nginx-volume-time
[root@docker1 ~]# docker run --rm -it --volumes-from nfs centos /bin/bash
[root@1a29a9cdffdf /]# cd /data/
[root@1a29a9cdffdf data]# ls
[root@1a29a9cdffdf data]# touch 123456567
切換進去查看
[root@docker1 ~]# cd nfs-data/
[root@docker1 nfs-data]# ls
123456567

 


 

數據卷容器

數據卷容器就是可讓一個容器訪問另外一個容器的卷,無論這個容器是否運行均可以訪問到。 
數據卷容器簡單解釋就是:它可讓這個數據在多個容器中共享

 
[root@docker1 ~]# docker run -it --rm --name volume-test3 --volumes-from nginx-volume-test2 centos /bin/bash
--volumes-from 另外一個容器的名稱

此時咱們能夠看到根目錄下有data目錄,由於默認是沒有的

[root@f06ca39d6090 /]# ls /data/1.txt
/data/1.txt

如今咱們能夠訪問到nginx-volume-test2下面掛載的目錄,這就實現相似nfs的功能 
 提示:咱們如今若是停掉test2 同樣能夠訪問,數據卷容器還有一個好處是無論這個容器是否運行都會起做用,只要有容器在使用數據卷容器就沒法刪除

相關文章
相關標籤/搜索