目录
(1)创建文件夹:
(2)准备nginx主配置文件:
(3)准备nginx-server配置文件:
(4)编写docker命令:
(5)构建容器:
(6)访问nginx:
在自定义文件夹中创建以下文件夹:
html:html持久化目录
config:config持久化目录
logs:logs持久化目录
需要自己准备nginx的主配置文件,然后将主配置文件上传到自定义的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; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
需要自己准备nginx的server配置文件,然后将server配置文件上传到自定义的config目录下。默认示例如下:
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
docker run --name container_name \ //指定容器名字,保证唯一要
-p 8081:8081 \ //映射容器的端口号到宿主机的端口号
--restart=always \ //容器开机自启
-v /wocloud/soft/nginx/html:/usr/share/nginx/html \ //挂载html文件夹
-v /wocloud/soft/nginx/config/nginx01.conf:/etc/nginx/conf.d/nginx01.conf \ //挂载server的配置文件
-v /wocloud/soft/nginx/config/nginx.conf:/etc/nginx/nginx.conf \ //挂载nginx的主配置文件
-v /wocloud/soft/nginx/logs:/var/log/nginx \ //挂载log文件夹
-d nginx:1.10.3
说明:-d:表示容器后台运行
--name:指定容器名字,保证唯一要
-p:映射容器的端口号到宿主机的端口号,冒号前端为宿主机端口号,冒号后为docker容器端口号
nginx:1.10.3:指定使用的镜像
最终的docker命令:
docker run --name container_nginx \ -p 8081:8001 \ --restart=always \ -v /wocloud/soft/nginx/html:/usr/share/nginx/html \ -v /wocloud/soft/nginx/config/nginx01.conf:/etc/nginx/conf.d/nginx01.conf \ -v /wocloud/soft/nginx/config/nginx.conf:/etc/nginx/nginx.conf \ -v /wocloud/soft/nginx/logs:/var/log/nginx \ -d nginx:1.10.3
执行编写的docker命令,完成容器的构建和启动。
通过浏览器访问nginx容器映射出来的8081端口号,即可访问到nginx服务。