Docker存储卷实战(nginx)
创建nginx容器,同时提供配置文件和网页文件
//真机网站文件存放位置
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
Images index.html
[root@localhost html]#
//拉取nginx镜像
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
e5ae68f74026: Pull complete
21e0df283cd6: Pull complete
ed835de16acd: Pull complete
881ff011f1c9: Pull complete
77700c52c969: Pull complete
44be98c0fab6: Pull complete
Digest: sha256:9522864dd661dcadfd9958f9e0de192a1fdda2c162a35668ab6ac42b465f0603
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
pengyudong/httpd latest fc94b7069f41 15 hours ago 702MB
pengyudong/httpd v1 b0f43183575f 36 hours ago 713MB
nginx latest f652ca386ed1 5 days ago 141MB
busybox latest d23834f29b38 8 days ago 1.24MB
httpd latest ad17c88403e2 2 weeks ago 143MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]#
//制作一个网站映射
[root@localhost ~]# docker run -it --name html -v /var/www/html:/usr/share/nginx/html busybox
/ # exit
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
094a90e53cb8 busybox "sh" 15 seconds ago Exited (0) 9 seconds ago html
//使用一个测试容器登入进去查看映射情况
[root@localhost ~]# docker run -it --volumes-from html busybox
/ # ls /usr/share/nginx/html/ //映射成功
Images index.html
/ #
//把测试的容器删除
[root@localhost ~]# docker rm -f 8e6cb7c394f8
8e6cb7c394f8
[root@localhost ~]#
//创建nginx配置文件存放位置
[root@localhost ~]# mkdir /config
[root@localhost ~]# ls /config/
[root@localhost ~]#
//提供nginx配置文件
root@localhost ~]# yum -y install nginx
[root@localhost ~]# cp -r /etc/nginx/* /config/
[root@localhost ~]# ls /config/
conf.d fastcgi_params mime.types scgi_params win-utf
default.d fastcgi_params.default mime.types.default scgi_params.default
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
[root@localhost ~]#
[root@localhost ~]# yum -y remove nginx
//创建一个数据卷容器,数据来源于html容器,给配置文件做一个映射
[root@localhost ~]# docker run --name config --volumes-from html -v /config:/etc/nginx busybox
//做一个测试容器查看网站和配置文件映射情况
[root@localhost ~]# docker run -it --volumes-from config busybox
/ # ls /etc/nginx/ //配置文件映射成功
conf.d fastcgi_params.default nginx.conf uwsgi_params.default
default.d koi-utf nginx.conf.default win-utf
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
/ # ls /usr/share/nginx/html/ //配置文件映射成功
Images index.html
//删除测试容器
[root@localhost ~]# docker rm -f wizardly_goldberg
//删除html容器,即使config容器数据来源于html容器,config里面的数据依旧不变
[root@localhost ~]# docker rm -f html
html
[root@localhost ~]# docker run -it --rm --volumes-from config busybox
/ # ls /usr/share/nginx/html/
Images index.html
/ # ls /etc/nginx/
conf.d fastcgi_params.default nginx.conf uwsgi_params.default
default.d koi-utf nginx.conf.default win-utf
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
/ #
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8237d7817211 busybox "sh" 21 hours ago Exited (0) 21 hours ago config
[root@localhost ~]#
//启动一个web容器
[root@localhost ~]# docker run -d -P --name web --volumes-from config nginx
6daf4db7177847de95c322ff4eae2d369e96d6434311efa8d01efe050fa5baea
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6daf4db71778 nginx "/docker-entrypoint.…" 11 seconds ago Up 10 seconds 0.0.0.0:49153->80/tcp, :::49153->80/tcp web
[root@localhost ~]#
[root@localhost ~]# docker port web
80/tcp -> 0.0.0.0:49153
80/tcp -> :::49153
[root@localhost ~]#
页面访问
//更新网站内容
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
Images index.html
[root@localhost html]# mv index.html 1.html
[root@localhost html]# echo "hello world" > index.php
[root@localhost html]# mv index.php index.html
[root@localhost html]# cat index.html
hello world
[root@localhost html]#
[root@localhost html]# mv 1.html Images/ /opt/
页面访问
//制作第二个网站访问页面
[root@localhost html]# ls
index.html
[root@localhost html]# mkdir test
[root@localhost html]# echo "test page" > test/index.html
[root@localhost html]# sed -n '1p' test/index.html
test page
[root@localhost html]#
访问页面
//为了方便访问两个网站,整理一下目录
[root@localhost html]# ls
game test
[root@localhost html]# ls game/
Images index.html
[root@localhost html]# ls test/
index.html
[root@localhost html]#
//使用真机修改配置文件,用两个端口号进行访问,映射到容器里面
[root@localhost ~]# cd /config/
[root@localhost config]# vim nginx.conf
[root@localhost config]# sed -n '35,58p' nginx.conf
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 8080;
server_name test.example.com;
location / {
root /usr/share/nginx/html/test;
index index.html;
}
}
server {
listen 80;
server_name game.example.com;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root /usr/share/nginx/html/game;
index index.html;
}
[root@localhost config]#
[root@localhost config]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6daf4db71778 nginx "/docker-entrypoint.…" 48 minutes ago Up 48 minutes 0.0.0.0:49153->80/tcp, :::49153->80/tcp web
8237d7817211 busybox "sh" 22 hours ago Exited (0) 22 hours ago config
[root@localhost config]# docker rm -f web
web
[root@localhost config]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8237d7817211 busybox "sh" 22 hours ago Exited (0) 22 hours ago config
[root@localhost config]# docker run -d -p 80:80 -p 8080:8080 --name web --volumes-from config nginx
1f57516abbc07dcb6930f6bfa4e0a939e32ebf2450baa0b3d8daa23bcc6f4972
[root@localhost config]# docker port web
80/tcp -> 0.0.0.0:80
80/tcp -> :::80
8080/tcp -> 0.0.0.0:8080
8080/tcp -> :::8080
[root@localhost config]#