Docker(Nginx)

[root@localhost /]# docker pull nginx                 #使用docker下载nginx镜像 
[root@localhost /]# docker run --name web_h5 -p 80:80 -d nginx  #启动nginx容器
[root@localhost /]# docker exec -it web_h5 bash      #这样就简单的把nginx启动了,但是我们想要改变配置文件nginx.conf ,进入容器,命令
[root@localhost /]# apt-get update              #容器内nginx位置:/etc/nginx/ 不能在容器中使用vim/vi,解决办法 [root@localhost /]# apt-get install vim [root@localhost /]# docker stop web_h5       #此时你就可以自己定制nginx.con文件了,改好配置文件之后重启容器,步骤,先把容器停了 [root@localhost /]# docker start web_h5      #重启nginx容器
上述方式容器中修改nginx配置文件十分不方便,第二种启动方式方式,通过挂载配置文件,就是把装有docker宿主机上面的nginx.conf配置文件映射到启动的nginx容器里面,这需要你首先准备好nginx.con配置文件,如果你应经准备好了,下一步是启动nginx(如上)
[root@localhost /]# docker run --name nginx -p 80:80 
-v /home/nginx/nginx.conf:/etc/nginx/nginx.conf 
-v /home/nginx/log:/var/log/nginx 
-v /home/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf 
-d nginx
命令注释:
--name :给你启动的容器起个名字,以后可以使用这个名字启动或者停止容器 -p    :映射端口,将docker宿主机的80端口和容器的80端口进行绑定 -v    :挂载文件用的, 第一个-v :表示将你本地的nginx.conf覆盖你要起启动的容器的nginx.conf文件, 第二个-v :表示将日志文件进行挂载,就是把nginx容器的日志写到你docker宿主机的/home/nginx/log/下面 第三个-v :表示的和第一个-v意思一样的。 -d    :表示启动的是哪个镜像

 Docker直接挂载启动Nginx:

[root@localhost /]# docker pull nginx    #镜像
[root@localhost /]# docker images        #查看镜像
[root@localhost /]# mkdir -p /data/nginx/{conf,html,logs}   #创建挂载目录

编写nginx.conf配置文件,并放在刚刚创建的目录下:

#user  nobody;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /usr/share/nginx/html;    #指定容器中的路径
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
           root   /usr/share/nginx/html;   #指定容器中的路径
        }
    }
}
[root@localhost /]# docker run --name web_h5 -p 82:80  
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  
-v /data/nginx/logs:/var/log/nginx 
-v /data/nginx/html:/usr/share/nginx/html 
-d nginx

在html目录下随便弄个html文件,重启后直接通过:域名:82  就可以访问到html文件。

posted @ 2021-05-27 17:03  21karat  阅读(754)  评论(0)    收藏  举报