1 Nginx部署
(1)搜索并下载镜像
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 14207 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1932 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 797 [OK] linuxserver/nginx An Nginx container, brought to you by LinuxS… 137 jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 123 tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 107 [OK] [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx 6ec7b7d162b2: Already exists cb420a90068e: Pull complete 2766c0bf2b07: Pull complete e05167b6a99d: Pull complete 70ac9d795e79: Pull complete Digest: sha256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker images; REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 f07dfa83b528 5 days ago 448MB nginx latest ae2feff98a0c 11 days ago 133MB centos latest 300e315adb2f 2 weeks ago 209MB
可以到dockerhub官网查看Nginx的详细版本信息 :https://hub.docker.com/_/nginx
(2)运行测试
docker run -d --name nginx01 -p 3334:80 nginx
-d 后台运行 --name 给容器命名 -p 3334:80 将宿主机的端口3334映射到该容器的80端口
运行结果:
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it nginx01 /bin/bash Error: No such container: nginx01 [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -d --name nginx01 -p 3334:80 nginx 20c896637ff5de8be835797109d62ee2465e28d9d716be5a8d550ef7d547fcf5 [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 20c896637ff5 nginx "/docker-entrypoint.…" 7 seconds ago Up 5 seconds 0.0.0.0:3334->80/tcp nginx01
(3)配置文件
进入容器,自定义配置文件
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it nginx01 /bin/bash root@20c896637ff5:/# whereis nginx nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx root@20c896637ff5:/# cd /etc/nginx root@20c896637ff5:/etc/nginx# ls conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf root@20c896637ff5:/# cd /etc/nginx root@20c896637ff5:/etc/nginx# ls conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
(4)访问测试
本地主机访问测试,curl命令发起请求,如果使用阿里云服务器需要设置安全组。
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 20c896637ff5 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 0.0.0.0:3334->80/tcp nginx01 [root@iZwz99sm8v95sckz8bd2c4Z ~]# curl localhost:3334
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
</body></html>
(5)安装vim
我们使用Nginx往往需要编写配置文件,但是Nginx官方镜像没有安装vim,需要我们手动进行安装。使用以下命令进行安装:
apt-get install vim
如果执行上述命令出现提示:
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package vim
则需要先同步 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的索引,这样才能获取到最新的软件包。执行以下命令来更新:
apt-get update
更新完毕再安装即可。我们修改了配置文件,只要重新启动容器docker restart 容器id,改动就可以生效了。
解决vim在终端不能复制的问题:在vim 中输入 :set mouse=r。
拓展:启动项目并设置数据卷,为避免nginx因为修改配置文件导致的错误而无法启动容器,我们可以通过cp命令覆盖配置文件,但是设置数据卷会更为方便。启动Nginx容器的同时设置数据卷的命令:
docker run --name my_nginx -d -p 80:80 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/log:/var/log/nginx -v /data/nginx/html:/usr/share/nginx/html nginx
参数说明:
第一个-v:挂载nginx的主配置文件,以方便在宿主机上直接修改容器的配置文件 第二个-v:挂载容器内nginx的日志,容器运行起来之后,可以直接在宿主机的这个目录中查看nginx日志 第三个-v:挂载静态页面目录