映射配置文件和网站
# 真机网站存放位置
[root@localhost ~]# ls /var/www/html/
assets icon.png index.html js share.png
# 拉取一个nginx镜像
[root@localhost html]# 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
nginx latest f652ca386ed1 5 days ago 141MB
busybox latest d23834f29b38 7 days ago 1.24MB
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
051ff21b4708 busybox "sh" 22 seconds ago Exited (0) 12 seconds ago html
# 使用一个测试容器登入进去查看映射情况
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
051ff21b4708 busybox "sh" 22 seconds ago Exited (0) 12 seconds ago html
[root@localhost ~]# docker run -it --volumes-from html busybox
/ # ls /usr/share/nginx/html/
assets icon.png index.html js share.png
# 把测试的容器删除
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20a8a172af5e busybox "sh" About a minute ago Exited (0) About a minute ago kind_shamir
051ff21b4708 busybox "sh" 6 minutes ago Exited (0) 6 minutes ago html
[root@localhost ~]# docker rm -f 20a8a172af5e
20a8a172af5e
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
051ff21b4708 busybox "sh" 7 minutes ago Exited (0) 7 minutes ago html
# 创建配置文件存放目录
[root@localhost ~]# mkdir /config
[root@localhost ~]# ls /config/
# 先用yum安装一个nginx
[root@localhost ~]# yum -y install epel-release 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
[root@localhost ~]# ls /etc/nginx
ls: cannot access '/etc/nginx': No such file or directory
# 创建一个数据卷容器,数据来源于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
93f019a60723 busybox "sh" 27 seconds ago Exited (0) 25 seconds ago config
051ff21b4708 busybox "sh" 13 minutes ago Exited (0) 13 minutes ago html
# 做一个测试容器查看网站和配置文件映射情况
[root@localhost ~]# docker run -it --volumes-from config busybox
/ # 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
/ # ls /usr/share/nginx/html/
assets icon.png index.html js share.png
# 删除测试容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b3a9bd1a9258 busybox "sh" About a minute ago Exited (0) 4 seconds ago friendly_greider
93f019a60723 busybox "sh" 3 minutes ago Exited (0) 3 minutes ago config
051ff21b4708 busybox "sh" 15 minutes ago Exited (0) 15 minutes ago html
[root@localhost ~]# docker rm -f b3a9bd1a9258
b3a9bd1a9258
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
93f019a60723 busybox "sh" 3 minutes ago Exited (0) 3 minutes ago config
051ff21b4708 busybox "sh" 16 minutes ago Exited (0) 16 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/
assets icon.png index.html js share.png
/ # 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
/ # exit
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
93f019a60723 busybox "sh" 5 minutes ago Exited (0) 5 minutes ago config
# 启动一个容器web
[root@localhost ~]# docker run -d -P --name web --volumes-from config nginx
19aa7ee3015989957047b395ebc17fab3bbffc1900741623b1c44a23fd939fd8
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19aa7ee30159 nginx "/docker-entrypoint.…" 9 seconds ago Up 7 seconds 0.0.0.0:49153->80/tcp, :::49153->80/tcp web
页面访问
更换网站内容
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# mkdir game
[root@localhost html]# mv * ../html/game/
[root@localhost html]# ls
game
[root@localhost html]# echo "hello world" > index.html
[root@localhost html]# cat index.html
hello world
页面访问
制作第二个网站访问页面
[root@localhost html]# mkdir test
[root@localhost html]# cd test/
[root@localhost test]# ls
images index.html js
页面访问
# 使用真机修改配置文件,用两个端口号进行访问,映射到容器里面
[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;
}