I am trying to setup ruby on rails with docker everything is good but i want dynamic domain pass as environment variable to nginx.conf file during build image by docker-compose command but i don't know how to do it.
i trying to use this command
Docker-compose build
dcoker-compose up
Docker File
FROM ruby:2.7.2
ENV RAILS_ROOT /var/www/quickcard
ENV BUNDLE_VERSION 2.1.4
ENV BUNDLE_PATH usr/local/bundle/gems
ENV RAILS_LOG_TO_STDOUT true
ENV RAILS_PORT 5000
COPY ./entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq && apt-get install -y build-essential \
git \
libxml2-dev \
libpq-dev \
libxslt-dev \
nodejs \
yarn \
imagemagick \
tzdata \
less \
cron \
&& rm -rf /var/cache/apk/*
RUN gem install bundler --version "$BUNDLE_VERSION"
RUN mkdir -p $RAILS_ROOT
WORKDIR $RAILS_ROOT
ADD Gemfile Gemfile
ADD Gemfile.lock Gemfile.lock
COPY yarn.lock yarn.lock
RUN bundle install
EXPOSE $RAILS_PORT
RUN ln -s $RAILS_ROOT/config/systemd/puma.service /etc/systemd/system/quickcard
COPY . .
RUN crontab -l | { cat; echo ""; } | crontab -
RUN yarn install
RUN yarn install --check-files
RUN ls /var/www/quickcard/public
ENTRYPOINT ["entrypoint.sh"]
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
Nginx Docker File
FROM nginx
RUN apt-get update -qq && apt-get -y install apache2-utils
ENV RAILS_ROOT /var/www/quickcard
WORKDIR $RAILS_ROOT
RUN mkdir log
COPY public public/
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
COPY ./multi_quickcard.key /etc/nginx/multi_quickcard.key
COPY ./quickcard-ssl-test.pem /etc/nginx/quickcard-ssl-test.pem
EXPOSE 80 443
CMD [ "nginx", "-g", "daemon off;" ]
Nginx.conf e.g
upstream puma {
# Path to Puma SOCK file, as defined previously
server app:5000 fail_timeout=0;
}
server {
listen 80;
server_name default_server;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
location / {
root /var/www/quickcard/public/;
proxy_pass http://puma;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location /api {
root /var/www/quickcard/public/;
proxy_pass http://puma;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location ^~ /assets/ {
root /var/www/quickcard/public/;
gzip_static on;
expires max;
add_header Cache-Control public;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
Docker Compose File
version: '2.2'
services:
app:
build:
context: .
dockerfile: ./Dockerfile
command: bash -c "bundle exec rails s -p 5000 -e production -b 0.0.0.0 && RAILS_ENV=production bundle exec rake assets:precompile"
environment:
RAILS_ENV: production
volumes:
- /var/wwww/quickcard
- /var/wwww/quickcard/public
ports:
- 5000:5000
sidekiq:
build: .
command: bundle exec sidekiq -C config/sidekiq.yml
environment:
RAILS_ENV: production
volumes:
- /var/wwww/quickcard/tmp
cron_job:
build: .
command: cron -f
nginx:
build:
context: .
dockerfile: ./nginx.Dockerfile
volumes:
- ./log-nginx:/var/log/nginx/
restart: always
ports:
- 80:80
- 443:443
nginx Docker image can extract environment variables before it starts, but it's a bit tricky. One solution is to:
Add env variables to your nginx.conf file.
Copy it to /etc/nginx/templates/nginx.conf.template in the container (as opposed to your normal /etc/nginx) in the build step or as a volume.
Set the NGINX_ENVSUBST_OUTPUT_DIR: /etc/nginx environment variable in docker-compose.yml.
This will cause the nginx.conf.template file to be copied to /etc/nginx as nginx.conf and the environment variables will be replaced with their values.
There is one caveat to keep in mind: using command property in docker-compose.yml seems to be disabling the extraction functionality. If you need to run a custom command to start-up nginx, you can use the Dockerfile version.
I created a repo with the full setup, but in case it's not available:
# docker-compose.yml
version: "3"
services:
nginx-no-dockerfile:
container_name: nginx-no-dockerfile
image: nginx:1.23.1-alpine
ports:
- 8081:80
volumes:
- ./site/index.html:/usr/share/nginx/html/index.html
- ./site/nginx.conf:/etc/nginx/templates/nginx.conf.template
working_dir: /usr/share/nginx/html
environment:
NGINX_ENVSUBST_OUTPUT_DIR: /etc/nginx
API_URL: http://example.com
nginx-with-dockerfile:
container_name: nginx-with-dockerfile
build:
context: ./site
dockerfile: ./Dockerfile
ports:
- 8082:80
volumes:
- ./site/index.html:/usr/share/nginx/html/index.html
environment:
NGINX_ENVSUBST_OUTPUT_DIR: /etc/nginx
API_URL: http://example.com
# site/nginx.conf
worker_processes auto;
events {
}
http {
include /etc/nginx/mime.types;
server {
listen 80;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
location /example {
proxy_pass $API_URL;
}
}
}
# site/Dockerfile
FROM nginx:1.23.1-alpine
WORKDIR /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx/templates/nginx.conf.template
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Run no Dockerfile version: docker compose up nginx-no-dockerfile
Run Dockerfile version:
docker compose build nginx-with-dockerfile
docker compose up nginx-with-dockerfile
Make sure to also have index.html file in the site folder.
The recommended approach Nginx seems to be to use the envsubst utility. You would need to create a template file with the variable placed inside as $Variable or {Variable}. You could then pass into envsubst the template file which would render the variable from the environment.
This has a few downsides in that it can potentially replace nginx variables unintentionally so I would make sure to pass in the specific variables you would want to replace.
See this question which addresses a similar problem for more details: https://serverfault.com/questions/577370/how-can-i-use-environment-variables-in-nginx-conf
Related
I have a project with the following elements:
frontend - Angular
backend - Laravel
DB - AWS RDS
I want to make to dockerize this project locally and I have several questions:
Is it possible to have only 1 docker NGINX service which will work with frontend and backend without using volumes?
When I will write at enironment of angular the route for the backend, like: http://backend/api/login - should it work?
When I'm trying to open localhost:8050 - I can see the frontend part and the request can't reach the backend container. Please advice
What is the best practise in this case to use at cloud solutions: to use shared drive with 2 folders for frontend and backend and mount them at each container or something else?
Docker-compose
version: '3'
services:
nginx-frontend:
restart: always
build:
dockerfile: dockerfile
context: ./nginx
ports:
- '8050:80'
backend:
build:
dockerfile: dockerfile
context: ./backend
ports:
- '1000:80'
Nginx configuration
server {
listen 80;
# Log files for Debug
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
# Laravel web root directory
root /var/www/html/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
# Nginx Pass requests to PHP-FPM
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass backend:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Nginx-frontend dockerfile
FROM node:14.17.6 as build
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY ./frontend/code/package.json ./frontend/code/package-lock.json ./
RUN apt-get update || : && apt-get install python -y
RUN npm install node-sass -y
RUN npm rebuild node-sass
RUN npm install
COPY ./frontend/code .
RUN npm run build
FROM nginx
COPY ./default.conf /etc/nginx/conf.d/default.conf
COPY --from=build /usr/src/app/dist /var/www/html
Backend dockerfile
FROM php:7.4-fpm as php-build
RUN apt-get -y update
RUN apt-get -y install curl
RUN apt-get -y install zip
RUN apt-get -y install libzip-dev
RUN apt-get -y install libpng-dev
RUN docker-php-ext-install zip
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
RUN docker-php-ext-configure gd && docker-php-ext-install gd
COPY ./code /var/www/html
WORKDIR /var/www/html
RUN composer require "ext-gd:*" --ignore-platform-reqs
RUN composer require phpoffice/phpspreadsheet --with-all-dependencies
RUN composer install
RUN chmod -R 777 /var/www/html/storage/
RUN chmod -R 777 /var/www/html/bootstrap/cache
FROM nginx
COPY ./default.conf /etc/nginx/conf.d/default.conf
COPY --from=php-build /var/www/html /var/www/html
The configuration files are provided.
docker file
FROM ruby:2.7.2
ENV RAILS_ROOT /var/www/quickcard
ENV BUNDLE_VERSION 2.1.4
ENV BUNDLE_PATH usr/local/bundle/gems
ENV RAILS_LOG_TO_STDOUT true
ENV RAILS_PORT 5000
COPY ./entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
RUN apt-get update -qq && apt-get install -y build-essential \
git \
libxml2-dev \
libpq-dev \
libxslt-dev \
nodejs \
yarn \
imagemagick \
tzdata \
less \
&& rm -rf /var/cache/apk/*
RUN gem install bundler --version "$BUNDLE_VERSION"
RUN bundle config set path $BUNDLE_PATH
RUN mkdir -p $RAILS_ROOT
WORKDIR $RAILS_ROOT
ADD Gemfile Gemfile
ADD Gemfile.lock Gemfile.lock
RUN bundle install
EXPOSE $RAILS_PORT
RUN ln -s $RAILS_ROOT/config/systemd/puma.service /etc/systemd/system/current
COPY . .
ENTRYPOINT ["entrypoint.sh"]
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
docker compose file
version: '2.2'
services:
app:
build:
context: .
dockerfile: ./Dockerfile
command: ['bundle','exec','rails','s','-p','5000','-e','production', '-b', '0.0.0.0']
volumes:
- /var/wwww/quickcard
ports:
- "5000:5000"
sidekiq:
build: .
command: bundle exec sidekiq -C config/sidekiq.yml
volumes:
- '/var/wwww/quickcard/tmp'
nginx:
build:
context: .
dockerfile: ./nginx.Dockerfile
ports:
- 80:80
links:
- app
nginx conf file
upstream app {
server app:5000;
}
server {
listen 80;
server_name quickcard;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
location / {
root /var/www/quickcard/public;
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location /api {
root /var/www/quickcard/public/;
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location ^~ /assets/ {
root /var/www/quickcard/public/;
gzip_static on;
expires max;
add_header Cache-Control public;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
I want everything on production with docker there are not a single puma configuration or service file on production then how to check puma status if puma start or not?
if i am hit ip of server then there is an error site can't be reached. how to configure nginx with application and with puma on production with docker-file. i don't want to set everything manually like puma service file
I am trying to setup ruby on rails application from docker everything is fine there is only one issue assets not load in application in docker. Please tell me how to resolve this issue.
Asset compile log
** Execute assets:precompile
I, [2022-06-21T14:19:36.070795 #8] INFO -- : Writing /var/www/quickcard/public/assets/admins_stylesheet-ee5c0de3bf28ba61ee5aae648b5394561912c52f237e45b9afd8b1dbaba97ddd.css
I, [2022-06-21T14:19:36.071640 #8] INFO -- : Writing /var/www/quickcard/public/assets/admins_stylesheet-ee5c0de3bf28ba61ee5aae648b5394561912c52f237e45b9afd8b1dbaba97ddd.css.gz
I, [2022-06-21T14:19:36.072396 #8] INFO -- : Writing /var/www/quickcard/public/assets/flags-38025784bedeb5e4cae496b131c85cabbd95ae0b1c0a3c9d9cb474d7262db04b.png
I, [2022-06-21T14:19:36.074876 #8] INFO -- : Writing /var/www/quickcard/public/assets/flags#2x-e21becc4db003c08194a77f3311709fc0002a99be5dd18e8a5ba661c0472dc35.png
I, [2022-06-21T14:19:36.582968 #8] INFO -- : Writing /var/www/quickcard/public/assets/application-4bcda1a2ad86133d1f3e33b2c53561990c0bd5c5e237e227357708bb00a21e7d.css
I, [2022-06-21T14:19:36.583732 #8] INFO -- : Writing /var/www/quickcard/public/assets/application-4bcda1a2ad86133d1f3e33b2c53561990c0bd5c5e237e227357708bb00a21e7d.css.gz
I, [2022-06-21T14:19:36.585850 #8] INFO -- : Writing /var/www/quickcard/public/assets/img/main3-9d715e6c156473fea20e2df822b880ce16f8d57b2de6e50e56d0bea18e9f2a27.jpg
Docker File
FROM ruby:2.7.2
ENV RAILS_ROOT /var/www/quickcard
ENV BUNDLE_VERSION 2.1.4
ENV BUNDLE_PATH usr/local/bundle/gems
ENV RAILS_LOG_TO_STDOUT true
ENV RAILS_PORT 5000
COPY ./entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq && apt-get install -y build-essential \
git \
libxml2-dev \
libpq-dev \
libxslt-dev \
nodejs \
yarn \
imagemagick \
tzdata \
less \
&& rm -rf /var/cache/apk/*
RUN gem install bundler --version "$BUNDLE_VERSION"
RUN bundle config set path $BUNDLE_PATH
RUN mkdir -p $RAILS_ROOT
WORKDIR $RAILS_ROOT
ADD Gemfile Gemfile
ADD Gemfile.lock Gemfile.lock
COPY yarn.lock yarn.lock
RUN bundle install
EXPOSE $RAILS_PORT
RUN ln -s $RAILS_ROOT/config/systemd/puma.service /etc/systemd/system/quickcard
COPY . .
RUN yarn install
RUN yarn install --check-files
RUN bundle exec rake assets:precompile --trace
RUN ls /var/www/quickcard/public
ENTRYPOINT ["entrypoint.sh"]
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
Compose File
version: '2.2'
services:
app:
build:
context: .
dockerfile: ./Dockerfile
command: ['bundle','exec','rails','s','-p','5000','-e','development', '-b', '0.0.0.0']
# environment:
# RAILS_ENV: production
volumes:
- /var/wwww/quickcard
- /var/wwww/quickcard/public
ports:
- 5000:5000
sidekiq:
build: .
command: bundle exec sidekiq -C config/sidekiq.yml
# environment:
# RAILS_ENV: production
volumes:
- /var/wwww/quickcard/tmp
nginx:
build:
context: .
dockerfile: ./nginx.Dockerfile
volumes:
- ./log-nginx:/var/log/nginx/
ports:
- 80:80
Nginx conf file
upstream puma {
server app:5000 fail_timeout=0;
}
server {
listen 80 default_server deferred;
index index.html index.htm;
access_log /var/www/quickcard/log/nginx.access.log;
error_log /var/www/quickcard/log/nginx.error.log;
location / {
root /var/www/quickcard/public;
proxy_pass http://puma;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location /api {
root /var/www/quickcard/public/;
proxy_pass http://puma;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location ^~ /assets/ {
rewrite /var/www/quickcard(/assets/.*) $1;
root /var/www/quickcard/public/;
gzip_static on;
expires max;
add_header Cache-Control public;
add_header Strict-Transport-Security "";
}
# location ^~ /assets/ {
# root /var/www/quickcard/public/;
# gzip_static on;
# expires max;
# add_header Cache-Control public;
# }
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
This configuration was tested without docker. The site on SSL was launched without errors. Now when I want to run this server configuration in docker, I get no errors during installation, but the server does not start at all.
nginx.dockerfile
FROM nginx:stable-alpine
RUN mkdir -p /var/www/html
WORKDIR /var/www/html
RUN addgroup -g 1000 laravel && adduser -G laravel -g laravel -s /bin/sh -D laravel
RUN chown laravel:laravel /var/www/html
COPY ./nginx/ssl/mysite.ru/mysite_ru.crt /etc/nginx/ssl/mysite.ru/mysite_ru.crt
COPY ./nginx/ssl/mysite.ru/mysite_ru.key /etc/nginx/ssl/mysite.ru/mysite_ru.key
RUN apk update \
&& ln -sf ./nginx/ssl/mysite.ru /etc/nginx/ssl/mysite.ru
ADD ./nginx/nginx.conf /etc/nginx/nginx.conf
ADD ./nginx/default.conf /etc/nginx/conf.d/default.conf
the ./nginx/ssl/mysite_com folder contains working files: mysite_com. crt and mysite_com.key
This files checked without docker
docker-compose.yml
services:
site:
build:
context: .
dockerfile: nginx.dockerfile
container_name: nginx
ports:
- 80:80
- 443:443
volumes:
- ./src:/var/www/html:delegated
- ./nginx/ssl:/etc/nginx/ssl
depends_on:
- php
- mysql
- postgres
networks:
- laravel
default.conf
server {
listen 443 ssl;
server_name mysite.ru;
ssl_certificate /etc/nginx/ssl/mysite.ru/mysite_ru.crt;
ssl_certificate_key /etc/nginx/ssl/mysite.ru/mysite_ru.key;
index index.php index.html;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Tell me, what is the error?
and where can I view nginx logs in docker?
It seems you have not added or copied your SSL cert and key files to your nginx image.
Add COPY keyword before RUN apk update ... and then restart nginx at the end so that your Dockerfile looks like this and I think it should solves your problem:
FROM nginx:stable-alpine
RUN mkdir -p /var/www/html
WORKDIR /var/www/html
RUN addgroup -g 1000 laravel && adduser -G laravel -g laravel -s /bin/sh -D laravel
RUN chown laravel:laravel /var/www/html
COPY mysite_com.crt /etc/nginx/ssl/mysite.com/
COPY mysite_com.key /etc/nginx/ssl/mysite.com/
RUN apk update \
&& ln -sf ./nginx/ssl/mysite_com /etc/nginx/ssl
ADD ./nginx/nginx.conf /etc/nginx/nginx.conf
ADD ./nginx/default.conf /etc/nginx/conf.d/default.conf
We have two docker containters. one running our angular app and one running our laravel api. Each has their own docker-compose file.
On our localhost there was no issue making api calls from angular to laravel over 127.0.0.1:3000
Then I took these containers and started them up on my Ubuntu server. Still no problem making calls over 195.xxx.xxx.xx:3000
I then added a ssl certificate to the domain and all of the sudden I can not make calls to the api over port 3000
Can anyone tell me where I am going wrong. I have tried different ports. If I remove the certbot stuff and call over http it all works fine again. Please please help...
For my ssl setup I followed this article and got it all setup without any real issues
Here is to docker setup for laravel
Dockerfile:
FROM php:7.3-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
mariadb-client \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
libzip-dev
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# Change current user to www
USER www
# Expose port 3000 and start php-fpm server
EXPOSE 3000
CMD php-fpm
docker-compose.yml
version: "3"
services:
#PHP Service
api:
build:
context: .
dockerfile: Dockerfile
image: laravel360
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "3000:80"
- "3001:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.22
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: name
MYSQL_ROOT_PASSWORD: password
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
Any finally the config file
server {
listen 80;
client_max_body_size 100M;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
server {
listen 443 ssl;
client_max_body_size 100M;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
Angular Docker
#############
### build ###
#############
# base image
FROM node:alpine as build
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package.json /app/package.json
RUN npm install
RUN npm install -g #angular/cli#~9.1.0
# add app
COPY . /app
# run tests
# RUN ng test --watch=false
# RUN ng e2e --port 4202
# generate build
RUN ng build --output-path=dist
FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html
EXPOSE 80 443
CMD [ "nginx", "-g", "daemon off;" ]
Docker Compose
version: '3'
services:
angular:
container_name: angular
build:
context: .
dockerfile: Dockerfile-prod
ports:
- "80:80"
- "443:443"
volumes:
- ./data/nginx:/etc/nginx/conf.d
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
certbot:
image: certbot/certbot
volumes:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
And then finally my nginx conf for the angular side
server {
listen 80;
server_name mydomaindotcom;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri /index.html;
}
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
}
server {
listen 443 ssl;
server_name mydomaindotcom;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri /index.html
proxy_pass http://mydomaindotcom; #for demo purposes
proxy_set_header Host http://mydomaindotcom;
}
ssl_certificate /etc/letsencrypt/live/mydomaindotcom/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mydomaindotcom/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}