Docker部署Nginx
事先准备nginx配置文件,nginx.conf以及default.conf(conf.d目录下)
nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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;
}
default.conf
server {
listen 8888;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
将Nginx的映射文件都放在 ~/docker/nginx
目录下
docker run \
-p 18000:8000 \
-v ~/docker/nginx/conf:/etc/nginx \
-v ~/docker/nginx/logs:/var/log/nginx \
-v ~/docker/nginx/html:/usr/share/nginx/html \
--restart=on-failure \
--name nginx -d nginx
解释:
docker run \
-p 18000:8000 \ # 配置容器的映射端口,即容器内的8000端口映射到宿主机的18000端口
-v ~/docker/nginx/conf:/etc/nginx \ # 将容器内nginx配置文件的目录挂载到宿主机的~/docker/nginx/conf目录下
-v ~/docker/nginx/logs:/var/log/nginx \ # 将容器内nginx日志目录挂载到宿主机的~/docker/nginx/logs目录下
-v ~/docker/nginx/html:/usr/share/nginx/html \ # 将容器内nginx html目录挂载到宿主机的~/docker/nginx/html目录下
--restart=on-failure \ # 表示只有当容器以非零退出码停止时,Docker才会自动重启容器
--name nginx \ # 配置容器名为nginx
-d nginx # -d表示后台启动nginx镜像
使用 docker ps
命令查看容器是否已启动
使用 docker logs
命令查看容器启动日志
使用 docker stop
命令停掉Nginx