1 |
使用目的に合わせてNginx(1.18)を実行させたい |
それenvsubstで対応できますよ。
今回はenvsubstを使用して、Nginx1.18の設定ファイルに環境変数を利用する例を見ていきます。
1 |
envsubstで、Nginxの設定ファイルのテンプレート内の環境変数を置換します。 |
テンプレートファイルの例
$環境変数または${環境変数}を、テンプレートファイルに記述します。
1 2 3 4 |
server { listen $NGINX_PORT; ## 待受ポート番号 root $NGINX_ROOT; ## 静的ディレクトリ } |
ケース1)Dockerfile
1 2 3 4 5 6 7 8 9 |
FROM nginx:1.18.0-alpine #環境変数の値設定 ENV NGINX_PORT 8080 ENV NGINX_ROOT /var/www/html/ COPY ./html /var/www/html/ #テンプレートファイルをコピー COPY ./templates/*.template /tmp/ #テンプレートファイル置換実行結果をNginxの設定ファイルとして出力 RUN envsubst < /tmp/default.conf.template > /etc/nginx/conf.d/default.conf |
Dockerイメージのビルド
1 |
docker build . -t nginx |
Dockerコンテナ実行
1 |
docker run -p 8080:8080 nginx |
ケース2)DockerCompose
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
version: '3' services: nginx: image: nginx:1.18.0-alpine environment: NGINX_PORT: 8080 NGINX_ROOT: /var/www/html/ volumes: - ./html:/var/www/html/ - ./templates:/tmp/ ports: - 8080:8080 command: >- /bin/sh -c "envsubst < /tmp/default.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'" |
Dockerコンテナ実行例
1 |
docker-compose up |