Cannot get SSL to work in Docker container Cannot get SSL to work in Docker container docker docker

Cannot get SSL to work in Docker container


Sometimes, especially when you have been trying to solve a problem for a long time and you're coding in a console window on the command line, you miss the simplest things. Have a look at these two lines in the Dockerfile:

ADD 000-default.conf /etc/apache2/sites-enabled/ADD default-ssl.conf /etc/apache2/sites-anabled/

The a is nearly indistinguishable from an e, making the misspelling hard to find. The Dockerfile builds the image correctly and adds the new directory in the container, then places the configuration file within that directory.

drwxr-xr-x  2 www www  4096 Nov 11 15:56 sites-anableddrwxr-xr-x  2 www www  4096 Oct 23 20:19 sites-availabledrwxr-xr-x  2 www www  4096 Nov 11 15:56 sites-enabled

Since no error is thrown for an unknown directory and the image builds successfully it will run, but in this case the SSL will not work appropriately.


I didn't want to 'add' files as other answers had done.and the HTML files would be installed via a mount to /var/www.All I needed was the apache server to accept HTTPS (SSL),redirect HTTP (non-SSL) to HTTPS, and some minor PHP configuration.

Here is what I did in my DockerFile...

# Install PHP Apache as the base imageFROM "php:7.3-apache"# Install ssl-cert (snakeoil) packageRUN set -eux; \    apt-get update && apt-get install -y ssl-cert; \    : "Clean Apt system" ;\    apt-get clean; \    rm -rf /var/lib/apt/lists/*; \    rm -rf /tmp/* /var/tmp/* /var/log/lastlog /var/log/faillog; \    rm -f /var/log/{apt/*,alternatives.log,dpkg.log}; \    :;# Set up apache with HTTPS redirectRUN set -eux; \    : "Time Zone"; \    ln -sf /usr/share/zoneinfo/Australia/Sydney /etc/localtime; \    : "Apache Changes" ;\    # enable SSL \    a2enmod rewrite ssl; \    a2ensite default-ssl; \    cd /etc/apache2/sites-enabled/; \    sed -i '/<\/VirtualHost>/d'                          000-default.conf; \    echo 'RewriteEngine On'                           >> 000-default.conf; \    echo 'RewriteRule . https://%{SERVER_NAME} [R,L]' >> 000-default.conf; \    echo '</VirtualHost>'                             >> 000-default.conf; \    :;# PHP ConfigurationRUN set -eux; \    cd $PHP_INI_DIR; \    cp php.ini-production php.ini; \    cd conf.d ;\    echo "date.timezone=Australia/Sydney"      > date_timezone.ini; \    echo "session.gc_probability=1"            > session.ini; \    echo "session.gc_divisor=100"             >> session.ini; \    echo "session.gc_maxlifetime=1440"        >> session.ini; \    echo "session.cookie=1440"                >> session.ini; \    :;

Hope you find this helpful