Docker挂载安装Nginx

Docker 安装Nginx

查看镜像

docker search nginx
[root@ysl ~]# docker search nginx
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        14172               [OK]                
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1929                                    [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   797                                     [OK]
linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   134                                     
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho…   120                                     
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp…   106                                     [OK]
bitnami/nginx                      Bitnami nginx Docker Image                      91                                      [OK]
alfg/nginx-rtmp                    NGINX, nginx-rtmp-module and FFmpeg from sou…   82                                      [OK]
jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        77                                      [OK]
nginxdemos/hello                   NGINX webserver that serves a simple page co…   64                                      [OK]
nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         46                                      
privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & Al…   44                                      [OK]
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  27                                      
schmunk42/nginx-redirect           A very simple container to redirect HTTP tra…   19                                      [OK]
staticfloat/nginx-certbot          Opinionated setup for automatic TLS certs lo…   16                                      [OK]
centos/nginx-112-centos7           Platform for running nginx 1.12 or building …   15                                      
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       15                                      
centos/nginx-18-centos7            Platform for running nginx 1.8 or building n…   13                                      
raulr/nginx-wordpress              Nginx front-end for the official wordpress:f…   13                                      [OK]
flashspys/nginx-static             Super Lightweight Nginx Image                   8                                       [OK]
mailu/nginx                        Mailu nginx frontend                            8                                       [OK]
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr…   7                                       [OK]
bitwarden/nginx                    The Bitwarden nginx web server acting as a r…   7                                       
wodby/nginx                        Generic nginx                                   1                                       [OK]
ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                                       [OK]
[root@ysl ~]# 

拉取镜像

docker pull nginx
[root@ysl ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
6ec7b7d162b2: Pull complete 
cb420a90068e: Pull complete 
2766c0bf2b07: Pull complete 
e05167b6a99d: Pull complete 
70ac9d795e79: Pull complete 
Digest: sha256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

创建启动容器

 docker run --name nginxtest  -p 9000:80 -d nginx
docker run --name nginxtest  -p 9000:80 -d nginx
bf28824643ed39c054e80750b28e5b0d093d62663394851aba9027393d286efe

image-20201218133416590

nginx 启动成功

进入容器内部并去宿主机进行挂载

[root@ysl ~]# docker exec -it  bf28824643ed /bin/bash
[root@ysl ~]# docker exec -it  bf28824643ed /bin/bash
root@bf28824643ed:/# ls
bin  boot  dev	docker-entrypoint.d  docker-entrypoint.sh  etc	home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@bf28824643ed:/# cat /etc/nginx/nginx.conf #查看nginx配置文件

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn; #Nginx 日志存放位置
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;  # 引入了 /etc/nginx/conf.d/ 文件下所有的配置文件 
}
root@bf28824643ed:/#   
这个配置文件可以看出 
nginx的日志文件在 /var/log/nginx 目录下
很重要的一点 最后一行 引入了 /etc/nginx/conf.d/ 文件下所有的配置文件 
通过查看default.conf 文件的信息 默认的页面路径 /usr/share/nginx/html
回到宿主机 exit
[root@ysl nginx]# docker run --name DockerNginx -p 9000:80 -v /var/docker/nginx/config/nginx/:/etc/nginx -v /var/docker/nginx/project/dist:/usr/share/nginx/html -v /var/docker/nginx/logs/:/var/log/nginx -d nginx
6720e775ec337d33bdb0bc25f13b2a99b346095dcab91ec16274880590d71629
[root@ysl nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
6720e775ec33        nginx               "/docker-entrypoint.…"   6 seconds ago       Up 5 seconds        0.0.0.0:9000->80/tcp   DockerNginx
afcb21b99a90        delron/fastdfs      "/usr/bin/start1.sh …"   9 days ago          Up 9 days                                  storage
1d9e6bca9b48        delron/fastdfs      "/usr/bin/start1.sh …"   9 days ago          Up 9 days                                  tracker
a1a1ec80a208        centos              "/bin/bash"              11 days ago         Up 11 days                                 charming_ride
  1. 退出容器 创建需要挂载文件夹
root@bf28824643ed:/# exit
exit
[root@ysl ~]# cd /var/
[root@ysl var]# mkdir docker
[root@ysl var]# cd docker
[root@ysl docker]# mkdir nginx
[root@ysl docker]# cd nginx/
[root@ysl nginx]# mkdir config
[root@ysl nginx]# mkdir project
[root@ysl nginx]# mkdir logs
  1. 将nginx容器中的一些配置信息 挂载到刚创建的对应目录中去
[root@ysl nginx]# docker cp 容器id:/etc/nginx /var/docker/nginx/config/
[root@ysl nginx]# docker cp 容器id:/var/log/nginx /var/docker/nginx/logs/
[root@ysl nginx]# docker cp 容器id:/usr/share/nginx/html /var/docker/nginx/project/dist
  1. 删除刚刚创建的容器
[root@ysl nginx]# docker rm -f nginx
Error: No such container: nginx
[root@ysl nginx]# docker rm -f 容器id
  1. 启动容器
[root@ysl nginx]# docker run --name DockerNginx -p 9000:80 -v /var/docker/nginx/config/nginx/:/etc/nginx -v /var/docker/nginx/project/dist:/usr/share/nginx/html -v /var/docker/nginx/logs/:/var/log/nginx -d nginx

在挂载启动容器的时候很容易犯的错误就是挂载的时候,路径没有指定对,这样的话容器会启动不起来

只要没启动起来八成都是挂载指定配置文件的原因

测试

image-20201218141540800

部署项目

> 在再次部署项目时,需要重新启动下容器,不然会包403错误
>
> ```shell
> docker restart 容器id
> ```
原文地址:https://www.cnblogs.com/shiyisy/p/14154672.html