docker部署nginx比之前编译安装nginx还要简单,主要只有以下几个操作
拉取nginx镜像
docker pull nginx
由于我昨天做了私有镜像仓库的配置学习,所以今天直接使用私有仓库的镜像192.168.122.198/test/nginx:v1
然后创建本地映射nginx配置,网站和日志的目录
sudo mkdir -p /data/nginx sudo mkdir -p /data/nginx/{etc,www,logs}
然后编写一个静态文件测试用
sudo vim /data/nginx/www/index.html <html> <head> <title>test nginx</title> </head> <body> test docker deploy nginx ! </body> </html>
然后启动,我们这次启动使用的镜像是上次学习私有仓库拉取的,下面是启动命令
docker run -itd -p 8888:80 --name nginx -v /data/nginx/www:/usr/share/nginx/html -v /data/nginx/logs:/var/log/nginx 192.168.122.198/test/nginx:v1
上述主要映射了两个目录,一个是网站目录,另一个是日志文件目录,其实在实际操作当中我们也是需要把nginx配置文件目录也要映射过去,这样方便优化配置
下面我们将配置文件映射试试,顺便提供一个现成的优化文件方便以后使用
sudo vim /data/nginx/etc/nginx.conf #内容如下 user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; multi_accept on; use epoll; } http { include 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; client_header_timeout 30; client_body_timeout 30; client_max_body_size 128m; reset_timedout_connection on; sendfile on; #tcp_nopush on; keepalive_timeout 30; send_timeout 30; limit_conn_zone $binary_remote_addr zone=addr:5m; limit_conn addr 100; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 128k; fastcgi_buffers 4 256k; fastcgi_busy_buffers_size 256k; gzip on; gzip_min_length 1k; gzip_buffers 16 64k; gzip_http_version 1.1; gzip_comp_level 9; gzip_types text/plain application/x-javascript text/css application/xml application/x-httpd-php; gzip_vary on; proxy_buffer_size 128k; proxy_buffers 32 32k; proxy_busy_buffers_size 128k; open_file_cache max=100000 inactive=30s; open_file_cache_valid 60s; open_file_cache_min_uses 2; open_file_cache_errors on; server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.html; #access_log /var/log/nginx/access.log; #error_log /var/log/nginx/error.log; } }
然后我们再试试添加配置映射启动试试
docker run -itd -p 8888:80 --name nginx -v /data/nginx/www:/usr/share/nginx/html -v /data/nginx/etc/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/logs:/var/log/nginx 192.168.122.198/test/nginx:v1
docker ps查看根刚才一样网页访问是OK的
刚才是直接通过docker命令创建部署的nginx,接下来我们做做docker-compose部署nginx的实践
我们在/data/nginx目录下创建一个docker-compose.yml文件
内容如下
version: '2.0' services: nginx: restart: always image: 192.168.122.198/test/nginx:v1 ports: - 8888:80 volumes: - ./etc/nginx.conf:/etc/nginx/nginx.conf - ./logs:/var/log/nginx - ./www:/usr/share/nginx/html environment: - TZ=Asia/Shanghai
然后启动
docker-compose up -d
然后查看8888端口监听了,docker ps也是正常的话,最后访问下刚才的那个8888的页面正常即说明部署成功了
然后删除是
docker-compose down -v
启动停止操作
docker-compose start docker-compose stop
以上命令均需要进入到docker-compose.yml文件所在目录井陉操作