? ? ? ? 使用mac運行docker容器,遇到了很多坑,比如本地?zé)o法訪問容器ip什么的,導(dǎo)致配置nginx案例時,花費了很長時間,最終不斷嘗試,終于配置出了nginx的反向代理案例,有問題,歡迎提問,一起進步~
?1. 實現(xiàn)效果
(1)打開瀏覽器,在瀏覽器地址欄輸入地址 www.123.com,通過nginx反向代理跳轉(zhuǎn)到docker容器tomcat主頁面中

2. 具體實現(xiàn)
(1)拉取nginx鏡像
? ? docker pull nginx
? (2)? ?拉取tomcat鏡像
? ? ?docker pull tomcat
(3)查看本地已經(jīng)安裝的鏡像,如果有我們剛拉取的鏡像:
? ?docker images

(4) 分別運行nginx、tomcat 容器
? ?4.1 nginx 為了方便配置 使用數(shù)據(jù)卷掛載(準備)
? ?4.1.1 創(chuàng)建 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