Docker Nginx – Can’t Turn SSL On in Nginx Container

alpinecertbotdockernginxssl-certificate

I'm new to docker and try to build an image based on Alpine to take care of my websites with HTTPS connexions.
Currently i'm at the step to make it work from the container so i'm not finished yet with my Dockerfile
my Dockerfile :

FROM alpine


RUN apk update \
        && apk upgrade \
        && apk add nginx php7 php7-fpm php7-opcache php7-gd php7-mysqli php7-zlib php7-curl \
        && mkdir /run/nginx
EXPOSE 80

From there i start my container :

docker run -ti -p 80:80 -p 443:442 -v /home/docker/web/conf:/etc/nginx/conf.d/ -v /home/docker/web/www:/var/www/localhost/htdocs test

I then install certbot-nginx, and start it to generate my certificate, which i copy in my /var/www/localhost/htdocs/example.fr/ to access it from outside the container.
Finally i start php-fpm7 & nginx.

My nginx config :

server {
        server_name example.fr;
        root /var/www/localhost/htdocs/example.fr;
        index index.html index.htm index.php;
        listen [::]:443 ssl;
        server_tokens off;
        # configure php
        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
                index index.html index.htm index.php;
        }

        location ~* \.pem {
                deny all;
        }

        ssl_certificate /var/www/localhost/htdocs/example.fr/fullchain.pem; 
        ssl_certificate_key /var/www/localhost/htdocs/example.fr/privkey.pem; # managed by Certbot

}

server {
    if ($host = example.fr) {
        return 301 https://$host$request_uri;
    }
       # managed by Certbot
       server_name example.fr;
       listen 80;
       listen [::]:80;
       #return 404;
}

I'm probably missing something simple but i'm stuck since a week.
thanks !

Best Answer

Rather than re-invent the wheel, perhaps take a look at some boilerplate projects such as:

https://github.com/wmnnd/nginx-certbot

This covers your needs:

  • nginx
  • certbot