Python工具箱系列(三十六)

基于Docker的数据库开发环境

前文介绍了sqlite/mysql/mssql等数据库系统在ubuntu的安装与部署过程,相对是比较复杂的,需要耐心等待下载以及排除各种故障,对于开发人员来说是不太友好。在某些情况下,开发人员要测试在多个数据库环境下软件的正确性,需要部署多个数据库,为此可以使用Docker技术。

Docker提供了基于镜像的运行环境,可以将操作系统、应用程序以及相关依赖打包,为使用者提供完整的使用体验,因此一经推出大获好评,迅速成为主流的软件开发技术之一。Docker有着完善的命令行功能,在Windows环境下有多种GUI管理软件,本文对Docker不再赘述。下面先在ubuntu bionic上建立Docker服务,随后基于Docker composer将多个数据库打包启动,为开发者提供多数据库支持环境。

# 删除旧版本
sudo apt-get remove docker docker-engine docker.io containerd runc
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common

# 真正有用的部分
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

# aliyun mirrors
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

sudo apt-get update
sudo apt-get install -y docker-ce
sudo systemctl enable docker
sudo systemctl start docker

sudo usermod -aG docker $USER
sudo systemctl daemon-reload
sudo systemctl restart docker

# 不在此机上开发python可以直接安装这个基于python2.7.17的低版本应用
apt install docker-compose

使用scp或者直接vi编辑Docker-compse.yml文件如下:

version: "3.3"

services:
    clickhouseserver:
        restart: always
        image: yandex/clickhouse-server:21.6.5.37-alpine
        container_name: clickhouseserver
        hostname: clickhouseserver
        environment:
            - TZ=Asia/Shanghai
        ports:
            - 8123:8123
            - 9000:9000
        ulimits:
            nofile:
                soft: 262144
                hard: 262144  
                
    sqlserver:
        image: mcr.microsoft.com/mssql/server:2019-CU3-ubuntu-18.04
        restart: always
        container_name: mssql
        environment: 
            - ACCEPT_EULA=Y
            - SA_PASSWORD=Zm88488848
        ports:
            - 1433:1433

    mysql:
        image: mysql
        command: --default-authentication-plugin=mysql_native_password
        restart: always
        ports:
            - 3306:3306
        environment:
            MYSQL_ROOT_PASSWORD: 88488848

    pg:
        image: postgres
        restart: always
        ports:
            - 5432:5432
        environment:
            POSTGRES_PASSWORD: 88488848

随后运行以下命令启动。

# 启动编排,可以按ctrl-c中止
docker-compose up

# 类似于服务的永久启动。
docker-compose up -d

在第一次启动时,由于本地没有对应的镜像,会自动下载,时间稍长些。后续启动时将非常快速。由于以上数据库不真正投入生产环境,所以没有引入挂载点。此外,根据官方文档要求,sqlserver镜像在配置时,必须使用强口令(8位大小写字母与符号的混合)。另,8848的意思是珠峰的高度。

猜你喜欢

转载自blog.csdn.net/shaanxihualu/article/details/131310914