Docker安装Nginx

Docker安装Nginx

一、拉取官方的镜像

docker pull nginx
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
8d691f585fa8: Pull complete 
5b07f4e08ad0: Pull complete 
abc291867bca: Pull complete 
Digest: sha256:922c815aa4df050d4df476e92daed4231f466acc8ee90e0e774951b0fd7195a4
Status: Downloaded newer image for nginx:latest
[root@localhost ~]# 

二、以下命令使用 NGINX 默认的配置来启动一个 Nginx 容器实例:

docker run --name runoob-nginx-test -p 8081:80 -d nginx
  • runoob-nginx-test 容器名称。
  • -d 设置容器在在后台一直运行。
  • -p 端口进行映射,将本地 8081 端口映射到容器内部的 80 端口。

执行以上命令会生成一串字符串,类似 6dd4380ba70820bd2acc55ed2b326dd8c0ac7c93f68f0067daecad82aef5f938,这个表示容器的 ID,一般可作为日志的文件名。

三、在浏览器中打开 http://192.168.122.199:8081/,效果如下:

nginx 部署

首先,创建目录 nginx, 用于存放后面的相关东西。

mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf

  

  • www: 目录将映射为 nginx 容器配置的虚拟目录。
  • logs: 目录将映射为 nginx 容器的日志目录。
  • conf: 目录里的配置文件将映射为 nginx 容器的配置文件。

拷贝容器内 Nginx 默认配置文件到本地当前目录下的 conf 目录,容器 ID 可以查看 docker ps 命令输入中的第一列:

docker cp 6dd4380ba708:/etc/nginx/nginx.conf ~/nginx/conf

部署命令

docker run -d -p 8082:80 --name runoob-nginx-test-web -v ~/nginx/www:/usr/share/nginx/html -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/nginx/logs:/var/log/nginx nginx

命令说明:

  • -p 8082:80: 将容器的 80 端口映射到主机的 8082 端口。

  • --name runoob-nginx-test-web:将容器命名为 runoob-nginx-test-web。

  • -v ~/nginx/www:/usr/share/nginx/html:将我们自己创建的 www 目录挂载到容器的 /usr/share/nginx/html。

  • -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:将我们自己创建的 nginx.conf 挂载到容器的 /etc/nginx/nginx.conf。

  • -v ~/nginx/logs:/var/log/nginx:将我们自己创建的 logs 挂载到容器的 /var/log/nginx。

启动以上命令后进入 ~/nginx/www 目录:

$ cd ~/nginx/www

创建 index.html 文件,内容如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
    <h1>我的第一个标题</h1>
    <p>我的第一个段落。</p>
</body>
</html>

输出结果为:

相关命令

如果要重新载入 NGINX 可以使用以下命令发送 HUP 信号到容器:

docker kill -s HUP container-name
 

重启 NGINX 容器命令:

docker restart container-name
 

引用:https://www.runoob.com/docker/docker-install-nginx.html

原文地址:https://www.cnblogs.com/1285026182YUAN/p/11808839.html