LoginSignup
1
1

More than 3 years have passed since last update.

Docker + nginx のベースイメージの書き方

Posted at

Dockerfile

FROM nginx:1.15.12-alpine

# Install gettext 
RUN apk --no-cache add bash libintl perl curl && \
    apk --no-cache add --virtual .build gettext && \
    cp /usr/bin/envsubst /usr/local/bin/envsubst && \
    apk del .build 

ENV NGX_PORT 80
ENV NGX_HOSTNAME _
ENV NGX_LOG_LEVEL warn
ENV NGX_ACCESS_LOG_ENABLED 0 

ADD nginx /etc/nginx/
ADD root /root/

CMD ["sh", "-c", "/root/scripts/docker_start"]

/root/scripts/docker_start

#!/usr/bin/env bash 

# Inject EnvVars
files=`find /etc/nginx -type f -name "*.template"`

for file in $files; do
  envsubst "$(printf '${%s} ' ${!NGX_*})" < $file | tee $file.dist
done

# Replace configured settings
dists=`find /etc/nginx -type f -name "*.template.dist"`
for dist in $dists; do
  mv -v "$dist" "${dist/.template.dist/}";
done

nginx -g "daemon off;"

/etc/nginx/conf.d/default.conf.template

server {
    listen       ${NGX_PORT};
    server_name  ${NGX_HOSTNAME};

    access_log /var/log/nginx/access.log main if=${NGX_ACCESS_LOG_ENABLED};

    # For Healthcheck
    location /_status {
      access_log off;
      return 200 'ok';
    }

    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;
    }
}

あとがき

  • bashじゃないと ${!NGX_*}とかできないので注意。
  • NGX_ じゃなくても大丈夫。
  • bash + envsubstが快適
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1