Latest web development tutorials

Docker install Nginx

Docker install Nginx

A method by constructing Dockerfile

Create Dockerfile

First, create a directory nginx, used to store back-related stuff.

w3big@w3big:~$ mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf

www directory to map the virtual directory nginx configuration container

nginx logs directory will be mapped to the container log directory

conf directory profile will be mapped to the profile nginx container

Enter nginx directory created, create Dockerfile

FROM debian:jessie

MAINTAINER NGINX Docker Maintainers "[email protected]"

ENV NGINX_VERSION 1.10.1-1~jessie

RUN apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 \
        && echo "deb http://nginx.org/packages/debian/ jessie nginx" >> /etc/apt/sources.list \
        && apt-get update \
        && apt-get install --no-install-recommends --no-install-suggests -y \
                                                ca-certificates \
                                                nginx=${NGINX_VERSION} \
                                                nginx-module-xslt \
                                                nginx-module-geoip \
                                                nginx-module-image-filter \
                                                nginx-module-perl \
                                                nginx-module-njs \
                                                gettext-base \
        && rm -rf /var/lib/apt/lists/*

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
        && ln -sf /dev/stderr /var/log/nginx/error.log

EXPOSE 80 443

CMD ["nginx", "-g", "daemon off;"]

Create a mirror through Dockerfile, replace it with your own name

docker build -t nginx .

Once created, we can find the image you just created in the local mirror list

w3big@w3big:~/nginx$ docker images nginx
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              555bbd91e13c        3 days ago          182.8 MB

Method two, docker pull nginx

Find nginx mirror on Docker Hub

w3big@w3big:~/nginx$ docker search nginx
NAME                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                     Official build of Nginx.                        3260      [OK]       
jwilder/nginx-proxy       Automated Nginx reverse proxy for docker c...   674                  [OK]
richarvey/nginx-php-fpm   Container running Nginx + PHP-FPM capable ...   207                  [OK]
million12/nginx-php       Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS...   67                   [OK]
maxexcloo/nginx-php       Docker framework container with Nginx and ...   57                   [OK]
webdevops/php-nginx       Nginx with PHP-FPM                              39                   [OK]
h3nrik/nginx-ldap         NGINX web server with LDAP/AD, SSL and pro...   27                   [OK]
bitnami/nginx             Bitnami nginx Docker Image                      19                   [OK]
maxexcloo/nginx           Docker framework container with Nginx inst...   7                    [OK]
...

Here we pull the official mirrors

w3big@w3big:~/nginx$ docker pull nginx

Wait for the download is complete, we can be found in local mirror list REPOSITORY as nginx mirror.


Mirroring using nginx

Run container

w3big@w3big:~/nginx$ docker run -p 80:80 --name mynginx -v $PWD/www:/www -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs  -d nginx  
45c89fab0bf9ad643bc7ab571f3ccd65379b844498f54a7c8a4e7ca1dc3a2c1e
w3big@w3big:~/nginx$

Command Description:

  • -p 80:80: will be mapped to the host port 80 of the container port 80

  • --name mynginx: name the container mynginx

  • -v $ PWD / www: / www : www is the host of the current directory is mounted to the container / www

  • -v $ PWD / conf / nginx.conf: /etc/nginx/nginx.conf: nginx.conf will host current directory is mounted to the container /etc/nginx/nginx.conf

  • -v $ PWD / logs: / wwwlogs : logs will host current directory is mounted to the container / wwwlogs

Check the container starts circumstances

w3big@w3big:~/nginx$ docker ps
CONTAINER ID        IMAGE        COMMAND                      PORTS                         NAMES
45c89fab0bf9        nginx        "nginx -g 'daemon off"  ...  0.0.0.0:80->80/tcp, 443/tcp   mynginx
f2fa96138d71        tomcat       "catalina.sh run"       ...  0.0.0.0:81->8080/tcp          tomcat

Accessed through a browser