Docker 安装nginx并且配置文件的映射
Docker 安装nginx并且配置文件的映射
1.默认已经有了docker的环境,如果没有的话请查看链接 https://docs.docker.com/get-started/ 2.首先将nginx的镜像下载到服务器上docker pull nginx
[root@VM_0_2_centos opt]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx Digest: sha256:23b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68 Status: Image is up to date for nginx:latest
3.下载完成之后可以通过docker images 查看下载的所有的镜像
[root@VM_0_2_centos opt]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest 990386cbd5c0 2 weeks ago 443MB nginx latest 53f3fd8007f7 2 weeks ago 109MB hello-world latest fce289e99eb9 4 months ago 1.84kB ubuntu 15.10 9b9cb95443b5 2 years ago 137MB training/webapp latest 6fae60ef3446 4 years ago 349MB
4.在data下面创建分别创建以下四个目录 /data/nignx/conf 挂载容器里面的配置,即nginx.conf /data/nignx/conf.d 挂载容器里面的子配置,即nginx.conf里面include的配置文件 /data/nignx/logs 挂载容器里面的代理的日志文件 /data/nignx/html 挂载容器里面的界面的访问 配置好之后就可以启动我们的nginx文件了 5.输入命令
[root@VM_0_2_centos opt]# docker run --name nginx -d -p 80:80 --net host -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /data/nginx/logs:/var/log/nginx nginx
命令解读: run:启动一个docker容器 name:容器的名称 d: 后台启动 p: 绑定别的端口 -p a:b 将宿主机器的a端口绑定到容器的b端口 -P 为随机绑定到端口 net :绑定的网络 这里配置成host(因为对于容器内部来说也有一个ip如果不配置的话默认用容器的ip,导致访问不到) v : 挂载的内容 宿主机器的文件夹:容器的文件夹
6.至此,nginx配置安装完成,界面输入ip可以看到nginx的欢迎界面 7.修改/data/nginx的conf.d和html下的文件,分别如图所示
default.conf server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.php; } location /create_code { proxy_pass http://127.0.0.1:8080; } location /weixin { proxy_pass http://127.0.0.1:8761; } location = /50x.html { root /usr/share/nginx/html; } }
index.html
<html> <head> <title>Mynginx</title> </head> <body> <h1> 欢迎使用nginx! </h1> </body> </html>
然后进行重启
[root@VM_0_2_centos html]# docker restart nginx nginx
8.访问http://coder.struggling-bird.cn/,因为我这里有自己的域名,并且跟服务器的ip进行了绑定 9.访问http://coder.struggling-bird.cn/create_code/#,出现即成功,这个是本人的自己的一个项目
以上即完成了nginx的安装和配置并且配置了动态端口和静态的界面