1:Docker pull 安装 Nginx
-
使用
docker search
命令搜索存放在 Docker Hub 中的镜像docker search nginx
(小知识:OFFICIAL:[ok] (代表这个镜像是官方镜像))
-
选定需要pull到系统中的官方 Nginx 镜像(如果不选择版本,默认选择最新版(latest))
# docker pull nginx -------- nginx 为选定需要pull到系统中的官方 nginx 镜像
docker pull nginx
- 启动docker中的nginx容器
docker run --name nginx -d -p 8008:80 -v /usr/docker/nginx/html:/usr/share/nginx/html nginx
- docker 启动 nginx 加载自定义配置
一般情况下docker启动时进行配置,只要把配置文件的目录挂载出来就可以,但是nginx却是先加载一个主配置文件nginx.conf,在nginx.conf里再加载conf.d目录下的子配置文件(一般最少一个default.conf文件)。
- docker 启动 nginx 加载自定义配置:
挂载Nginx配置与静态目录
说明 :-p表示递归创建文件夹,这里挂载是为了后面配置Nginx方便,不创建挂载后面配置Nginx需要进入容器配置比较麻烦,所以挂载到宿主机
mkdir -p /usr/docker/nginx/html
mkdir -p /etc/docker/nginx/{conf.d,logs}
# 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挂载的是一个目录
# 5. 第四个 “-v”是吧nginx的日志文件也挂载出来,方便查看
docker run \
--name nginx \
-d -p 8008:80 \
-v /usr/docker/nginx/html:/usr/share/nginx/html \
-v /etc/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /etc/docker/nginx/conf.d:/etc/nginx/conf.d \
nginx
docker run \
--name nginx \
-d -p 8008:80 --net host \
-v /usr/docker/nginx/html:/usr/share/nginx/html \
-v /etc/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /etc/docker/nginx/conf.d:/etc/nginx/conf.d \
nginx
注意!注意!注意!:
1、此处重中之重:
启动nginx容器时 一定要加 --net host 参数 (解释:容器将不会虚拟出自己的网卡,配置自己的IP等,而是使用宿主机的IP和端口。)
个人理解:如果不加此参数,nginx相当于是代理nginx镜像的IP及端口,因为nginx镜像也是独立的虚机,贴上此图,便于理解
- 首先创建这些文件夹及文件
- 准备挂载的 nginx.conf :
vim /etc/docker/nginx/nginx.conf
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;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/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 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;
#}
}
include /etc/nginx/conf.d/*.conf;
}
- 准备挂载的 default.conf :
vim /etc/docker/nginx/conf.d/default.conf
server {
listen 80;
listen [::]:80;
server {
listen 80;
listen [::]:80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
server {
listen 80;
listen [::]:80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/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;
#}
}
- 这样你就可以去外面配置nginx的配置了
- 最后提示:有些文件是需要自己创建的好,比括号里面的这个配置文件(/etc/docker/nginx/nginx.conf),等这些文件都配置好了,重新运行创建、启动容器命令(友情提示,如果一开始运行过创建、启动容器的话,可以先把那个容器删了,因为你容器名字重了,会报错!!!)
- 友情提示:如果创建容器成功,启动不了,可以先把配置文件挂载的去掉。先启动一个nginx,然后把配置文件copy出来一份
- copy命令
docker cp 容器id:容器的文件 需要拷贝到的地方
docker cp f47d1a7f4853:/etc/nginx/nginx.conf /etc/docker/nginx/nginx.conf
docker cp f47d1a7f4853:/etc/nginx/conf.d/default.conf /etc/docker/nginx/conf.d/default.conf
- 成功截图
2:最后总结(想了解更多就多看下)
启动一个名为nginx81(名字自己根据需求起名字,一般见名知意即可) 的容器
docker run --name nginx81 -d -p 80:80 -v /usr/docker/nginx/html:/usr/share/nginx/html nginx
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
默认容器对这个目录有可读写权限,可以通过指定ro,将权限改为只读(readonly)
docker run --name my-nginx -d -p 80:80 -v /usr/docker/nginx/html:/usr/share/nginx/html:ro -d nginx
- 列出运行中的容器
# 使用 docker ps 命令即可列出运行中的容器
docker ps
# 使用 docker ps -a 命令即可列出所有(包括已停止的)的容器
docker ps -a
- 列出已下载的镜像
# 使用 docker images 命令即可列出已下载的镜像
docker images
- 普通的挂载方式
# 普通的挂载方式
docker run --name mynginx2 --mount source=/var/www,target==/usr/share/nginx/html,readonly \
--mount source=/var/nginx/conf,target=/etc/nginx/conf,readonly -p 80:80 -d nginx
3:参考文章
参考文章地址
- https://my.oschina.net/u/3375733/blog/1591091
- https://blog.csdn.net/wangfei0904306/article/details/77623400?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param
- https://note.youdao.com/ynoteshare1/index.html?id=0d12819002db9df5127aa43b209f6f06&type=note