##########################################
#运行容器
#安装Nginx
#搜索、下载镜像
docker search nginx
docker pull nginx
docker images nginx
#运行容器mynginx
docker run -p : --name mynginx -d nginx
#查看端口
netstat -antp|grep
#访问测试
curl 127.0.0.1
#外部浏览器访问ip正常,部署成功
#进入Nginx容器
docker exec -it mynginx /bin/sh
#退出容器 exit
Ctrl+d #快捷键
#列出容器
docker ps -a
#删除容器
docker rm mynginx
##########################################
运行Nginx部署网站
##########################################
#接下来思考问题:
#Nginx配置、查看日志、部署网站
#需要把外部的目录或文件映射到docker容器
#创建目录
Ngdir=/www/docker/nginx
mkdir -p $Ngdir/{www,log,conf/conf.d}
#创建配置(采用默认配置去注释)
echo '#man config
user nginx;
worker_processes ;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections ;
}
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;
keepalive_timeout ;
include /etc/nginx/conf.d/*.conf;
}
'>$Ngdir/conf/nginx.conf
#
echo '#nginx
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 404 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
'>$Ngdir/conf/conf.d/default.conf
#html
echo 'Welcome to nginx!'>$Ngdir/www/index.html
echo 'error_page 500 502 503 504 404'>$Ngdir/www/50x.html
#使用nginx镜像,创建容器mynginx
docker run -p 80:80 --name mynginx \
-v $Ngdir/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
-v $Ngdir/conf/conf.d:/etc/nginx/conf.d:ro \
-v $Ngdir/www:/usr/share/nginx/html:rw \
-v $Ngdir/log:/var/log/nginx:rw \
-d nginx
#测试html内容
curl 127.0.0.1
curl 127.0.0.1/123
#查看error.log
cat $Ngdir/log/error.log
#测试成功 ^_^
# 参数说明:
# -p 80:80:本地80端口:映射docker容器80端口
# -v $Ngdir/log:/var/log/nginx 主机log目录挂载到容器log/nginx