Docker存储卷实战-Nginx
创建Nginx容器,同时提供配置文件和网页文件
映射配置文件和网站
// 真机网站存放位置
[root@localhost ~]# ls /var/www/html/
game.html images index.html js style
// 拉取一个nginx镜像
[root@localhost ~]# docker pull nginx
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest ffe9d497c324 About an hour ago 1.24MB
nginx latest f652ca386ed1 5 days ago 141MB
centos latest 5d0da3dc9764 2 months ago 231MB
// 制作一个网站映射
[root@localhost ~]# docker run -it --name html -v /var/www/html:/usr/share/nginx/html busybox
/ # exit
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2f9e5d335efb busybox "sh" 10 seconds ago Exited (0) 3 seconds ago html
// 使用一个测试容器登入进去查看映射情况
[root@localhost ~]# docker run -it --volumes-from html busybox
/ # ls /usr/share/nginx/html/ // 映射成功
game.html images index.html js style
// 把测试的容器删除
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db86571f61a6 busybox "sh" About a minute ago Exited (0) 5 seconds ago affectionate_dewdney
2f9e5d335efb busybox "sh" 2 minutes ago Exited (0) About a minute ago html
[root@localhost ~]# docker rm -f affectionate_dewdney
affectionate_dewdney
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2f9e5d335efb busybox "sh" 2 minutes ago Exited (0) 2 minutes ago html
// 创建配置文件存放目录
[root@localhost ~]# mkdir /config
[root@localhost ~]# ls /config/
[root@localhost ~]#
// 先用yum安装一个nginx
[root@localhost ~]# yum -y install nginx
// 默认配置文件存放位置
[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
// 复制配置文件到config目录下
[root@localhost ~]# cp -r /etc/nginx/* /config/
[root@localhost ~]# ls /config/
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
// 配置文件转移成功之后。可以删除真机上的nginx了
[root@localhost ~]# yum -y remove nginx
// 创建一个数据卷容器,数据来源于html容器,给配置文件做一个映射
[root@localhost ~]# docker run --name config --volumes-from html -v /config:/etc/nginx busybox
[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
e008c59e6db5 busybox "sh" 6 seconds ago Exited (0) 6 seconds ago config
2f9e5d335efb busybox "sh" 9 minutes ago Exited (0) 9 minutes ago html
// 做一个测试容器查看网站和配置文件映射情况
[root@localhost ~]# docker run -it --volumes-from config 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/ // 网站映射成功
game.html images index.html js style
// 删除测试容器
/ # exit
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8f877c35c199 busybox "sh" 2 minutes ago Exited (0) 56 seconds ago eager_keldysh
e008c59e6db5 busybox "sh" 3 minutes ago Exited (0) 3 minutes ago config
2f9e5d335efb busybox "sh" 12 minutes ago Exited (0) 12 minutes ago html
[root@localhost ~]# docker rm -f eager_keldysh
eager_keldysh
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e008c59e6db5 busybox "sh" 11 minutes ago Exited (0) 11 minutes ago config
2f9e5d335efb busybox "sh" 20 minutes ago Exited (0) 20 minutes ago html
// 删除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/
game.html images index.html js style
/ # 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
/ # exit
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e008c59e6db5 busybox "sh" 13 minutes ago Exited (0) 13 minutes ago config
// 启动一个容器web
[root@localhost ~]# docker run -d -P --name web --volumes-from config nginx
1971ec99cfa73fc15e368cee6c127e486bed478704871ab0d3d3c56dd8e7d9fb
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1971ec99cfa7 nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:49153->80/tcp, :::49153->80/tcp web
页面访问
更换网站内容
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# mv index.html 1.html
[root@localhost html]# ls
1.html game.html images js style
[root@localhost html]# echo "hello world" > index.html
[root@localhost html]# cat index.html
hello world
页面访问
制作第二个网站访问页面
[root@localhost html]# mkdir test
[root@localhost html]# echo "test page" > test/index.html
访问页面
// 为了方便访问两个网站,整理一下目录
[root@localhost html]# ls
game.html images index.html js style test
[root@localhost html]# mkdir game
[root@localhost html]# ls
game game.html images index.html js style test
[root@localhost html]# mv game.html images/ index.html js/ style/ game
[root@localhost html]# ls
game test
[root@localhost html]# ls game/
game.html images index.html js style
// 使用真机修改配置文件,用两个端口号进行访问,映射到容器里面
[root@localhost html]# cd /config/
[root@localhost config]# ls
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
[root@localhost config]# vim nginx.conf
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 8080; // 第二个网站使用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]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c730e1ec7a03 nginx "/docker-entrypoint...." 8 minutes ago Up 8 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp web
e008c59e6db5 busybox "sh" 2 hours ago Exited (0) 2 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
e008c59e6db5 busybox "sh" 2 hours ago Exited (0) 2 hours ago config
// 做80端口和8080端口映射
[root@localhost config]# docker run -d -p 80:80 -p 8080:8080 --name web --volumes-from config nginx
c730e1ec7a03b6622c1ecdaeff66ce64c15aec98dfdda3bc3f4905cfbf603d63
[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
直接访问