创建nginx容器
需要同时提供配置文件和网页文件
实例:
[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
f1432947c621 busybox "sh" 23 seconds ago Exited (0) 14 seconds ago html
3037202498fe httpd:v1.0 "/scripts/start.sh" 17 hours ago Exited (255) 3 minutes ago 0.0.0.0:80->80/tcp, :::80->80/tcp httpd
5797953ad449 centos "/bin/bash" 21 hours ago Exited (255) 18 hours ago centos
安装nginx并提供测试的配置文件
[root@localhost ~]# yum -y install nginx
[root@localhost ~]# mkdir /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
基于容器html的容器卷挂载数据卷
[root@localhost ~]# docker run --name nginx_conf --volumes-from html -v /config:/etc/nginx busybox
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7015c9be3ee1 busybox "sh" 9 seconds ago Exited (0) 7 seconds ago nginx_conf
f1432947c621 busybox "sh" About a minute ago Exited (0) 58 seconds ago html
3037202498fe httpd:v1.0 "/scripts/start.sh" 17 hours ago Exited (255) 3 minutes ago 0.0.0.0:80->80/tcp, :::80->80/tcp httpd
5797953ad449 centos "/bin/bash" 22 hours ago Exited (255) 18 hours ago centos
基于容器nginx_conf的容器卷,创建nginx容器
[root@localhost ~]# docker run --name nginx -dit -p 80:80 --volumes-from nginx_conf nginx
c9819c1ffe6614ce322d46ff567fbfd2edce09e6f27418956456d7d804184755
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c9819c1ffe66 nginx "/docker-entrypoint.…" 8 seconds ago Up 7 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
查看一下
[root@localhost ~]# docker exec -it nginx /bin/bash
root@c9819c1ffe66:/# 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
编写网页文件,随便放一个网站
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# ls
[root@localhost html]# cat index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>别踩白块</title>
<link rel="stylesheet" href="css/index.css" />
<script src="js/index.js" type="text/javascript"></script>
</head>
<body>
<h2>score</h2>
<h2 id="score">0</h2>
<div id="main">
<div id="con"></div>
</div>
<div class="btn">
<button class="start" onclick="start()">
开始游戏
</button>
</div>
</body>
去容器查看一下
root@c9819c1ffe66:/# ls /usr/share/nginx/html/
'(15'$'\346\235\241\346\266\210\346\201\257'') '$'\351\203\250\347\275\262\344\270\252\347\275\221\347\253\231''_'$'\346\227\251\345\267\235\350\257\255\347\232\204\345\215\232\345\256\242''-CSDN'$'\345\215\232\345\256\242''.html'
123
index.html
root@c9819c1ffe66:/# ls /usr/share/nginx/html/
css index.html js
效果图: