Use docker-compose to build and deploy Caddy

Original link: https://blog.csdn.net/LZY_1993/article/details/103943065

1. Create a directory (under the root directory)

$ mkdir -p caddy/{conf,logs,www}
$ cd caddy

The directory structure is a personal habit and can be configured according to your own habits.
 
2. Configure Caddyfile

$ vi conf / Caddyfile
www.?????.net {
gzip
root /opt/www 
timeouts 30s
log /opt/logs/access.log 
proxy /other localhost:8080 
proxy /more localhost:8081 localhost:8082 #负载均衡
}

The above is the configuration after configuring the public domain name, but has not configured the domain name yet? Then just use IP, as follows

192.168 . 1.2 : 80  192.168 . 1.2 : 443 {
 gzip 
root / opt / www 
tls self_signed #Automatically signed, very important 
timeouts 30s 
log / opt / logs / access.log 
proxy / other localhost: 8080 
}

 

I believe many people will be like me. Caddy is deployed on a virtual machine, but the access is on a physical machine. If you write a domain name according to some online configuration documents, you will not be able to access it. You will not be in the same environment. Enter it on the physical machine. The custom domain name is impossible to resolve IP access to your virtual machine.

3 、 配置 docker-compose.yml

$ vi docker-compose.yml
version: '3.3'

services:
# http/2 server
caddy:
image: abiosoft/caddy:latest
container_name: caddy
hostname: caddy
domainname: caddy
restart: always
network_mode: "host"
environment:
- TZ=Asia/Shanghai
- agree
- email ?????@qq.com
volumes:
- "/root/caddy/conf/Caddyfile:/etc/Caddyfile"
- "/root/caddy/.caddy:/root/.caddy"
- "/root/caddy/logs:/opt/logs"
- "/root/caddy/www:/opt/www"
ports:
- 80:80
- 443:443

 

Three of the environment variables (environment) are very important
 1. Configure the Shanghai time zone
 2. Agree to caddy to automatically configure the ssl certificate
 3. Caddy needs the feedback mailbox 

Note that if you customize your own directory structure, the configuration under the data volume here should also correspond to your own directory structure

 
4. Start

$ docker-compose up- d 
$ docker logs -f caddy # View logs

 

Ok? Haven't installed docker-compose yet?

# Install docker- compose 
$ sudo curl -L https: // github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o / usr / local / bin / docker-compose 

# Add directory permissions 
$ sudo  chmod + x / usr / local / bin / docker- compose 

# Verify that docker - compose installation is successful 
$ docker -compose --version

 

5.
Just copy a file to the www directory, I copied a picture bg.jpg,
and then enter https://192.168.1.2/bg.jpg

After the Firefox browser is opened, the certificate is not trusted. Click Advanced and continue to access
 
6. Related commands

$ docker- compose up # Start the container in the foreground, when the command exits, all containers will stop 

$ docker -compose up- d # Start the containers in the background and keep them running 

$ docker logs - f [container_id] # View a container ’s Started log printing (log printed from scratch) 

$ docker logs -f- tail [quantity] [container_id] # View the started log printing of a container (check the last n log prints). Example: docker logs -f- tail  50 44b 

$ docker - compose stop # Stop compose service 

$ docker - compose restart # Restart compose service 

$ docker -compose kill      # kill compose service 

$ docker -compose ps #View      compose service status 

$ docker-compose rm #Delete      compose service

 




Guess you like

Origin www.cnblogs.com/lzy1993/p/12727382.html