Docker搭建微服務自動部署 <架構探險之路>,讓咱們來了解下Docker中如何安裝、使用nginx吧!php
Linux 環境html
安裝編譯工具及庫文件linux
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
複製代碼
首先要安裝 PCREnginx
PCRE 做用是讓 Nginx 支持 Rewrite 功能c++
若提示無wget則安裝(在 Docker 拉取的源 centos 中默認是沒安裝的)web
yum -y install wget
複製代碼
進入程序安裝目錄docker
cd /usr/local/src/
複製代碼
下載pcre安裝包centos
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
複製代碼
解壓安裝包:bash
tar -zxvf pcre-8.35.tar.gz
複製代碼
刪除安裝包架構
rm -rf pcre-8.35.tar.gz
複製代碼
進入安裝包
cd pcre-8.35
複製代碼
編譯安裝
./configure
make && make install
複製代碼
查看pcre版本
preconv --version
複製代碼
[root@de790c02f7b6 pcre-8.35]# preconv --version
GNU preconv (groff) version 1.22.2 with iconv support
複製代碼
安裝 Nginx
下載安裝包
wget http://nginx.org/download/nginx-1.6.2.tar.gz
複製代碼
安裝
cd nginx-1.8.0
./configure --prefix=/usr/local/nginx
make
make install
複製代碼
檢查安裝結果
/usr/local/nginx/sbin/nginx -v
複製代碼
腳本
FROM centos:latest
## 製做者信息
MAINTAINER xiachaoyang xiazhaoyang@live.com
RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel
## 建立目錄
RUN mkdir -p /usr/local/nginx
## 本地安裝 直接自動解壓
## ADD nginx-1.8.0.tar.gz /usr/local/src
## 在線安裝
RUN cd /usr/local/src
ADD http://nginx.org/download/nginx-1.8.0.tar.gz .
RUN tar -zxvf nginx-1.8.0.tar.gz
RUN ln -s /usr/local/src /componets
RUN cd nginx-1.8.0 && ./configure --prefix=/usr/local/nginx && make && make install
RUN rm -vf /usr/local/nginx/conf/nginx.conf
ADD http://www.apelearn.com/study_v2/.nginx_conf /usr/local/nginx/conf/nginx.conf
EXPOSE 80
# 設置啓動時執行命令
#ENTRYPOINT /usr/local/nginx/sbin/nginx -v
複製代碼
鏡像
docker build -t env-nginx . docker images docker tag [鏡像ID] env-nginx:1.0 docker run -i -t -p 80:80 env-nginx:1.0
清空 Nginx Docker
docker rm -f docker container ls -a -q
docker rmi -f env-nginx:2.0 docker build -t env-nginx:2.0 . docker run -i -t -p 8001:80 --name nginx --restart=always env-nginx:2.0
運行後執行nginx
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker run -i -t -p 8001:80 --restart=always 127.0.0.1:5000/env-nginx:1.0
[root@56dfba762876 /]# cd usr/local/nginx/sbin/
[root@56dfba762876 sbin]# ls
nginx
[root@56dfba762876 sbin]# ./nginx -v
nginx version: nginx/1.8.0
[root@56dfba762876 sbin]# ps -ef|grep nginx
root 18 1 0 07:44 pts/0 00:00:00 grep --color=auto nginx
[root@56dfba762876 sbin]# ./nginx
[root@56dfba762876 sbin]# ps -ef|grep nginx
root 20 1 0 07:45 ? 00:00:00 nginx: master process ./nginx
nobody 21 20 0 07:45 ? 00:00:00 nginx: worker process
nobody 22 20 0 07:45 ? 00:00:00 nginx: worker process
複製代碼
配置路由
關閉nginx
./nginx -s stop
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
56dfba762876 127.0.0.1:5000/env-nginx:1.0 "/bin/bash" 10 minutes ago Up 10 minutes 0.0.0.0:8001->80/tcp brave_kowalevski
d75f05b4d7f8 hyper/docker-registry-web "start.sh" 5 days ago Up About an hour 0.0.0.0:8000->8080/tcp local-docker-res-web
1b5239bc943b registry "/entrypoint.sh /etc…" 5 days ago Up About an hour 0.0.0.0:5000->5000/tcp local-docker-res
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker attach 56dfba762876
[root@56dfba762876 sbin]# ls
nginx
[root@56dfba762876 sbin]# cd ..
[root@56dfba762876 nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@56dfba762876 nginx]# cd c
client_body_temp/ conf/
[root@56dfba762876 nginx]# cd conf/
[root@56dfba762876 conf]# ls
fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf
fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
[root@56dfba762876 conf]# cd ../sbin/
[root@56dfba762876 sbin]# ./nginx -s stop
[root@56dfba762876 sbin]# ps -ef|grep nginx
root 30 1 0 07:55 pts/0 00:00:00 grep --color=auto nginx
複製代碼
修改路由配置
vi nginx.config
[root@56dfba762876 conf]# cd ../sbin/
[root@56dfba762876 sbin]# ./nginx
[root@56dfba762876 sbin]# cd ..
[root@56dfba762876 nginx]# cd conf/
[root@56dfba762876 conf]# vi nginx.conf
[root@56dfba762876 conf]# cd ../sbin/
[root@56dfba762876 sbin]# ./nginx -s stop
[root@56dfba762876 sbin]# ./nginx
[root@56dfba762876 sbin]#
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
location /docker-registry-web/ {
proxy_pass http://192.168.2.102:8000/;
proxy_redirect default;
}
location /jenkins/ {
proxy_pass http://192.168.2.102:8002/;
proxy_redirect default;
}
}
複製代碼
jenkins經過nginx映射效果
上圖靜態資源拉取失敗緣由:容器內部端口訪問不經過本地主機,直接訪問資源沒法獲取,具體緣由後續跟進...
複製代碼
未完待續....