前言
作为开发经常需要本地数据库,docker可以快速部署,并且可以快速删除。总结了一下常用数据库,mysql,redis,mongo,postgres四种数据库部署
version: "3.5"
services:
mongo-data:
image: mongo
container_name: mongo
environment:
NETWORK:
volumes:
- ./mongo/mongo_configdb:/data/configdb
- ./mongo/mongo_db:/data/db
ports:
- 27017:27017
restart: on-failure
logging:
driver: "json-file"
options:
compress: "true"
max-file: "10"
max-size: "50m"
redis-data:
image: redis
container_name: redis
volumes:
- ./redis.conf:/etc/redis/redis.conf
- ./redis/data:/data
ports:
- 6379:6379
entrypoint: []
environment:
NETWORK:
restart: on-failure
logging:
driver: "json-file"
options:
compress: "true"
max-file: "10"
max-size: "50m"
mysql:
# 镜像名
image: mysql:8.0.21
# 容器名(以后的控制都通过这个)
container_name: mysql
# docker 验证
security_opt:
- seccomp:unconfined
# 重启策略
restart: on-failure
environment:
# 时区上海
TZ: Asia/Shanghai
LANG: C.UTF-8
# root 密码
MYSQL_ROOT_PASSWORD: root1234
# 初始化数据库(后续的初始化sql会在这个库执行)
MYSQL_DATABASE: mall
# 初始化用户(登录 mysql -uroot -p )
MYSQL_USER: root
# 用户密码
MYSQL_PASSWORD: root1234
# 映射端口
ports:
- 3306:3306
volumes:
# 数据挂载
- ./mysql/data/:/var/lib/mysql/
# 配置挂载
- ./mysql/conf/:/etc/mysql/conf.d/
# 初始化目录挂载
- ./mysql/init/:/docker-entrypoint-initdb.d/
# command:后面添加启动参数
command:
# 将mysql8.0默认密码策略 修改为 原先 策略 (mysql8.0对其默认策略做了更改 会导致密码无法匹配)
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
# https://github.com/felipewom/docker-compose-postgres
postgres:
image: 'postgres:latest'
container_name: postgres
ports:
- 5432:5432
environment:
POSTGRES_USER: user # 用户名
POSTGRES_PASSWORD: pass # 密码
POSTGRES_DB: auth # 自定义库名称 The PostgreSQL default database (automatically created at first launch)
volumes:
- ./db-data/:/var/lib/postgresql/data/
常用命令
#启动
docker-compose up -d
# 关闭
docker-compose stop
[关闭后]移除
docker-compose rm
强制关闭并移除镜像. 还有参数v 表示移除卷积数据,一般不用
docker-compose rm -fs