Giammo
Giammo

Reputation: 53

Docker Lamp redirect https

I'm new to dockers. I installed docker lamp (mattrayner/lamp).

I put my project in /var/www/html and when I browse the project I am always redirected to https.

The problem is that with https localhost it gives me an error. With http, however, the project works.

Can anyone give me some suggestions?

Thank you all for your availability.

Upvotes: 0

Views: 1309

Answers (1)

JasonS
JasonS

Reputation: 275

I have finally managed to get the 'mattrayner/lamp:latest-1804' image running locally on HTTPS using a self-signed certificate,

I don't think this is appropriate for production ofcourse (since it is self-signed and you'd probably use a docker-compose file), but it enables local development on HTTPS for the application,

Here are the main details to get it running on your end:

The Solution

The directory structure of the solution is as follows:

Lamp
│   Dockerfile
│
└───app
│   │   index.php
│   
└───certs
│   │   docker.crt
│   |   docker.key
│   |   key.pem
└───ssl
│   │   custom-ssl.conf

The Dockerfile

FROM mattrayner/lamp:latest-1804

# Copy self-signed certificate onto ssl cert directory in docker container
COPY certs/docker.crt certs/docker.key /etc/ssl/certs/

# Copy custom ssl config file to apache virtual sites directory in docker container
COPY ssl/custom-ssl.conf /etc/apache2/sites-available/custom-ssl.conf

# Enable SSL, enable custom site and restart apache server
RUN a2enmod ssl && a2ensite custom-ssl.conf && service apache2 restart

# Copy application directory (containing index.php)
COPY /app .

# Set 'run.sh' as default command for docker container
CMD ["/run.sh"]

The Custom SSL Config File

<VirtualHost *:443>
  # Directory where webpages are stored
  DocumentRoot "/var/www/html"

  # Name of your server (could be a domain name)
  ServerName localhost
  
  # Switch on SSL
  SSLEngine on

  # Paths to Self-Signed certificate and key
  SSLCertificateFile /etc/ssl/certs/docker.crt
  SSLCertificateKeyFile /etc/ssl/certs/docker.key
</VirtualHost>

The Self-Signed Certificates

You can redirect to the 'certs' directory and generate the self-signed certificate using the following command:

openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout docker.key -out docker.crt

The Application

My application consisted of just an 'index.php' file in the 'app' directory. The contents of the 'index.php' file are as follows:

<?php
$db = new PDO('mysql:host=localhost', 'root', null);

function getOSInformation()
 {
     if (false == function_exists("shell_exec") || false == is_readable("/etc/os-release")) {
         return null;
     }

      $os         = shell_exec('cat /etc/os-release');
     $listIds    = preg_match_all('/.*=/', $os, $matchListIds);
     $listIds    = $matchListIds[0];

      $listVal    = preg_match_all('/=.*/', $os, $matchListVal);
     $listVal    = $matchListVal[0];

      array_walk($listIds, function(&$v, $k){
         $v = strtolower(str_replace('=', '', $v));
     });

      array_walk($listVal, function(&$v, $k){
         $v = preg_replace('/=|"/', '', $v);
     });

      return array_combine($listIds, $listVal);
 }
$osInfo = getOSInformation();
?>
<!doctype html>
<html lang=en>
<head>
    <meta charset=utf-8>
    <title>Hello World from Docker-LAMP</title>

    <style>
        @import 'https://fonts.googleapis.com/css?family=Montserrat|Raleway|Source+Code+Pro';

        body { font-family: 'Raleway', sans-serif; }
        h2 { font-family: 'Montserrat', sans-serif; }
        pre {
            font-family: 'Source Code Pro', monospace;

            padding: 16px;
            overflow: auto;
            font-size: 85%;
            line-height: 1.45;
            background-color: #f7f7f7;
            border-radius: 3px;

            word-wrap: normal;
        }

        .container {
            max-width: 1024px;
            width: 100%;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <img src="https://cdn.rawgit.com/mattrayner/docker-lamp/831976c022782e592b7e2758464b2a9efe3da042/docs/logo.svg" alt="Docker LAMP logo" />
            <h2>Welcome to <a href="https://github.com/mattrayner/docker-lamp" target="_blank">Docker-Lamp</a> a.k.a mattrayner/lamp</h2>
        </header>
        <article>
            <p>
                For documentation, <a href="https://github.com/mattrayner/docker-lamp" target="_blank">click here</a>.
            </p>
        </article>
        <section>
            <pre>
OS: <?php echo $osInfo['pretty_name']; ?><br/>
Apache: <?php echo apache_get_version(); ?><br/>
MySQL Version: <?php echo $db->getAttribute( PDO::ATTR_SERVER_VERSION ); ?><br/>
PHP Version: <?php echo phpversion(); ?><br/>
phpMyAdmin Version: <?php echo getenv('PHPMYADMIN_VERSION'); ?>
            </pre>
        </section>
    </div>
</body>
</html>

Building & Running The Solution

Building the Docker Image

To build the docker image for this app, you can redirect to the 'Lamp' directory (base directory) and run the following command:

docker build . -t lamp

Running the Docker Image on a Docker Container

To run the docker image we just built, you can run the following command:

docker run -p 443:443 lamp

IMPORTANT NOTE - Your app will only be accesible once you see the container logs saying 'apache2 entered RUNNING state' and 'mysqld entered RUNNING state'

Accessing the Application

You should be able to access the app at 'https://localhost'

IMPORTANT NOTE - For first time access, you will see a 'Your Connection is not private' warning, you can click 'Advanced' and also click 'proceed to "https://localhost"'

Upvotes: 3

Related Questions