前言
1、docker安装Nginx
1.1、观察nginx是否可下载
healerjean$ docker search nginx
1.2、开始安装
healerjean$ docker pull nginx
2、先运行这个nginx
healerjean$ docker run -it -p 80:80 nginx
3.1、查看这个nginx的容器id号
docker ps
healerjean$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9fbe362214a6 nginx "nginx -g 'daemon of…" Less than a second ago Up 2 seconds 0.0.0.0:80->80/tcp objective_chandrasekhar
2、进入这个容器
2.1、docker exec :在运行的容器中执行命令
-d :分离模式: 在后台运行
-i :即使没有附加也保持STDIN(标准输入) 打开,以交互模式运行容器,通常与 -t 同时使用;
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
docker exec -it 9fbe362214a6 /bin/bash
docker exec -it 9fbe362214a6 /bin/bash
healerjean$ docker exec -it 9fbe362214a6 /bin/bash
root@9fbe362214a6:/# ls -l
total 64
drwxr-xr-x 2 root root 4096 Jul 16 00:00 bin
drwxr-xr-x 2 root root 4096 Jun 26 12:03 boot
drwxr-xr-x 5 root root 360 Aug 15 10:12 dev
drwxr-xr-x 1 root root 4096 Aug 15 10:12 etc
drwxr-xr-x 2 root root 4096 Jun 26 12:03 home
drwxr-xr-x 1 root root 4096 Jul 16 00:00 lib
drwxr-xr-x 2 root root 4096 Jul 16 00:00 lib64
drwxr-xr-x 2 root root 4096 Jul 16 00:00 media
drwxr-xr-x 2 root root 4096 Jul 16 00:00 mnt
drwxr-xr-x 2 root root 4096 Jul 16 00:00 opt
dr-xr-xr-x 185 root root 0 Aug 15 10:12 proc
drwx------ 2 root root 4096 Jul 16 00:00 root
drwxr-xr-x 1 root root 4096 Aug 15 10:12 run
drwxr-xr-x 2 root root 4096 Jul 16 00:00 sbin
drwxr-xr-x 2 root root 4096 Jul 16 00:00 srv
dr-xr-xr-x 13 root root 0 Aug 15 10:12 sys
drwxrwxrwt 1 root root 4096 Jul 24 17:21 tmp
drwxr-xr-x 1 root root 4096 Jul 16 00:00 usr
drwxr-xr-x 1 root root 4096 Jul 16 00:00 var
root@9fbe362214a6:/# cd /etc/nginx/conf.d/
root@9fbe362214a6:/etc/nginx/conf.d# ls -l
total 4
-rw-r--r-- 1 root root 1093 Jul 24 13:02 default.conf
3、docker cp
3.1、将容器29df10f32d44:/etc/nginx/nginx.conf 目录拷贝到主机/Users/healerjean/Desktop目录下
:~ healerjean$ docker cp 29df10f32d44:/etc/nginx/nginx.conf /Users/healerjean/Desktop
3.2、将主机/Users/healerjean/Desktop/AAA.md 拷贝到容器29df10f32d44:/etc/nginx/中
docker cp /Users/healerjean/Desktop/AAA.md 29df10f32d44:/etc/nginx/
:~ healerjean$ docker cp /Users/healerjean/Desktop/AAA.md 29df10f32d44:/etc/nginx/
:~ healerjean$ docker exec -it 29df10f32d44 /bin/bash
root@29df10f32d44:/# cd /etc/nginx/
root@29df10f32d44:/etc/nginx# ls -l
total 44
-rw-r--r-- 1 501 dialout 1519 Jul 27 08:08 AAA.md
drwxr-xr-x 1 root root 4096 Aug 28 07:10 conf.d
-rw-r--r-- 1 root root 1007 Jul 24 13:02 fastcgi_params
-rw-r--r-- 1 root root 2837 Jul 24 13:02 koi-utf
-rw-r--r-- 1 root root 2223 Jul 24 13:02 koi-win
-rw-r--r-- 1 root root 5231 Jul 24 13:02 mime.types
lrwxrwxrwx 1 root root 22 Jul 24 13:02 modules -> /usr/lib/nginx/modules
-rw-r--r-- 1 root root 643 Jul 24 13:02 nginx.conf
-rw-r--r-- 1 root root 636 Jul 24 13:02 scgi_params
-rw-r--r-- 1 root root 664 Jul 24 13:02 uwsgi_params
-rw-r--r-- 1 root root 3610 Jul 24 13:02 win-utf
root@29df10f32d44:/etc/nginx#
4、 -v 挂在本地目录到容器中
4.1、建议启动的时候挂载 ::ro 表示分配给只读权限(这样容器就可以使用宿主主机的目录了)
docker run -p 80:80 -v /Users/healerjean/Desktop:/usr/local/mynginx:ro -d nginx
JeandeMBP:~ healerjean$ docker run -p 80:80 -it -v /Users/healerjean/Desktop:/usr/local/mynginx:ro -d nginx
f3a39301086b999f4bcd9ccddcd83672007be0280889c3f4f0c1fc6bc45b0db4
JeandeMBP:~ healerjean$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3a39301086b nginx "nginx -g 'daemon of…" 2 seconds ago Up 7 seconds 0.0.0.0:80->80/tcp sharp_varahamihira
JeandeMBP:~ healerjean$ docker exec -it f3a39301086b /bin/bash
root@f3a39301086b:/# cd /usr/local/mynginx/
root@f3a39301086b:/usr/local/mynginx# ls -l
total 35824
-rw-r--r-- 1 root root 19915 Jan 25 2018 1???.docx
-rw-r--r-- 1 root root 18885250 Aug 28 06:58 2018-07.xlsx
-rw-r--r-- 1 root root 17711506 Aug 28 06:55 2018-08.xlsx
-rw-r--r-- 1 root root 1519 Jul 27 08:08 AAA.md
-rw-r--r-- 1 root root 0 Jul 17 09:05 AAA.sql
-rw-r--r-- 1 root root 0 Jul 26 09:22 AAAAA.txt
-rw-r--r-- 1 root root 972 Mar 8 10:56 DDKJ
-rw-r--r-- 1 root root 671 Aug 11 07:00 HttpHelper.java
-rw-r--r-- 1 root root 936 Mar 6 06:43 _posts
-rw-r--r-- 1 root root 1542 Aug 24 09:01 default
drwxr-xr-x 4 root root 136 Aug 16 07:42 images
drwx------ 20 root root 680 Aug 28 03:21 study
drwxr-xr-x 15 root root 510 Aug 20 11:11 workspace
drwxr-xr-x 23 root root 782 Aug 2 03:59 youhui-h5
root@f3a39301086b:/usr/local/mynginx#
Docker部署nginx并修改配置文件
docker 部署个nginx,简直太简单了好吧
直接一行命令搞定:
docker run \
--name nginx-health-web-pc \
-d -p 6800:80 \
-v /usr/docker/nginx/html:/usr/share/nginx/html \
nginx
运行启动不亦乐乎~~~~~这时候忽然前端过来说:“你的nginx里得加一个配置”,顺带还告诉你:“某某某以前就是这样配的",
此时好胜的你当然不能拒绝,但是真正配置起来还是要费点心思的,一般情况下docker启动时进行配置,只要把配置文件的目录挂载出来就可以,简洁方便,但是nginx却是先加载一个主配置文件nginx.conf,在nginx.conf里再加载conf.d目录下的子配置文件(一般最少一个default.conf文件)。这比单独挂载一个目录麻烦了不少,但只要思路清晰,倒也不难。
我们先看挂载好的命令:
启动docker的命令
docker run \
--name myNginx \
-d -p 80:80 \
-v /usr/docker/myNginx/html:/usr/share/nginx/html \
-v /etc/docker/myNginx/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /etc/docker/myNginx/conf.d:/etc/nginx/conf.d \
nginx
这里有几个注意事项:
(1)第一个“-v”,是项目位置,把项目放到挂载到的目录下即可;
(2)第二个“-v”,是挂载的主配置文件"nginx.conf",注意"nginx.conf"文件内有一行"include /etc/nginx/conf.d/*.conf;",这个include指向了子配置文件的路径,此处注意include后所跟的路径一定不要出错。
(3)第三个“-v”,把docker内子配置文件的路径也挂载了出来,注意要与(2)中include指向路径一致
(4)重点强调一下,nginx.conf是挂载了一个文件(docker是不推荐这样用的),conf.d挂载的是一个目录
我们先启动一下,可以发现是有问题的,因为配置文件还没有。
配置配置文件
我们找到常规方法安装的nginx时生成的配置文件(一般以“/etc/nginx”下),对应上面启动命令中的挂载位置,把主配置文件nginx.conf放到对应位置“/etc/docker/myNginx/nginx.conf”,把子配置文件“default.conf”放到“/etc/docker/myNginx/conf.d”目录下
重新运行启动命令,发现已经好了,至此docker中的文件已经可以随意配置,跟原生安装是一模一样的
思路:配置时一定要铆定一个思路:挂载出来的文件运行时是要加载到docker进程中去的!这样就不容易混淆。
---------------------------------------------------------------分隔线---------------------------------------------------------------------
贴出我的配置文件:
nginx.conf
user root;
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;
autoindex on;
#gzip on;
include /etc/nginx/conf.d/*.conf;
client_max_body_size 100M;
client_header_buffer_size 128k;
large_client_header_buffers 4 128k;
}
default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/nginx/dacheng-wechat-web;
# root /usr/nginx/html;
index index.html index.htm;
autoindex on;
try_files $uri /index/index/page.html;
#try_files $uri /index/map/page.html;
}
#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;
#}
}