1.安裝Docker,若是已經安裝能夠跳過此步驟html
yum install docker
啓動docker服務nginx
systemctl start docker
2.使用docker pull命令從docker hub下載最新版本的nginx鏡像web
docker pull nginx
3.在宿主機下新建三個目錄:www,logs,conf分別用來映射nginx容器的項目目錄,日誌目錄,配置文件docker
mkdir /data/nginx cd /data/nginx mkdir www logs conf
4.使用nginx鏡像建立一個nginx容器app
docker run -d -p 80:80 --name nginx-web \ -v /data/nginx/www:/usr/share/nginx/html \ -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \ -v /data/nginx/logs:/var/log/nginx nginx
-v:將宿主機目錄或者文件掛載到容器對應的目錄或者文件代理
-d:守護態(後臺)運行容器rest
-p:將容器的端口映射到主機的端口,前面80是宿主機端口,後面的80是容器端口日誌
--name:命名容器code
5.在/data/nginx/conf下新建nginx.conf配置文件orm
touch /data/nginx/conf/nginx.conf
6.配置nginx.conf文件
假如咱們有兩個項目分別在/data/nginx/www/demo1和/data/nginx/www/demo2下,對應的域名爲demo1.diandi.com和demo2.diandi.com
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; gzip on; server { listen 80; server_name demo1.diandi.com; location / { root /usr/share/nginx/html/demo1; index index.html; } } server { listen 80; server_name demo2.diandi.com; location / { root /usr/share/nginx/html/demo2; index index.html; } } }
由於咱們宿主機的/data/nginx/www目錄已經代理了容器的/usr/share/nginx/html目錄因此root參數/usr/share/nginx/html實際目錄是/data/nginx/www
7.重啓容器
docker restart nginx-web