启动的时候也可以不带版本号直接拉取latest版
1.安装nginx
docker run -p 80:80 --name nginx \
-v /usr/local/docker/nginx/conf:/etc/nginx \
-v /usr/local/docker/nginx/html:/usr/share/nginx/html \
-v /usr/local/docker/nginx/conf.d:/etc/nginx/conf.d \
-v /usr/local/docker/nginx/logs:/var/log/nginx -d --restart=always nginx:1.19.0
这样会-v:not found
需要创建挂载数据 -v 后路径的文件夹
mkdir /usr/local/docker/nginx/conf
mkdir /usr/local/docker/nginx/html
mkdir /usr/local/docker/nginx/conf.d
mkdir /usr/local/docker/nginx/logs
把docker 容器中nginx的配置文件复制到宿主机
docker cp nginx:/etc/nginx /usr/local/docker/nginx/conf
重新运行nginx
docker stop nginx
docker rm nginx
docker run -p 80:80 --name nginx \
-v /usr/local/docker/nginx/conf:/etc/nginx \
-v /usr/local/docker/nginx/html:/usr/share/nginx/html \
-v /usr/local/docker/nginx/conf.d:/etc/nginx/conf.d \
-v /usr/local/docker/nginx/logs:/var/log/nginx -d --restart=always nginx:1.19.0
修改/usr/local/docker/nginx/conf/nginx.conf文件
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
client_max_body_size 100M;
listen 80;
server_name *.doson.com;
if ($http_host ~* "^(.*?)\.doson\.com$") { #正则表达式
set $domain $1; #设置变量
}
location / {
if ($domain ~* "mysql") {
# proxy_pass http://doson.com:3306; #域名中有mysql,转发到3306端口
proxy_pass http://172.20.10.11:3306;
}
if ($domain ~* redis{
# proxy_pass http://ds.com:6379; #域名中有redis,转发到6379端口
proxy_pass http://172.20.10.11:6379;
}
tcp_nodelay on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#以上三行,目的是将代理服务器收到的用户的信息传到真实服务器上
root html;
index index.html index.htm; #默认情况
}
}
}
修改C:\Windows\System32\drivers\etc下hosts文件,通过域名访问虚拟机
说明nginx已成功启动