[Nginx] Expose specified port for Nginx in Dockerfile

When we build a custom docker image based on nginx docker image, the default lisenting port will be 8080. However, port 8080 is a quite popular, so sometimes I want to give a different port for my service. The below snippet of code is the solution.

FROM nginx
COPY ./public/ /usr/share/nginx/html
EXPOSE 1313
CMD ["/bin/sh", "-c", "sed -i 's/listen  .*/listen 1313;/g' /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"]

If this post helped you to solve a problem or provided you with new insights, please upvote it and share your experience in the comments below. Your comments can help others who may be facing similar challenges. Thank you!
Buy Me A Coffee
DigitalOcean Referral Badge
Sign up to get $200, 60-day account credit !