What is reverse proxy?
A reverse proxy allows you to send multiple domain names to the same IP address, and then route the traffic to individual containers. This article will show how to set it up.
Prerequisites
You will need a Docker server with public IP address and a non-root user with sudo privileges. You could install Docker on AWS.
nginx-proxy container
Start by running the following commands on Docker
$ sudo docker network create nginx-proxy
$ sudo docker run -d -p 80:80 --name nginx-proxy --net nginx-proxy -v /var/run/docker.sock:/tmp/docker.sock jwilder/nginx-proxy
This will start the nginx-proxy container, which will automate the configuration of the nginx service for any container started on the nginx-proxy network.
Docker Compose
All Containers must be created using the nginx-proxy network. Using the example docker-compose file below, you must change the following highlighted items:
- db_node_domain: This is a unique db container name for each website
- Passwords: There is a password for MySQL root, and a MySQL wordpress user.
- VIRTUAL_HOST: The domain that you want to route to this Wordpress instance.
- container_name: The two container names will need to be unique for each website.
version: "3"
services:
my_school_domain:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: PASSWORD
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: PASSWORD
container_name: wordpress_db
wordpress:
depends_on:
- my_school_domain
image: wordpress:latest
expose:
- 80
restart: always
environment:
VIRTUAL_HOST: my.school.domain.com
WORDPRESS_DB_HOST: my_school_domain:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: PASSWORD
container_name: wordpress
volumes:
db_data:
networks:
default:
external:
name: nginx-proxy
Run the following command to start this docker-compose stack:
sudo docker-compose up -d
Thanks for visiting.