########################################## #運行容器 #安裝Nginx #搜索、下載鏡像 docker search nginx docker pull nginx docker images nginx #運行容器mynginx docker run -p 80:80 --name mynginx -d nginx #查看端口 netstat -antp|grep 80 #訪問測試 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 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; keepalive_timeout 65; 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
