Docker安装nginx

大家好最近正在学习docker。就想写一下笔记供大家参考。今天我们NGINX安装问题。

首先docker是一个容器。它分为主机和宿机。就好比docker中形容的一艘船上有很大多集装箱。船就相当我们的主机,集装箱就相当的我们的宿机。每个宿机都是独立的。里面都有自己的一套系统。在主机中宿机就相当主机中的一个进程。宿机可以共享主机资源。就我们常说的数据的挂载到宿机的相关目录。那我们现在切入正题。

  1. 搜索镜像
    docker search nginx
gongzgiyangdeMacBook-Air:www gongzhiyang$ docker search nginx
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                                                  Official build of Nginx.                        10744               [OK]                
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker con…   1508                                    [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable of…   676                                     [OK]
jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as p…   469                                     [OK]
webdevops/php-nginx                                    Nginx with PHP-FPM                              120                                     [OK]
kitematic/hello-world-nginx                            A light-weight nginx container that demonstr…   119                                     
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   86                                      [OK]
bitnami/nginx                                          Bitnami nginx Docker Image                      60                                      [OK]
linuxserver/nginx                                      An Nginx container, brought to you by LinuxS…   51                                      
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          48                                      [OK]
tobi312/rpi-nginx                                      NGINX on Raspberry Pi / armhf                   23                                      [OK]
nginx/nginx-ingress                                    NGINX Ingress Controller for Kubernetes         15                                      
blacklabelops/nginx                                    Dockerized Nginx Reverse Proxy Server.          12                                      [OK]
wodby/drupal-nginx                                     Nginx for Drupal container image                11                                      [OK]
centos/nginx-18-centos7                                Platform for running nginx 1.8 or building n…   10                                      
nginxdemos/hello                                       NGINX webserver that serves a simple page co…   9                                       [OK]
webdevops/nginx                                        Nginx container                                 8                                       [OK]
centos/nginx-112-centos7                               Platform for running nginx 1.12 or building …   6                                       
1science/nginx                                         Nginx Docker images that include Consul Temp…   4                                       [OK]
travix/nginx                                           NGinx reverse proxy                             2                                       [OK]
mailu/nginx                                            Mailu nginx frontend                            2                                       [OK]
pebbletech/nginx-proxy                                 nginx-proxy sets up a container running ngin…   2                                       [OK]
toccoag/openshift-nginx                                Nginx reverse proxy for Nice running on same…   1                                       [OK]
ansibleplaybookbundle/nginx-apb                        An APB to deploy NGINX                          0                                       [OK]
wodby/nginx                                            Generic nginx                                   0                                       [OK]
  1. 下载镜像
    docker pull nginx

  2. 查看镜像

gongzgiyangdeMacBook-Air:www gongzhiyang$  docker images nginx
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              7042885a156a        3 weeks ago         109MB

  1. 在使用镜像之前我们要配置一下本地的服务
    创建一系列的文件和目录如下
    这个 NGINX.conf 是主机中当前目录下的nginx.conf挂载到容器的/etc/nginx/nginx.conf

`Docker安装nginx_第1张图片

nginx.conf 文件摘要

这个就是挂载到容器的目录。
location / {
            root   /www;
            index  index.html index.htm;
        }
  1. 使用镜像
docker run  -p 81:81 --name webserver2 -v $PWD/www:/www -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs: /var/log/nginx  -d nginx

命令说明:

  • -p 81:81 -p: 端口映射,格式为:主机(宿主)端口:容器端口
    将容器的81端口映射到主机的81端口
  • –name webserver2 将容器命名为webserver2
  • -v $PWD/www:/www 将主机中当前目录下的www挂载到容器的/www
  • $PWD/conf/nginx.conf:/etc/nginx/nginx.conf 主机中当前目录下的nginx.conf挂载到容器的/etc/nginx/nginx.conf
  • $PWD/logs: /var/log/nginx 将主机中当前目录下的logs挂载到容器的/var/log/nginx
  • $pwd 标示当前的目录
gongzgiyangdeMacBook-Air:docker-www gongzhiyang$ pwd
/Users/gongzgiyang/docker-www
  1. 查看运行的容器
gongzgiyangdeMacBook-Air:docker-www gongzhiyang$ docker run -p 81:81 --name mynginx1 -v $PWD/www:/www -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs  -d nginx
44370a1b81cc4904a4f955aaf6c0810f1feddf5b05b3d8c7b09b71dc189c1075


gongzgiyangdeMacBook-Air:docker-www gongzhiyang$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                        NAMES
44370a1b81cc        nginx               "nginx -g 'daemon of…"   5 seconds ago       Up 4 seconds        80/tcp, 0.0.0.0:81->81/tcp   mynginx1
  1. 查看结果
    Docker安装nginx_第2张图片

到现在为止就完成了NGINX的搭建,我们在本地就可以控制配置文件和项目文件实现了同步。就聊到这了。谢谢大家。

你可能感兴趣的:(Docker)