#!/bin/sh
set -e
Path=/data/nginx
VERSION="1.17.0"

#【1】############################################################### images
if [ "`docker images | grep nginx`" ]; then
    echo 'Detecting image exist. Will not pull again.'
    docker images nginx
else
    echo 'Begin to pull image ...'
    docker pull nginx:$VERSION
fi

#【2】############################################################### config
if [ -z "$DOCKER_NET" ]; then
  DOCKER_NET="default"
else
    echo "DOCKER_NET=$DOCKER_NET"
    DOCKER_NET=$DOCKER_NET
fi

mkdir -p $Path
wget -P $Path http://www.klquan.com/scripts/docker/nginx/nginx.tar.gz
cd $Path && tar xf nginx.tar.gz && rm -f nginx.tar.gz 

#【3】############################################################### run
docker run -d --restart=always -p 80:80 -p 443:443 --hostname nginx \
	-v $Path/conf/nginx.conf:/etc/nginx/nginx.conf \
	-v $Path/conf/conf.d:/etc/nginx/conf.d \
	-v $Path/log:/var/log/nginx \
	-v $Path/html:/usr/share/nginx/html \
        --name nginx nginx:$VERSION
echo 'nginx started. serving ...'
docker logs -f nginx

exit 0
