使用mac运行docker容器,遇到了很多坑,比如本地无法访问容器ip什么的,导致配置nginx案例时,花费了很长时间,最终不断尝试,终于配置出了nginx的反向代理案例,有问题,欢迎提问,一起进步~
1. 实现效果
(1)打开浏览器,在浏览器地址栏输入地址 www.123.com,通过nginx反向代理跳转到docker容器tomcat主页面中
2. 具体实现
(1)拉取nginx镜像
docker pull nginx
(2) 拉取tomcat镜像
docker pull tomcat
(3)查看本地已经安装的镜像,如果有我们刚拉取的镜像:
docker images
(4) 分别运行nginx、tomcat 容器
4.1 nginx 为了方便配置 使用数据卷挂载(准备)
4.1.1 创建 Nginx 临时容器,用于拷贝所需配置文件
docker run --name tmp-nginx -d nginx
4.1.2 拷贝 Nginx 配置文件
docker cp tmp-nginx:/etc/nginx/nginx.conf /Users/yinmb/docker/nginx/nginx.conf
4.1.3 拷贝默认配置文件:
docker cp tmp-nginx:/etc/nginx/conf.d/default.conf /Users/yinmb/docker/nginx/conf.d/default.conf
4.1.4 强制删除临时nginx容器
docker rm -f tmp-nginx
4.2 运行Nginx 容器 并映射 Nginx 配置文件、站点配置文件目录
docker run --name nginx -p 80:80 -v /Users/yinmb/docker/nginx/nginx.conf:/etc/nginx/nginx.conf -v /Users/yinmb/docker/nginx/conf.d:/etc/nginx/conf.d -d nginx
4.3 运行tomcat
docker run --name tomcat -p 8080:8080 -d tomcat
4.4 docker ps 查看运行的容器
(5) 配置mac的hosts文件,添加:127.0.0.1 www.123.com
vim /etc/hosts
(6)配置nginx 文件
vim /Users/yinmb/docker/nginx/conf.d/default.conf
server {
listen 80;
server_name 172.17.0.3;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
proxy_pass http://172.17.0.4:8080;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
6.1 查看容器ip 命令(补充)
docker inspect nginx
网友评论