看这篇文档的前提条件, 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)文件夹中文件中设置)