docker安装nginx,以及常用操作
作者:互联网
1、下载nginx镜像
拉取nginx
[root@wangazure ~]# docker pull nginx:1.18.0 #拉取nginx
1.18.0: Pulling from library/nginx
f7ec5a41d630: Pull complete
0b20d28b5eb3: Pull complete
1576642c9776: Pull complete
c12a848bad84: Pull complete
03f221d9cf00: Pull complete
Digest: sha256:e90ac5331fe095cea01b121a3627174b2e33e06e83720e9a934c7b8ccc9c55a0
Status: Downloaded newer image for nginx:1.18.0
docker.io/library/nginx:1.18.0
[root@wangazure ~]# docker images #查看所有镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 6.2.6 7614ae9453d1 4 weeks ago 113MB
nginx 1.18.0 c2c45d506085 9 months ago 133MB
2、运行nginx
[root@wangazure ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 6.2.6 7614ae9453d1 4 weeks ago 113MB
nginx 1.18.0 c2c45d506085 9 months ago 133MB
[root@wangazure ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@wangazure ~]# docker run -p 8082:80 -d --restart=always --name nginx01 nginx:1.18.0 #-p映射端口(服务器端口 :容器端口) -d 后台运行,--restart=always跟着docker服务启动(自启动),--name nginx01起别名nginx01,最后是镜像名称或者id都可以
77379630b3295ea20d156ccfeca49d05db3b7b5b92197e84dc6f5d6c67f7b338
[root@wangazure ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
77379630b329 nginx:1.18.0 "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:8082->80/tcp, :::8082->80/tcp nginx01
[root@wangazure ~]#
访问浏览器8082端口,有nginx页面,可以
3、nginx自定义配置文件
其中nginx.conf的配置文件在容器里/etc/nginx目录下
root@e4365b27da62:/etc/nginx# ls
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
root@e4365b27da62:/etc/nginx# pwd
/etc/nginx
root@e4365b27da62:/etc/nginx#
要想用自己的配置文件启动nginx,就要先启动一个nginx容器,从里面拷贝出来一份配置文件(nginx.conf)
我这里放的位置是服务器上的目录是(/root/nginx/nginxconf)
[root@wangazure ~]# mkdir /root/nginx
[root@wangazure ~]# mkdir /root/nginx/nginxconf
[root@wangazure ~]# docker cp nginx01:/etc/nginx/nginx.conf /root/nginx/nginxconf #把容器里的配置文件复制到/root/nginx/nginxconf里
[root@wangazure ~]# ls
install.sh nginx
[root@wangazure ~]# cd nginx/
[root@wangazure nginx]# cd nginxconf/
[root@wangazure nginxconf]# ls
nginx.conf
[root@wangazure nginxconf]#
用自定义的配置文件启动nginx
[root@wangazure ~]# docker run -p 8082:80 -d --restart=always --name nginx01 -v /root/nginx/nginxconf/nginx.conf:/etc/nginx/nginx.conf nginx:1.18.0
ac282b2280f702f6d29db78b3cd17bb9029f08b802af287bba7fed709b2b2a98
[root@wangazure ~]#
4、托管一些简单的静态内容
[root@wangazure ~]# docker run -p 8082:80 -d --restart=always --name nginx01 -v /root/nginx/nginxconf/nginx.conf:/etc/nginx/nginx.conf nginx:1.18.0
1c3934a1943263294459ad09d6491d787aecb7603dd37e1bdf7a50df9589c6ee
[root@wangazure ~]# docker exec -it nginx01 bash #进入容器
root@1c3934a19432:/# find / -name index.html #查找html的路径
find: '/proc/1/map_files': Operation not permitted
find: '/proc/29/map_files': Operation not permitted
find: '/proc/30/map_files': Operation not permitted
find: '/proc/36/map_files': Operation not permitted
/usr/share/nginx/html/index.html
root@1c3934a19432:/#
关闭容器,再启动容器(第二个-v是把服务器下的/root/nginx/nginxhtml和容器的/usr/share/nginx/html同步)
[root@wangazure ~]# docker rm -f nginx01
nginx01
[root@wangazure ~]# docker run -p 8082:80 -d --restart=always --name nginx01 -v /root/nginx/nginxconf/nginx.conf:/etc/nginx/nginx.conf -v /root/nginx/nginxhtml:/usr/share/nginx/html nginx:1.18.0
a075f34bd445cffeba91fdc07923d6082c2081e8cbff419b36be18b63badfc90
[root@wangazure ~]#
这就用上了
自定义index.html
一定要切换到刚才创建的/root/nginx/nginxhtml/目录里
[root@wangazure ~]# cd nginx/nginxhtml/
[root@wangazure nginxhtml]# ls
[root@wangazure nginxhtml]# vim index.html
<html>
<body>
<p>hello docker nginx!!!</p>
</body>
</html>
刷新浏览器,就可以看到自己写的index.html,不用重启镜像。
以后静态资源就放这儿(/root/nginx/nginxhtml/)就好了。
未完待续······
标签:root,1.18,nginx,conf,docker,安装,wangazure 来源: https://blog.csdn.net/weixin_44953395/article/details/122654236