1. 搜索nginx镜像
docker search nginx
2. 拉取nginx镜像
docker pull nginx
3. 创建容器,设置端⼝映射、⽬录映射
# 在/root⽬录下创建nginx⽬录⽤于存储nginx数据信息
mkdir ~/nginx
cd ~/nginx
mkdir conf
cd conf
# 在~/nginx/conf/下创建nginx.conf⽂件
vim nginx.conf
4.将以下内容粘贴到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;
}
5.创建nginx的配置文件目录,配置端口,域名,根目录,首页等
# 在/nginx⽬录下创建conf.d⽬录⽤于存储nginx的配置文件
mkdir ~/nginx/conf.d
cd conf.d
#在~/nginx/conf.d/下创建 80.conf
vim ~/nginx/conf.d/80.conf
将下面代码复制到80.conf中
server {
listen 80; # 监听的端⼝
server_name localhost; # 域名或ip
location / { # 访问路径配置
root /usr/share/nginx/html;# 根⽬录
index index.html index.htm; # 默认⾸⻚
}
error_page 500 502 503 504 /50x.html; # 错误⻚⾯
location = /50x.html {
root html;
}
}
6.创建nginx容器,注意:一定要在 ~/nginx 的目录下创建
docker run -id --name=c_nginx \
-p 80:80 \
-p 81:81 \
-p 82:82 \
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $PWD/conf.d:/etc/nginx/conf.d \
-v $PWD/logs:/var/log/nginx \
-v $PWD/html:/usr/share/nginx/html \
nginx
参数说明:
- -p 80:80:将容器的 80端⼝映射到宿主机的 80 端⼝。
- -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf:将主机当前⽬的/conf/nginx.conf 挂载到容器的 :/etc/nginx/nginx.conf。配置⽬录
- -v $PWD/logs:/var/log/nginx:将主机当前⽬录下的 logs ⽬录挂载到容的/var/log/ngin,⽇志⽬录
使用外部机访问 http://域名 或者 http:// IP (类如 nginx的宿主机ip为 192.168.xxx.xxx 则访问 http://192.168.xxx.xxx)