- 数据卷容器
- 创建nginx容器,提供网站和配置文件
- 利用数据卷容器迁移数据
如果用户需要在容器之间共享一些持续更新的数据,最简单的方式是使用数据卷容器。数据卷容器其实就是一个普通的容器,专门用它提供数据卷供其他容器挂载,使用方法如下:
- 创建一个数据卷容器dbdata,并在其中创建一个数据卷挂载到/dbdata
[root@localhost ~]# docker run -dit --name dbdata -v /data busybox ae5b45f951b678c219902e204a2a4674d6ec0a220878df131c73cb4892ae564b
- 然后可以在其他容器中使用–volumes-from来挂载dbdata容器中的数据卷,例如创建db1和db2两个容器,并从dbdata容器挂载数据卷:
[root@localhost ~]# docker run -it --name db1 --volumes-from dbdata busybox /bin/sh / # ls bin dev home root tmp var data etc proc sys usr //含有data目录 [root@localhost ~]# docker run -it --name db2 --volumes-from dbdata busybox /bin/sh / # ls bin dev home root tmp var data etc proc sys usr //含有data目录
注意:此时,容器db1和db2都挂载同一个数据卷到相同的/dbdata目录。三个容器任何一方在该目录下的写入,其他容器都可以看到。
//再db2容器内创建文件test /data # touch test /data # ls test //再db1容器内查看 / # cd data/ /data # ls test
使用–volumes-from参数所挂载数据卷的容器自身并不需要保持在运行状态
如果删除了挂载的容器(包括dbdata、db1和db2),数据卷并不会被自动删除。如果要删除一个数据卷,必须在删除最后一个还挂载着它的容器时显式使用docker rm -v命令来指定同时删除关联的容器。
创建nginx容器,提供网站和配置文件- 创建网站和配置文件防止目录
[root@localhost ~]# mkdir -p /var/www/html [root@localhost ~]# mkdir /config //把网站放入目录下 [root@localhost html]# pwd /var/www/html [root@localhost html]# ls index.html 中国社会公益网_files //配置文件放入/config下 [root@localhost config]# pwd /config [root@localhost config]# ls conf.d mime.types.default default.d nginx.conf fastcgi.conf nginx.conf.default fastcgi.conf.default scgi_params fastcgi_params scgi_params.default fastcgi_params.default uwsgi_params koi-utf uwsgi_params.default koi-win win-utf mime.types
- 创建网站数据卷容器
[root@localhost ~]# docker run --name html -v /var/www/html/:/usr/share/nginx/html busybox [root@localhost ~]# docker ps -a ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 44c27b2cfd09 busybox "sh" 12 seconds ago Exited (0) 11 seconds ago html //创建一个容器测试数据卷是否可以正常使用 [root@localhost ~]# docker run -it --rm --volumes-from html busybox / # ls bin etc proc sys usr dev home root tmp var / # ls /usr/share/nginx/html/ index.html ???????_files //看到网站内容表示数据卷容器可以正常使用
- 创建配置文件数据卷
[root@localhost ~]# docker run --volumes-from html --name nginx_conf -v /config:/etc/nginx busybox [root@localhost ~]# docker ps -a ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 03fd9590f3bc busybox "sh" 9 seconds ago Exited (0) 8 seconds ago nginx_conf 44c27b2cfd09 busybox "sh" 7 minutes ago Exited (0) 7 minutes ago html //创建一个容器测试数据卷容器是否可以正常使用 [root@localhost ~]# docker run -it --rm --volumes-from nginx_conf busybox / # ls /etc/nginx/ conf.d mime.types.default default.d nginx.conf fastcgi.conf nginx.conf.default fastcgi.conf.default scgi_params fastcgi_params scgi_params.default fastcgi_params.default uwsgi_params koi-utf uwsgi_params.default koi-win win-utf mime.types / # ls /usr/share/nginx/html/ index.html ???????_files
注意:此时配置文件的数据卷容器既含有网站内容,又含有配置文件内容
- 使用数据卷容器开启一个nginx容器
[root@localhost ~]# docker run -d --name web --volumes-from nginx_conf -P nginx [root@localhost ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 86fb553821bf nginx "/docker-entrypoint.…" 46 seconds ago Up 44 seconds 0.0.0.0:49153->80/tcp, :::49153->80/tcp web
- 网站访问
- 修改配置文件
再宿主机上修改配置文件,生成两个网站,测试一下
[root@localhost config]# vim nginx.conf
......
server {
listen 8080;
server_name web.example.com;
location / {
root /usr/share/nginx/html/web;
index index.html;
}
}
server {
listen 80;
server_name test.example.com;
root /usr/share/nginx/html/test;
......
[root@localhost html]# pwd
/var/www/html
[root@localhost html]# ls
test web
[root@localhost html]# cd test/
[root@localhost test]# ls
index.html
[root@localhost test]# cd ../web/
[root@localhost web]# ls
index.html 中国社会公益网_files
//查看容器内配置文件是否更改了
root@86fb553821bf:/# vi /etc/nginx/nginx.conf
......
server {
listen 8080;
server_name web.example.com;
location / {
root /usr/share/nginx/html/web;
index index.html;
}
}
server {
listen 80;
server_name test.example.com;
root /usr/share/nginx/html/test;
......
- 重新映射端口
因为之前创建nginx容器时使用的-P选项,当时没有8080端口,现在更改了配置文件,有两个端口需要映射
[root@localhost ~]# docker run -d --name web --volumes-from nginx_conf -p 80:80 -p 8080:8080 --rm nginx 35d11cee89e66213c7fde2b29984590db4dd44b34e8117e07446b7a3128afe80 [root@localhost ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 35d11cee89e6 nginx "/docker-entrypoint.…" 21 seconds ago Up 19 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp web
- 网页访问
- –restart always
我们在启动容器时,如果重启docker或者重启主机,本来在运行的容器都将会停止,所以,在运行容器时,我们可以加上 --restart always,使用此参数,不管我们是重启docker服务还是重启主机,容器都将会运行,如果是重启主机前提docker服务是开机自启的
[root@localhost ~]# docker run -d --name web --volumes-from nginx_conf -p 80:80 -p 8080:8080 --restart always nginx afa76a87716dfea932bafbfea99c45e275c59abb2a4ad6e1b967e13758f168d5 [root@localhost ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES afa76a87716d nginx "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp web //重启docker [root@localhost ~]# systemctl restart docker [root@localhost ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES afa76a87716d nginx "/docker-entrypoint.…" about a minute ago Up 3 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp web利用数据卷容器迁移数据
可以利用数据卷容器对其中的数据卷进行备份、恢复,以实现数据的迁移。
- 备份
[root@localhost ~]# docker run --volumes-from web -v $(pwd):/backup centos tar zcf /backup/nginx_conf.tar.gz /etc/nginx tar: Removing leading `/' from member names [root@localhost ~]# ll total 12 -rw-------. 1 root root 1169 Jul 10 20:37 anaconda-ks.cfg -rw-r--r--. 1 root root 6090 Dec 7 20:01 nginx_conf.tar.gz [root@localhost ~]# tar xf nginx_conf.tar.gz [root@localhost ~]# ls anaconda-ks.cfg etc nginx_conf.tar.gz [root@localhost ~]# ls etc/nginx/ conf.d koi-utf scgi_params default.d koi-win scgi_params.default fastcgi.conf mime.types uwsgi_params fastcgi.conf.default mime.types.default uwsgi_params.default fastcgi_params nginx.conf win-utf fastcgi_params.default nginx.conf.default
首先利用centos镜像创建了一个容器。使用–volumes-from web参数来让容器挂载web容器的数据卷(即web数据卷);使用-v $(pwd):/backup参数来挂载本地的当前目录到容器的/backup目录。
容器启动后,使用了tar zcf /backup/nginx_conf.tar.gz /etc/nginx命令来将/etc/nginx下内容备份为容器内的/backup/nginx_conf.tar.gz,即宿主主机当前目录下的nginx_conf.tar.gz。
- 恢复
//开启一个容器,此时目录下为空 [root@localhost ~]# docker run -it --name test --rm -v /etc/nginx busybox / # ls /etc/nginx/ / # //恢复数据 [root@localhost ~]# docker run --volumes-from test -v $(pwd):/backup centos tar xf /backup/nginx_conf.tar.gz //test容器查看内容 / # ls /etc/nginx/ conf.d koi-utf scgi_params default.d koi-win scgi_params.default fastcgi.conf mime.types uwsgi_params fastcgi.conf.default mime.types.default uwsgi_params.default fastcgi_params nginx.conf win-utf fastcgi_params.default nginx.conf.default / #



