首先你需要有一个环境配置好的服务器,docker 环境要配置好 这个很好弄网上很多这样的资料。
1 .拉取nginx镜像
docker pull nginx
2.查看下载下来的镜像
docker images
3.创建挂载目录
mkdir -p /data/nginx/{conf,conf.d,html,logs}
4.创建好目录之后要把两个重要的配置文件复制到刚刚对应的文件夹中
(可以把容器先简单运行起来 使用docker cp 命令 把文件拷贝出来
docker cp 容器id:/etc/nginx/nginx.conf /data/nginx/nginx.conf
docker cp 容器id:/etc/nginx/conf.d/default.conf /data/nginx/conf.d/default.conf )这一步也可以换一种实现
创建文件 然后把下面的文件编辑到创建的文件中
touch /data/nginx/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;
}
在创建一个文件 把内容编辑到文件中
touch /data/nginx/conf.d/default.conf
default.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;
}
上面步骤好了之后创建容器
docker run --name nginx83 -d -p 83:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/logs:/var/log/nginx -v /data/nginx/conf.d:/etc/nginx/conf.d -d nginx
查看容器运行状态
docker ps
说明已经运行成功了
找一个html 页面把页面放到挂载出来的html目录中
index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<h1>hello world</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
然后重启一下nginx docker 容器
docker restart 容器id
然后在浏览器访问
0.0.0.0:83
你的主机地址:83
成功了!!!!