Quickly setup HTTPS on PHP Apache2 Docker container with self-signed SSL certificate

I often use local Docker images for development reasons, and sometimes, I need to implement features that only works in HTTPS environment: i.e. JWT Bearer token.

So, I prepared a Docker container based on official PHP Apache2 Docker container, that quickly setup HTTPS environment with a self-signed certificate.

Follows a starting docker-compose structure that you can be extended as needed. It contains only 2 files:

docker-compose.yml
apache2/Dockerfile

Let’s see file content.

docker-compose.yml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
version: "3.7"
services:

  php-ssl:
    build:
      context: ./apache2/
      dockerfile: Dockerfile
    ports:
      - 8081:80
      - 8443:443
    volumes:
      - ../html:/var/www/html
    networks:
      - php

networks:
  php:

volumes:
  data:
  

apache2/Dockerfile:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
FROM php:7.4-apache

MAINTAINER Valerio Galano

# Prepare apt
RUN apt-get update

# Prepare fake SSL certificate
RUN apt-get install -y ssl-cert

# Setup Apache2 mod_ssl
RUN a2enmod ssl

# Setup Apache2 HTTPS env
RUN a2ensite default-ssl.conf

# Work directory
WORKDIR /var/www/html

Quick & dirty.

I hope it can be useful.

Docker  PHP  SSL  HTTPS 

See also