Skip to content

Commit

Permalink
Fix Dockerfile for NGINX on Cloud Run example (GoogleCloudPlatform#1415)
Browse files Browse the repository at this point in the history
* Fix Dockerfile for NGINX example

The current Dockerfile for the react-nginx-cloud-run example substitutes the `$PORT` template variable at build-time, instead of run-time. This appears to work *some of the time*. However, this does not match the Cloud Run docs that specify that the server must listen on the provided `$PORT`, which I presume can be set to something other than 8080 at run-time.

* copy-edit

Co-authored-by: Todd Kopriva <43478937+ToddKopriva@users.noreply.github.com>
  • Loading branch information
coryvirok and ToddKopriva committed Sep 11, 2020
1 parent e7eca53 commit 4ea0ae9
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions tutorials/deploy-react-nginx-cloud-run/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ Create a file in the root of the project named `nginx.conf` and add the followin
}

By creating this file, you provide the `$PORT` environment variable that Cloud Run expects your application to listen on.
By creating this file, you provide the `$PORT` environment variable that Cloud Run expects your application to listen on. The `$PORT`
environment variable will be provided by the `docker run` command, so the Nginx server configuration template needs to have its environment
variables substituted at run time, not at build time.

This file customizes Nginx so that `react-router-dom` always responds with the proper route. This configuarion enables gzip
compression, which makes the web application lightweight and fast.
Expand All @@ -85,12 +87,14 @@ Create a file named `Dockerfile` in the root folder of the project and paste the
# server environment
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/configfile.template

COPY --from=react-build /app/build /usr/share/nginx/html

ENV PORT 8080
ENV HOST 0.0.0.0
RUN sh -c "envsubst '\$PORT' < /etc/nginx/conf.d/configfile.template > /etc/nginx/conf.d/default.conf"
COPY --from=react-build /app/build /usr/share/nginx/html
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]
CMD sh -c "envsubst '\$PORT' < /etc/nginx/conf.d/configfile.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"


This configuration defines two environments: one where the web application is built and one where the web application
will run.
Expand Down

0 comments on commit 4ea0ae9

Please sign in to comment.