I recently changed a virtual server, and I had to configure everything all over again. I prefer the site to be accessible via https and letsencrypt certificates are obtained and renewed automatically. This can be achieved by using two docker images, nginx-proxy and nginx-proxy-companion.
This guide is how to set up a website on docker, with a proxy, which automatically receives SSL certificates. The CentOS 7 virtual server is used.
I assume that the server has already been purchased, configured, it is accessed via a key, fail2ban is installed, etc.
First you need to install docker.
$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum install docker-ce docker-ce-cli containerd.io
$ sudo systemctl enable docker $ sudo systemctl start docker
$ usermod -aG docker user
The next step is to install docker-compose. The utility can be installed in several ways, but I prefer to install through the pip manager and virtualenv, so as not to clutter up the system with unnecessary packages.
$ sudo yum install python-pip
$ pip install virtualenv
$ mkdir docker $ cd docker $ virtualenv ve
$ source ve/bin/activate
pip install docker-compose
In order that containers saw each other, we will create a network. The default driver is bridge.
$ docker network create network
Next you need to configure docker-compose, the proxy will be in the proxy folder, the test site in the test folder. For example, I use the domain name example.com
$ mkdir proxy $ mkdir test $ touch proxy/docker-compose.yml $ touch test/docker-compose.yml
Proxy / docker-compose.yml content
version: '3' networks: default: external: name: network services: nginx-proxy: container_name: nginx-proxy image: jwilder/nginx-proxy ports: - 80:80 - 443:443 volumes: - certs:/etc/nginx/certs - vhost.d:/etc/nginx/vhost.d - html:/usr/share/nginx/html - /var/run/docker.sock:/tmp/docker.sock:ro nginx-proxy-letsencrypt: container_name: nginx-proxy-letsencrypt image: jrcs/letsencrypt-nginx-proxy-companion volumes: - certs:/etc/nginx/certs - vhost.d:/etc/nginx/vhost.d - html:/usr/share/nginx/html - /var/run/docker.sock:/var/run/docker.sock:ro environment: - NGINX_PROXY_CONTAINER=nginx-proxy volumes: certs: vhost.d: html:
The environment variable NGINX_PROXY_CONTAINER is needed so that the letsencrypt container sees the proxy container. The / etc / nginx / certs /etc/nginx/vhost.d and / usr / share / nginx / html folders must be shared between the two containers. For the letsencrypt container to work correctly, the application must be available on both port 80 and port 443.
Content test / docker-compose.yml
version: '3' networks: default: external: name: network services: nginx: container_name: nginx image: nginx:latest environment: - VIRTUAL_HOST=example.com - LETSENCRYPT_HOST=example.com - LETSENCRYPT_EMAIL=admin@example.com
Here, the environment variables are needed for the proxy to correctly process the request to the server and request a certificate for the correct domain name.
It remains only to run docker-compose
$ cd proxy $ docker-compose up -d $ cd ../test $ docker-compose up -d
Source: https://habr.com/ru/post/445448/
All Articles