Docker Compose简单配置Mysql Nginx挂载持久化

警告
本文最后更新于 2021-04-22,文中内容可能已过时。

记录片段:用 docker-compose 简单 mysql 与 nginx 两个容器,并挂载 volumes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
version: "3"
services:
  mysql:
    image: mysql:5.7
    container_name: mysql
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=***
    volumes:
      - ~/mysql/datas:/var/lib/mysql
      - ~/mysql/conf/my.cnf:/etc/my.cnf
    ports:
      - "3306:3306"
  nginx:
    image: nginx:latest
    restart: always
    volumes:
      - ~/nginx/logs:/var/log/nginx
      - ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
      - ~/nginx/www:/usr/share/nginx/html
    ports:
      - "80:80"

创建``datasconf 两个空目录,编写my.cnf文件放入conf`文件夹,配置如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4

创建logsconfwww三个空目录,编写nginx.conf文件放入conf文件夹。

当我们只有一个端口可以开Server时,就需要一个Server开启多个location来写规则,具体需求就是在www目录下有homedocs两个目录,通过不同的后缀去访问不同的页面,规则这样写:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
Server {
  listen 80;
  listen [::]:80;

  charset     utf-8;
  server_name location;

  location / {
    root /usr/share/nginx/html;
    index index.html;
  }

  location /docs {
    alias /usr/share/nginx/html/docs/;
    index index.html;
  }
}

需要注意的是rootalias的区别,一个是从root下跟location路径,一个是通过location路径的别名到alias的路径。

这时候,又发现了一个问题,那就是如果不加/,就会出现端口丢失问题,因为容器内映射的端口是 80 端口,外边公网给的 ip 是另一个,而 80 是浏览器 web 页面的默认端口,举个 🌰,47.1.2.1:8090/docs没有加/就会经过 nginx 的 301 重定向到47.1.2.1/docs/,由于端口丢失,即使重定向到带有/的地址也只是 404,这时只需要修改为如下配置即可。

1
2
3
4
5
6
7
location /docs {
    alias /usr/share/nginx/html/docs/;
    index index.html;
    if (-d $request_filename) {
      rewrite [^/]$ $scheme://$http_host$uri/ permanent;
    }
  }

2021-04-22补充redis的配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
version: "3"
services:
  redis:
    image: redis:alpine
    container_name: redis
    restart: always
    volumes:
      - ../redis/data:/data
      - ../redis/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ../redis/logs:/logs
    command: redis-server /usr/local/etc/redis/redis.conf
    ports:
      - "6379:6379"