看這篇文檔的前提條件, docker已經裝好,
注意:如果是阿里雲ECS服務器,請先打開安全組端口,80 和 443
1.查看docker nginx鏡像
#docker search nginx
2 用docker 拉取 nginx鏡像
#docker pull nginx
3.查找鏡像中,nginx 的配置文件路徑,(因爲要掛載)
關鍵)查看nginx鏡像裏面配置文件、日誌等文件的具體位置,只有找到鏡像配置文件的路徑,後面掛載文件和文件夾才能覆蓋這些路徑
以終端的方式打開鏡像容器
#docker run -i -t nginx /bin/bash
# ls -l
找到鏡像中nginx.conf配置文件路徑/etc/nginx/nginx.conf
#s -l /etc/nginx/
找到default.conf配置文件的路徑/etc/nginx/conf.d/default.conf
#ls -l /etc/nginx/conf.d/
找到默認首頁文件夾html路徑/usr/share/nginx/html
# ls -l /usr/share/nginx/
找到日誌文件路徑/var/log/nginx
#ls -l /var/log/
然後輸入exit退出容器的終端
--------------------------------------------------------------
開始了
1.首先,清楚剛剛你通過命令行進入docker 產生的容器
#docker ps
#docker ps -a
刪除上面↑ 兩個命令下存在的nginx 容器,有幾個刪幾個.刪除乾淨
2.在你要掛載的地方創建目錄
在linux系統中創建掛載源文件和文件夾(我的是centos7)
mkdir -p /usr/docker/nginx/conf
mkdir -p /usr/docker/nginx/conf.d
mkdir -p /usr/docker/nginx/html
mkdir -p /usr/docker/nginx/logs
長這個樣子
在conf文件夾中,創建nginx.conf文件
將以下內容複製進去
user root;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
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"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
在conf.d 文件夾中,創建default.conf
將以下內容複製進去
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
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;
#}
}
------
最後在linux 中執行以下docker命令(這是一行命令)
docker run --privileged=true --name myNginx -d -p 80:80 -v /usr/docker/nginx/html:/usr/share/nginx/html -v /usr/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/docker/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /usr/docker/nginx/logs:/var/log/nginx nginx
然後開啓nginx容器即可
#docker start (nginx容器id)
--------------------------------------------------------------
引入自己的tomcat(在(conf.d)文件夾中文件中設置)