创建Nginx容器,同时提供配置文件和网页文件
映射配置文件和网站
// 真机网站存放位置
[root@localhost ~]# ls /var/www/html/
articles.xls game.html images index.html js style
// 拉取一个nginx镜像
[root@localhost ~]# docker pull nginx
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f652ca386ed1 4 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
353751ed8264 busybox "sh" 5 seconds ago Exited (0) 2 seconds ago html
// 使用一个测试容器登入进去查看映射情况
[root@localhost ~]# docker run -it --volumes-from html busybox
/ # ls /usr/share/nginx/html/ // 网站映射成功
articles.xls images js
game.html index.html style
// 把测试的容器删除
/ # exit
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
64ff86f2f505 busybox "sh" 2 minutes ago Exited (0) 3 seconds ago xenodochial_brattain
353751ed8264 busybox "sh" 2 minutes ago Exited (0) 2 minutes ago html
[root@localhost ~]# docker rm -f 64ff86f2f505
64ff86f2f505
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
353751ed8264 busybox "sh" 4 minutes ago Exited (0) 4 minutes ago html
// 创建配置文件存放目录
[root@localhost ~]# mkdir /config
[root@localhost ~]# ls /config/
// 先用yum安装一个nginx
[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum -y install nginx
// 默认配置文件存放位置
[root@localhost ~]# 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
// 复制配置文件到config目录下
[root@localhost ~]# cp -r /etc/nginx/* /config/
[root@localhost ~]# ls /config/
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
// 配置文件转移成功之后。可以删除真机上的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
1c56b8c0709a busybox "sh" 8 seconds ago Exited (0) 7 seconds ago config
353751ed8264 busybox "sh" 12 minutes ago Exited (0) 12 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/ // 网站映射成功
articles.xls images js
game.html index.html style
// 删除测试容器
/ # exit
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21af77f9a4c0 busybox "sh" 2 minutes ago Exited (0) About a minute ago zen_goldberg
1c56b8c0709a busybox "sh" 3 minutes ago Exited (0) 3 minutes ago config
353751ed8264 busybox "sh" 15 minutes ago Exited (0) 15 minutes ago html
[root@localhost ~]# docker rm -f 21af77f9a4c0
21af77f9a4c0
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c56b8c0709a busybox "sh" 3 minutes ago Exited (0) 3 minutes ago config
353751ed8264 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/
articles.xls images js
game.html index.html 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
1c56b8c0709a busybox "sh" 12 minutes ago Exited (0) 12 minutes ago config
// 启动一个容器web
[root@localhost ~]# docker run -d -P --name web --volumes-from config nginx
7715720ed6432e830c370daa5450cecc16b002ed35319741a0ab1a60efe7e860
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7715720ed643 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]# echo "hello amu" > index.html
[root@localhost html]# cat index.html
hello amu
制作第二个网站访问页面
[root@localhost html]# mkdir test
[root@localhost html]# echo "test page" > test/index.html
// 为了方便访问两个网站,整理一下目录
[root@localhost html]# mkdir game
[root@localhost html]# ls
articles.xls game.html index.html style
game images js test
[root@localhost html]# mv articles.xls game.html index.html style images js game/
[root@localhost html]# ls
game test
// 使用真机修改配置文件,用两个端口号进行访问,映射到容器里面
[root@localhost html]# cd /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 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;
}
// 前面的web容器我已经删除了,后面重新映射创建一个新的web容器
[root@localhost config]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c56b8c0709a busybox "sh" 37 minutes ago Exited (0) 37 minutes ago config
// 做80端口和8080端口映射
[root@localhost config]# docker run -d -p 80:80 -p 8080:8080 --name web --volumes-from config nginx
df5cf9e9aea7a3f446178420c659019f39cbbd77c889094c13e0cf7f940432de
[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
直接访问
使用8080端口访问
// 即使我们删除了config容器,也不影响web容器的数据,网站也是正常访问
[root@localhost config]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
df5cf9e9aea7 nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp web
1c56b8c0709a busybox "sh" 42 minutes ago Exited (0) 42 minutes ago config
[root@localhost config]# docker rm -f config
config
[root@localhost config]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
df5cf9e9aea7 nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp web
restart 重启
// restart自动重启容器
[root@localhost ~]# docker run -d --name apache -P --restart always httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
e5ae68f74026: Already exists
bc36ee1127ec: Pull complete
d3576f2b6317: Pull complete
f1aa5f54b226: Pull complete
aa379c0cedc2: Pull complete
Digest: sha256:fba8a9f4290180ceee5c74638bb85ff21fd15961e6fdfa4def48e18820512bb1
Status: Downloaded newer image for httpd:latest
04a0c83cc4b8bf971487429cafb37438d2801e02d9e8efceae8b0e5354019450
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
04a0c83cc4b8 httpd "httpd-foreground" 4 seconds ago Up 2 seconds 0.0.0.0:49157->80/tcp, :::49157->80/tcp apache
df5cf9e9aea7 nginx "/docker-entrypoint.…" 14 minutes ago Up 14 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp web
// 重启之后只有apache容器在运行了
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
04a0c83cc4b8 httpd "httpd-foreground" 49 seconds ago Up 1 second 0.0.0.0:49153->80/tcp, :::49153->80/tcp apache
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/5580.html