查找 Docker Hub 上的 nginx 镜像
docker search nginx
拉取官方的镜像
docker pull nginx
使用 NGINX 默认的配置来启动一个 Nginx 容器实例:
docker run --name sb-nginx-test -p 8081:80 -d nginx
sb-nginx-test
容器名称。-
-d
设置容器在在后台一直运行。 -p
端口进行映射,将本地 8081 端口映射到容器内部的 80 端口。
在浏览器中打开
nginx 部署
首先,创建目录 nginx, 用于存放后面的相关东西
mkdir -p /var/nginx/{www,logs,conf}
拷贝容器内 Nginx 默认配置文件到本地当前目录下的 conf 目录,容器 ID 可以查看 docker ps 命令输入中的第一列
(就是把这个启动的容器里面的nginx.conf文件复制出一份来到 /var/nginx/conf 这个目录下)
docker cp f009ed282822:/etc/nginx/nginx.conf /var/nginx/conf
-
www: 目录将映射为 nginx 容器配置的虚拟目录。
-
logs: 目录将映射为 nginx 容器的日志目录。
-
conf: 目录里的配置文件将映射为 nginx 容器的配置文件。
部署命令
docker run -d -p 8082:80--name sb-nginx-test-web1 -v /var/nginx/www:/usr/share/nginx/html -v /var/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /var/nginx/logs:/var/log/nginx nginx
命令说明:
-
-p 8082:80: 将容器的 80 端口映射到主机的 8082 端口。
-
--name sb-nginx-test-web:将容器命名为 sb-nginx-test-web。
-
-v /var/nginx/www:/usr/share/nginx/html:将我们自己创建的 www 目录挂载到容器的 /usr/share/nginx/html。
-
-v /var/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:将我们自己创建的 nginx.conf 挂载到容器的 /etc/nginx/nginx.conf。
-
-v /var/nginx/logs:/var/log/nginx:将我们自己创建的 logs 挂载到容器的 /var/log/nginx。
进入 /var/nginx/www 目录
cd /var/nginx/www
创建 index.html 文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>SB教程(www.啊啊啊啊啊啊啊啊啊啊.com)</title>
</head>
<body>
<h1>我的第一个标题</h1>
<p>我的第一个段落。</p>
</body>
</html>
打开浏览器
后记:
这是我的参数:
/var/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 1111 default_server;
listen [::]:1111 default_server;
server_name localhost;
root /usr/share/nginx/html;
# root /var/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
# location / {
# proxy_pass http://localhost:8066;
# client_max_body_size 200m;
# }
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
所有的操作
启动命令脚本:
docker run -d -p 8082:1111 --name zhangqiangnginx -v /var/nginx/www:/usr/share/nginx/html -v /var/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /var/nginx/logs:/var/log/nginx nginx