1.首先拉取nginx
2.先運行nginx,將容器內的/etc/nginx/nginx.conf拷貝出來
3.重新編輯nginx.conf,放入本地路徑
4.停止nginx,刪除容器,然后重新運行容器,掛載配置自己編輯的配置文件
#拉取最新版本nginx
docker pull nginx
#查看拉取的鏡像
docker images
#運行nginx鏡像
docker run -itd -p 8080:80 --name nginxTest nginx
#拷貝nginxTest容器配置文件到本地機器
docker cp nginxTest:/etc/nginx/nginx.conf /nginx.conf
nginx.conf配置
#源配置文件
user nginx;
worker_processes auto;
#日志文件 不加/ 表示相對位置相對當前文件 加/ 絕對路徑
#當path/file 的值為 /dev/null時,這樣就不會輸出任何日志了,這也是關閉error日志的唯一手段;
#leve的取值范圍是debug、info、notice、warn、error、crit、alert、emerg從左至右級別依次增大。
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
#導入mime.types文件
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;
#導入*.conf文件
include /etc/nginx/conf.d/*.conf;
}
修改為自己的配置文件按
worker_processes 1;
#日志文件/log/error.log
error_log /log/error.log error;
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"';
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
#項目訪問路徑 /html/sealHome 在這個文件夾下要有iddex.html
root /html/sealHome;
index index.html;
}
location = /50x.html {
root html;
}
}
server {
listen 8085;
server_name localhost;
location / {
#項目訪問路徑 /html/sealHome 在這個文件夾下要有iddex.html
root /html/sealAdmin;
index index.html;
}
location = /50x.html {
root html;
}
}
}
5.配置完成之后 停止剛才的容器,刪除容器,重新部署自己的項目
#查看容器
docker ps
#停止容器
docker stop nginxTest
#刪除容器
docker rm nginxTest
#啟動容器 重命名 端口映射 配置文件掛載 項目位置掛載 日志文件掛載 重啟自啟
docker run -itd --name myNginx -p 8080:80 -p 8085:8085 -v /nginx.conf:/etc/nginx/nginx.conf -v /usr/local/docker/nginx/html:/html -v /usr/local/docker/nginx/log:/log --restart always nginx