DEV Community

Deploying containerized NginX to Heroku - how hard can it be?

Anniina Sallinen on November 02, 2020

I'm currently taking a Docker MOOC course. In part 3 of the course, there is an exercise about deploying a dockerized software to Heroku with CI/CD...
Collapse
 
davisruk profile image
davisruk

Thanks for this - I had the same problem and it put me on the right track.

One observation I have is that you don't really need to use ARG in your Dockerfile as it would only be available at image build time rather than container runtime. It's not actually doing anything in your file (the sed command gets run at container startup so the ARG is not actually available to it).

The reason that your solution is working is that Heroku creates a PORT environment variable when it instantiates the container (think -e when running docker containers locally) so you will always have access to this variable when the container is running on the Heroku platform.

However, if you run the container locally you have to provide the evironment variable yourself e.g. docker run -e PORT=4200 ...

If you don't want to do this then you can replace the ARG entry in your Dockerfile with an ENV statement and provide a default value eg. ENV PORT=4200. ENV values are available at container instantiation and can be overridden. Heroku will always override it and you can also run it locally without providing a command line environment variable.

Once again, thanks for the insight as it gave me what I needed to solve my issue.

Collapse
 
annisalli profile image
Anniina Sallinen

Thank you, I'm happy that I could help! 🤩

And you're also right about the ARG, thank you for noticing it. I actually think I just forgot to remove it after I realised the PORT environment variable is defined in runtime and not in build time in Heroku 😄

Very nice explanation about how to define the environment variable when you run the container locally 😊 I don't have that information in my blog post, but that is definitely useful information!

Collapse
 
narasimha1997 profile image
Narasimha Prasanna HN

Correct me if I'm wrong.
What is the use of deploying nginx and app within the same container? Basically if you're using nginx for load balancing, then it has to be a separate container and the backend microservice can be replicated across machines as containers. So you can achieve load balancing across machines.

Collapse
 
annisalli profile image
Anniina Sallinen • Edited

You are right! The app and load balancer would have a separate containers. In this case we don't have separate application, though.
In the Dockerfile RUN jekyll build generates static HTML pages, and in COPY --from=build-stage /usr/src/app/_site/ /usr/share/nginx/html we copy the generated pages for nginx to use. In nginx config, we tell nginx to serve those pages in

location / {
    root /usr/share/nginx/html;
    index index.html;
  }   
Enter fullscreen mode Exit fullscreen mode

Thus, we don't need any other containers.

Did I answer to your question? 😊

Collapse
 
narasimha1997 profile image
Narasimha Prasanna HN

Yes! Thanks 😀
Maybe I didn't go through the Dockerfile properly. For static sites this is a good approach.

Collapse
 
matrixx profile image
Saija Saarenpää

Nice article. I've been using Nginx as a proxy (jwilder/nginx-proxy docker image), but I'm unfamiliar with its use as a static web server, and also Heroku is new to me, but this gave me a nice glance what to wait for from them if I'm taking a closer acquaintance with them.

Collapse
 
annisalli profile image
Anniina Sallinen

Thank you Saija! I've used Heroku before for some school projects, it's in my opinion very nice service, it's easy to deploy and host your application there.

Nginx as webserver serving static web pages was a new for me as well 😊

Collapse
 
lauravuo profile image
Laura Vuorenoja

Sed is (too?) often your friend when writing infra scripts 😄

Collapse
 
udabasili profile image
Udbasili

Question though..wont it he cheaper to use an infrastructure like digital ocean to host since you can already create docker and nginx?

Collapse
 
armandasalmd profile image
Armandas Barkauskas

Saved my day! Thanks

Collapse
 
mazeem91 profile image
Mostafa Azeem

Thanks a lot, I spent more than 2 days to figure out the core issue without any luck