skip to Main Content

I have been trying build a Docker container to set Nginx webserver on my custom domain, e.g. example.com, using SSL. This is my Dockerfile:

FROM node:latest as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY ./ .
RUN npm run build

FROM nginx as production-stage
RUN mkdir /app
COPY --from=build-stage /app/dist /app
COPY nginx.conf /etc/nginx/nginx.conf

nginx.conf:

user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
  worker_connections  1024;
}
http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
  access_log  /var/log/nginx/access.log  main;
  sendfile        on;
  keepalive_timeout  65;
  server {
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    server_name example.com; # managed by Certbot

    location / {
      root   /app;
      index  index.html;
      try_files $uri $uri/ /index.html;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }
  }
  server {
    if ($host = example.com) {
      return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80 ;
    listen [::]:80 ;
    server_name example.com;
    return 404; # managed by Certbot
  }
}

Then:

sudo docker build . -t app
sudo docker run -v /etc/letsencrypt/:/etc/letsencrypt/:ro -d -p 443 app

However, when I visit http://example.com or https://example.com the website shows nothing, and then the connection times out. But when I visit the external IP address xx.xxx.xx.xxx/ the Nginx default page is shown. What am I doing wrong?

2

Answers


  1. Chosen as BEST ANSWER

    Solution was easier than I thought.

    First of all, I uninstalled nginx on the host machine, so as to allow connections on port 80 from the container (you don't really need to uninstall, just stop the nginx service with sudo service nginx stop).

    Then, the problem was on the following line, on the port parameter:

    sudo docker run -v /etc/letsencrypt/:/etc/letsencrypt/:ro -d -p 443 app

    Changed it from -p 443 to -p 80:80 -p 443:443 to enable both HTTP and HTTPS connections, and now it is working properly.


  2. There is a nginx installed on host and its listening on port 80. So used another port

    docker run -d -p 8080:80 my-app
    

    Then try to access http://example.com:8080 again.

    Or shut down nginx on host and use port 80.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search